收录日期:2019/06/20 13:36:17 时间:2010-06-22 20:34:45 标签:sockets,winsock

It is pure winsock server question. Just to clear that I already know how threads work. I have global sockets called Global and 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 ??????>>>>>>>>>>>>>>>>
}

What I am trying to do is whenever I receive new connection on socket m_socket I accept it on socket Global, then start new thread and then I want a Local socket within thread to start using accepted socket on Global, and that's all.

A quick answer or fix will be appreciated rather than links.

Bye

Can't you pass the new socket (GLOBAL) as a parameter to CreateThread?