views:

17

answers:

1

Hello there guys

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

A: 

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

ninjalj
And do what after it is passed friend?CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)newthreadfunction, (void*)Global, 0, 0);and in thread functionSOCKET Local = (SOCKET)OneParam;Where void* OneParam is now parameter to thread function.Now what to do ? To use Local.
Jason z
ANSWER<code>CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)newthreadfunction, (void*)Global, 0, 0);</code>And in thread function<code>long WINAPI newthreadfunction(void * PTR) { SOCKET Local = (SOCKET)PTR; /*use local now */}</code>thanks ninjalj
Jason z