* [ECOS] RE : I lose sockets
@ 2006-08-17 8:16 Goldschmidt Simon
0 siblings, 0 replies; only message in thread
From: Goldschmidt Simon @ 2006-08-17 8:16 UTC (permalink / raw)
To: ecos-discuss
Hi Roger,
>> You have the basic TCP server structure wrong. You should create one
listen'ing socket and then keep it for
>> ever, repeatadly doing accept() on it as each client connects. ie you
don't need a listening socket per
>> connection.
>Now I've made the following structure (simplistic)
>socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); bind(hmiParameter->IPsocket,
(struct sockaddr *) &local, sizeof(local));
listen(hmiParameter->IPsocket, SOMAXCONN);
>
>Connect: accept(hmiParameter->IPsocket, (struct sockaddr
*)&client_addr,
>&x))
>while(true) {
> select(hmiParameter->client + 1, &in_fds, 0, 0, &tv);
> if(read(hmiParameter->client, buf, sizeof(buf)-1) <= 0) {
> goto Connect;
> }
>}
>
>( remark: close(hmiParameter->IPsocket) will never be called )
TCP state machine needs the client to close a connection first, before
the server closes it.
Otherwise sockets must stay in a half-open state for 3 minutes. If you
ensure the client closes the connection first, this code would make sure
you don't loose sockets:
int listen_sock=-1;
int client_sock=-1;
...
listen(listen_sock, ..);
...
while(true)
{
client_sock = accept(listen_sock ,...);
// now, the actual connection:
recv(client_sock, ..);
send(client_sock, ..);
... // some more communication...
// and if the client finally wants to disconnect:
close(client_sock);
}
Closing a connection from the server side first won't fail, but you
'loose' sockets (for some minutes) because they stay in TIME_WAIT state.
Hope this is an answer to your problem.
Simon.
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2006-08-17 8:16 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-17 8:16 [ECOS] RE : I lose sockets Goldschmidt Simon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).