Click to See Complete Forum and Search --> : socket thread question
pouncer
April 6th, 2006, 09:22 PM
when i create a thread like this..
sock_thread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)Socket_Thread, 0, 0, &sock_thread_id);
DWORD WINAPI Socket_Thread() {
}
the thread is based on the winsock 'sock'
how can i make a 1 thread work for an array of sockets?
wildfrog
April 7th, 2006, 04:26 AM
the thread is based on the winsock 'sock'What do you mean by 'based on the winsock 'sock''? The thread has nothing in common with winsock (except the name similarity).
- petter
pouncer
April 7th, 2006, 07:53 AM
SOCKADDR_IN adder;
adder.sin_family = AF_INET;
adder.sin_addr.s_addr = inet_addr(IRC_Channel_IP);
adder.sin_port = htons(6667);
connect(sock, (SOCKADDR*)&adder, sizeof(adder));
sock_thread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)Socket_Thread, 0, 0, &sock_thread_id);
//thread
DWORD WINAPI Socket_Thread() {
//handles the main recv() buffer etc..
while (!stop) {
num_recvd = recv(sock, recv_buffer, 1024, 0);
if (recv_buffer = "hello") { send sock "hello!"; }
//etc etc
}
}
the thread is only for the 'sock', and only recevies data from:
recv(sock, recv_buffer, 1024, 0);
i want to make it perfrom the same actions (in this thread) on a number of different sockets which are connected to the same ip
wildfrog
April 7th, 2006, 08:01 AM
You can use WSAEventSelect (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsaeventselect_2.asp) to make one or more socket signal events when they're ready for reading/writing/accepting etc. Then you use WaitForMultipleObject (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitformultipleobjects.asp) in you thread loop to wait for those events.
- petter
pouncer
April 7th, 2006, 08:20 AM
thanks mate. and this needs an array of sockets doesn't it ?
maybe i'll have at the top
int MAXSOCKET = 0;
SOCKET sock[];
and then when i connect
connect(sock[MAXSOCKET], (SOCKADDR*)&adder, sizeof(adder));
MAXSOCKET++;
do you think this will be ok?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.