By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Sockets question

P: n/a
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.

Feb 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 24, 11:01 am, "Karim" <karim.els...@gmail.comwrote:
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.
What are all of the C style casts for? You should replace them with
whatever C++ casts are appropriate for the casts you're using.

I had a problem where I was rather stupidly using a C cast interfacing
with a legacy library and had put it at the wrong level of a
structure. This was causing the program to write over the wrong buffer
causing all sorts of weird behaviour (I was actually seeing the this
pointer change when calling between methods on the same object). It
took me days to track it down because the cast always looked right. It
was only changing to C++ casts that let the compiler spot my stupid
mistake.
K

Feb 24 '07 #2

P: n/a
On Feb 24, 12:30 am, "Kirit Sælensminde"
<kirit.saelensmi...@gmail.comwrote:
On Feb 24, 11:01 am, "Karim" <karim.els...@gmail.comwrote:
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.

What are all of the C style casts for? You should replace them with
whatever C++ casts are appropriate for the casts you're using.

I had a problem where I was rather stupidly using a C cast interfacing
with a legacy library and had put it at the wrong level of a
structure. This was causing the program to write over the wrong buffer
causing all sorts of weird behaviour (I was actually seeing the this
pointer change when calling between methods on the same object). It
took me days to track it down because the cast always looked right. It
was only changing to C++ casts that let the compiler spot my stupid
mistake.

K
Unfortunately, this doesn`t solve the problem. Any more ideas?

Feb 24 '07 #3

P: n/a
On Feb 23, 10:01 pm, "Karim" <karim.els...@gmail.comwrote:
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.
I'm not sure how you conclude that since you receive corrupted data on
the clients that the server must be sending it using the wrong
descriptor. Maybe you can elaborate as to what you actually observed
and why you drew that conclusion.

Without going into detail, while I would suggest many changes in the
snippet above... functionally, it should work (at least in the success
scenario)... If you're getting buffer corruption, the first place I'd
check is both your send/receive buffers and especially the buffer
length used in reading from or writing to the socket, that's usually
the culprit. Also, it may be a good idea to at least check some of the
return codes from your socket calls, and make sure your global errno
is not set.

Feb 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.