Winsock:接受全局套接字,启动新线程并复制到那里,释放以前的全局

时间:2010-06-22 20:34:45

标签: sockets winsock

这是纯winsock服务器问题。只是为了清楚我已经知道线程是如何工作的。 我有全局套接字Global和Main_Socket。

long __stdcall newthreadfunction(); //prototype of new thread function

SOCKET Global; // To be shared and copied by thread
SOCKET Main_Socket; //main listening socket

WSADATA wsaData;
sockaddr_in service;


int main() {

........ DO STARTUP THINGS HERE ........

   Main_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 service.sin_family = AF_INET;
 service.sin_addr.s_addr = inet_addr(IP);//"127.0.0.1"
 service.sin_port = htons(PORT);
   bind(Main_Socket, (SOCKADDR*)&service, sizeof(service));

   while(1) {
     Global = SOCKET_ERROR;
     while (Global == SOCKET_ERROR)  {
      Global = accept(Main_Socket, NULL, NULL); 
      CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)newthreadfunction, 0, 0, 0);  
     }
   }
...........CLEANUP HERE ..............
}

long __stdcall newthreadfunction() {
 SOCKET Local = ?????????????????????; 

 What to do here so that the accepted connection (on Global) gets attached to 
        SOCKET Local and it starts sending and receiving independently from Global and 
        Main_Socket ??????>>>>>>>>>>>>>>>>
}

我想要做的是每当我在套接字m_socket上接收到新连接时,我在socket Global上接受它,然后启动新线程,然后我想在线程中的一个本地套接字开始使用Global上的接受套接字,这就是全部。

快速回答或修复将被欣赏而不是链接。

再见

1 个答案:

答案 0 :(得分:0)

你不能将新套接字(GLOBAL)作为参数传递给CreateThread吗?