This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
RE : I lose sockets
- From: "Goldschmidt Simon" <sgoldschmidt at de dot pepperl-fuchs dot com>
- To: <ecos-discuss at sourceware dot org>
- Date: Thu, 17 Aug 2006 10:16:45 +0200
- Subject: [ECOS] RE : I lose sockets
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