由于您已经了解套接字和线程,因此我将想法伪代码发送给您(如果需要代码的特定部分,请告诉我)
您没有提到的一件事是如何通过其 IP o 通过任何其他方法(如 ID)来跟踪客户?任何给定的设备可以打开多个具有不同客户端 ID 的连接吗?或者您只接受每台设备的一个连接?无论如何,如果客户已经在列表中,您打算怎么做?您会将创建的线程与新套接字进行通信吗?您会销毁该线程并创建一个新线程吗?或者您可能会忽略这个新请求?
这是我的想法(取自一个工作应用程序):
服务器准备服务器套接字并在接受状态下等待。
一旦客户端连接,服务器就会启动一个线程来处理客户端,传递它刚刚使用 accept 命令创建的套接字。当参与客户端的线程启动时,它从客户端接收到的第一条消息应该是一个密码或特殊签名,以便让客户端进入(这是可选的)。
服务器代码:
Prepares the server socket which listen in a well known port
Clear client list;
While (!Terminated)
{
// if you want to impose a limit for connections, check it here:
if (Is the list of connected client full?)
{
Sleep(reasonable time in seconds or miliseconds);
continue;
}
ClientSocket = ServerSocket.Accept();
if the client's IP is already in the list
{
depends on what you want to do.
}
else
{
Add client's IP to the list
Start (create) new client Tread(ClientSocket);
}
}
// when server finish
If (client list is not empty?)
{
Kill all threads
or
Wait until all threads are done
or
Wait an amount of time and then kill those remaining.
}
线程客户端代码:
// This is optional, just to make sure a valid client is connected
Read packet from ClientSocket
if (!Is_the_passport_packet)
{
close socket;
return;
}
// if passport is not required, start here
Try
{
While (!Terminated)
{
if (read packet from client);
{
switch (packet.Command)
{
// In your question you said you want the Server thread to process the request
// I guess you have your requirements to do so,
// anyway, you must use a mutex o some other synchronization method.
case TASK_1:
[sync] process TASK_1(packet, ClientSocket);
break;
case TASK_2:
[sync] process TASK_2(packet, ClientSocket);
break;
etc ….
case WORK_DONE:
Close Socket;
return;
default:
Log(received an unknown command: packet.command);
break;
}
}
else if (Client has quit (closed/broken socket))
{
// as you may know, a socket is consider shutdown when you received a 0 length data
// and a broken connection when received -1 in either case all you have to do is
Close Socket;
return;
}
}
}
catch (Exception e)
{
Log(received an exception: e.message);
}
finally
{
Remove this client from the client's list
}