2010-06-22 15 views
0

C'est une question de serveur pure winsock. Juste pour clarifier que je sais déjà comment fonctionnent les threads. J'ai des sockets globales appelées Global et Main_Socket.Winsock: Accepter sur le socket global, démarrer un nouveau thread et copier là-bas, libre précédent global

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 ??????>>>>>>>>>>>>>>>> 
} 

Ce que je suis en train de faire est chaque fois que je reçois une nouvelle connexion sur la prise m_socket je l'accepte sur socket globale, puis commencer à nouveau thread et je veux un socket local à l'intérieur fil pour commencer à utiliser socket accepté Global, et c'est tout.

Une réponse rapide ou une correction sera appréciée plutôt que des liens.

Bye

Répondre

0

Tu ne peux pas passer le nouveau socket (GLOBAL) en tant que paramètre à CreateThread?

+0

Et faire quoi après qu'il soit passé ami? CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) nouvellefonctionnalité, (void *) Global, 0, 0); et dans la fonction de filetage SOCKET Local = (SOCKET) OneParam; Où void * OneParam est maintenant le paramètre à fonction de thread. Maintenant, que faire? Pour utiliser Local. –

+0

RÉPONSE CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)newthreadfunction, (void*)Global, 0, 0); Et en fonction de fil long WINAPI newthreadfunction(void * PTR) { SOCKET Local = (SOCKET)PTR; /*use local now */ } grâce ninjalj –