【发布时间】:2011-12-15 20:58:07
【问题描述】:
我想更改我使用的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 的客户端?
我很抱歉我不能表现出更多的努力,我不喜欢给人懒惰的印象。但是对于我搜索的所有时间以及由此产生的反复试验,我没有太多可以展示的东西。
【问题讨论】:
-
例如,您可以为每个连接的客户端生成一个新线程。
-
我会推荐 boost asio。
-
但是我什么时候会将连接的客户端分叉到新线程?就像..在我设置侦听器端口之前,这意味着每个客户端都需要自己的端口,或者之后。如果之后,我将如何让班级异步接受消息?我真的 100% 不知道该怎么做。
-
'但是在什么时候我会将连接的客户端分叉到新线程' - 在接受()返回服务器客户端套接字之后立即。将套接字从 accept() 传递给新线程。如果您要像这样为每个新的客户端连接创建一个新线程,通常只在客户端-服务器线程中运行同步调用。
标签: c++ windows multithreading sockets