public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
From: "Goldschmidt Simon" <sgoldschmidt@de.pepperl-fuchs.com>
To: <ecos-discuss@sourceware.org>
Subject: [ECOS] RE : I lose sockets
Date: Thu, 17 Aug 2006 08:16:00 -0000	[thread overview]
Message-ID: <651FDF993328694286BDDA6A0A3606B912E187@pfde-mx5.EU.P-F.BIZ> (raw)

 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

                 reply	other threads:[~2006-08-17  8:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=651FDF993328694286BDDA6A0A3606B912E187@pfde-mx5.EU.P-F.BIZ \
    --to=sgoldschmidt@de.pepperl-fuchs.com \
    --cc=ecos-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).