Sockets question
Hi,
I have a server that it very simple. I declare a socket and I get a
descriptor and when calling listen i pass the value 5
(listen(g_socketDescriptor, 5) so I can queue 5 connections on that
socket.
Every time the blocking call to Accept() returns, I get back a new
descriptor say x1 ,x2,... x5
and I spawn a thread that does some heavy processing and write back to
the socket descriptor (x1,..,x5) that the thread was spawned with then
after a bit, it writes to this socket.
on the client side I noticed sometimes corrupted data which means the
threads when they write to those descriptors (using the call send()) i
might be sending data to the wrong client.
My understanding is that the descriptor is a unique identifier that i
could use to decide who to exactly send the data too from the
server...
Can anyone see what is going wrong here? The code is too big so I`ll
just maybe give a snapshot.
g_socketDescriptor = socket(PF_INET,SOCK_STREAM,0);
bind(g_socketDescriptor,(struct sockaddr*)&socketAddress,
sizeof (socketAddress)
listen(g_socketDescriptor,5)
int finalId;
pthread_t tid;
while (true)
{
if ((finalId = accept(g_socketDescriptor,
(struct sockaddr*)&remoteAddress,
&remoteAddressLen)) == -1)
{
SpawnNewThreadHere(finalId);
}
}
Thread code:
MainThread(finalId)
{
// do some processing, then send some data
send(finalId,SomeBuffer);
}
~Thread()
{
close(finalId);
}
Thanks.