我想更改我用来接受无限量客户端的socket class。目前它允许一个客户端,并且一旦该客户端断开服务器退出。让套接字服务器接受多个客户端
#include "stdafx.h"
#include "mySocket.h"
#include "myException.h"
#include "myHostInfo.h"
void main()
{
#ifdef WINDOWS_XP
// Initialize the winsock library
WSADATA wsaData;
try
{
if (WSAStartup(0x101, &wsaData))
{
myException* initializationException = new myException(0,"Error: calling WSAStartup()");
throw initializationException;
}
}
catch(myException* excp)
{
excp->response();
delete excp;
exit(1);
}
#endif
// get local server information
myHostInfo uHostAddress;
string localHostName = uHostAddress.getHostName();
string localHostAddr = uHostAddress.getHostIPAddress();
cout << "------------------------------------------------------" << endl;
cout << " My local host information:" << endl;
cout << " Name: " << localHostName << endl;
cout << " Address: " << localHostAddr << endl;
cout << "------------------------------------------------------" << endl;
// open socket on the local host
myTcpSocket myServer(PORTNUM);
cout << myServer;
myServer.bindSocket();
cout << endl << "server finishes binding process... " << endl;
myServer.listenToClient();
cout << "server is listening to the port ... " << endl;
// wait to accept a client connection.
// processing is suspended until the client connects
cout << "server is waiting for client connecction ... " << endl;
myTcpSocket* client; // connection dedicated for client communication
string clientHost; // client name etc.
client = myServer.acceptClient(clientHost);
cout << endl << "==> A client from [" << clientHost << "] is connected!" << endl << endl;
while(1)
{
//Send message to the client
client->sendMessage(std::string("Test"));
// receive from the client
string clientMessageIn = "";
int numBytes = client->recieveMessage(clientMessageIn); //Get message from client, non-blocking using select()
if (numBytes == -99) break;
if(clientMessageIn != "")
{
std::cout << "received: " << clientMessageIn << std::endl; //What did we receive?
/* Do somethign with message received here */
}
}
#ifdef WINDOWS_XP
// Close the winsock library
try
{
if (WSACleanup())
{
myException* cleanupException = new myException(0,"Error: calling WSACleanup()");
throw cleanupException;
}
}
catch(myException* excp)
{
excp->response();
delete excp;
exit(1);
}
#endif
}
如何改变main()函数,使其在不断等待新的客户端连接,而一旦他们这样做,为他创造一个新的线程(客户端),或新的处理器插槽(无论这可能是什么)。
我确实发现this线程是提供信息的,但是我缺少实际在上面的代码中实现它所需的套接字知识。
答案指出When doing socket communication, you basically have a single listener socket for all incoming connections, and multiple handler sockets for each connected client.
所以我在我的代码猜;
myTcpSocket myServer(PORTNUM);
myServer.bindSocket();
myServer.listenToClient();
将是listener socket
但是哪里/我将如何叉谁是连接开了一个handler socket
客户端?
我很抱歉无法在我的部分显示更多的努力,我不喜欢碰到懒惰。但是,在我搜寻的所有时间以及由此导致的反复试验中,我没有太多的展示。
例如,您可以创建新的线程为每个连接的客户端。 – Cyclonecode 2011-12-15 21:00:29
我会推荐加强asio。 – Bashwork 2011-12-15 21:03:27
但是我将在什么时候将连接的客户端分叉到新线程?就像在设置侦听器端口之前,意味着每个客户端都需要自己的端口,或者之后。如果事后,我将如何使这个类异步接受消息?如何做到这一点,我真的百分之百地迷失了方向。 – natli 2011-12-15 21:04:09