 |
|
 |
|
Next: frama-c override disparity
|
| Author |
Message |
External

Since: Nov 06, 2008 Posts: 4
|
(Msg. 1) Posted: Wed Jun 03, 2009 7:39 am
Post subject: TCP multiconnection server Archived from groups: comp>os>linux>development>system (more info?)
|
|
|
Hi List,
[and thanks in advance for all your effort! Hope not to be off topic
and if so please redirect me]
I've a TCP server with a blocking select awaiting for incoming
connections (on the original fd) and for packets from already
connected hosts (on the newly per-connection generated fds).
Everything works great as long as i ADD new connections, but when I
close ONE of the active connections on the CLIENT side (by closing the
application), I see that on the server the select returns and I
receives a zero bytes long message from the "recv". According to the
man page this is good as it tells the server that a connection was
closed. Upon such event I don't do any particular thing (I just
thought that It's a good idea to close the old client-fd). From this
point on, I don't seem to be listening to any new connection... I
mean, if I open a new client I don't get any reaction from the server.
Why is that?
I can feed you with as much data/code as you need, but basically what
I do is in the following "pseudo"C-code.
....
fd = socket(...)
bind(fd...);
listen(fd...);
while(1)
{
FDZERO(&set)
for(ALL_CONNECTED_CLIENTS) FD_SET(CLIENTFD, &set);
FD_SET(fd, &set)
select(maxfd+1, &set, 0, 0, NULL);
if(FD_ISSET(fd,&set)) accept(fd,...);
else
{
for(ALL_CONNECTED_CLIENTS)
if(IF_ISSET(CLIENTFD[x], &set)) i = recv(CLIENTFD[x],...);
if (i==0) close(CLIENTFD[x]);
}
} |
|
| Back to top |
|
 |  |
External

Since: Apr 25, 2007 Posts: 134
|
(Msg. 2) Posted: Wed Jun 03, 2009 8:29 am
Post subject: Re: TCP multiconnection server [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jun 3, 7:39 am, InuY4sha <inuy4... RemoveThis @gmail.com> wrote:
> I've a TCP server with a blocking select awaiting for incoming
> connections (on the original fd) and for packets from already
> connected hosts (on the newly per-connection generated fds).
> Everything works great as long as i ADD new connections, but when I
> close ONE of the active connections on the CLIENT side (by closing the
> application), I see that on the server the select returns and I
> receives a zero bytes long message from the "recv". According to the
> man page this is good as it tells the server that a connection was
> closed. Upon such event I don't do any particular thing (I just
> thought that It's a good idea to close the old client-fd). From this
> point on, I don't seem to be listening to any new connection... I
> mean, if I open a new client I don't get any reaction from the server.
> Why is that?
> I can feed you with as much data/code as you need, but basically what
> I do is in the following "pseudo"C-code.
[snip]
There's a bug in code you haven't shown. Most likely, when 'recv'
returns <=0, you probably don't remove the socket from your list of
connected sockets.
DS |
|
| Back to top |
|
 |  |
External

Since: Nov 06, 2008 Posts: 4
|
(Msg. 3) Posted: Thu Jun 04, 2009 2:00 am
Post subject: Re: TCP multiconnection server [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
> There's a bug in code you haven't shown. Most likely, when 'recv'
> returns <=0, you probably don't remove the socket from your list of
> connected sockets.
I found the bug... I didn't include the original socket fd in the int
maxfd (+1) variable used by the select, after a closed connection!
SOLVED |
|
| Back to top |
|
 |  |
External

Since: Nov 06, 2008 Posts: 4
|
(Msg. 4) Posted: Thu Jun 04, 2009 2:04 am
Post subject: Re: TCP multiconnection server [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
According to the man page I'm reading from,
recv returns -1 if an error occurs which doesn't necessarly mean the
connection should be closed. When recv returns 0 on a connection
oriented socket, it means the client half closed his side of the link,
therefore we should close our side too.
So I would distinguish the 2 cases "<" and "==" and act differently.
Please correct me if I'm wrong?
Thanks in advance
> There's a bug in code you haven't shown. Most likely, when 'recv'
> returns <=0, you probably don't remove the socket from your list of
> connected sockets.
>
> DS |
|
| Back to top |
|
 |  |
External

Since: Apr 25, 2007 Posts: 134
|
(Msg. 5) Posted: Thu Jun 04, 2009 6:42 am
Post subject: Re: TCP multiconnection server [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jun 4, 2:04 am, InuY4sha <inuy4....DeleteThis@gmail.com> wrote:
> According to the man page I'm reading from,
> recv returns -1 if an error occurs which doesn't necessarly mean the
> connection should be closed. When recv returns 0 on a connection
> oriented socket, it means the client half closed his side of the link,
> therefore we should close our side too.
> So I would distinguish the 2 cases "<" and "==" and act differently.
> Please correct me if I'm wrong?
If you're going to handle the two cases differently, you need to
distinguish fatal from non-fatal errors. Otherwise, you'll blow up if
you ever encounter a fatal error (because you never handle it, the
kernel will never stop notifying you). The naive solution is to treat
all errors as fatal and treat a return of zero as the same as a return
of -1.
If you want handle half-closed connections (you can still send even
after recv returns zero) or non-fatal errors (no buffer space, for
example), of course, things will get more complicated.
DS |
|
| Back to top |
|
 |  |
| Related Topics: | RFC3971 - Does anyone know if RFC3971 support is being developed for Linux? It does not seem to be implemented in the mainline..
Size 8 bit, 16 bit, 32 bit and 64 bit systems. - I need to find out what is the size of following data structures in 8 bit, 16 bit, 32 bit, and 64 bit systems. struct....
Size 8 bit, 16 bit, 32 bit and 64 bit systems. - I need to find out what is the size of following data structures in 8 bit, 16 bit, 32 bit, and 64 bit systems. struct....
Access shared memory from kernel module - Hi All, I wanted to know if shared memory created in user space can be accessed from a loadable kernel module. Have no...
Controlling UART transmission of bytes - I'm programming an ARM's UART that comes with a library implementing the standard unix termios interface. Regarding..
FTP-Server (textbasiert) unter SuSE auf Strato-Root-Server - Hi Ich habe einen Strato-Root-Server und möchte auf diesem einen FTP-Server installieren... Kann mir jemand sagen wie... |
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
 |
|
|