Re: long lasting client socket connection



On 2012-01-13, Bill M <wpmccormick@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Rainer Weikusat wrote, On 1/13/2012 8:54 AM:
Kaz Kylheku<kaz@xxxxxxxxxxx> writes:
On 2012-01-12, Bill M<wpmccormick@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I'm working on a socket client app that makes a connection to a server
and leaves it open. Thinking that since he's repeatedly asking
questions, there's little point in wasting time closing and reopening
again. Are there any good design patterns out there for this I should
take a look at?

[...]

But seriously, noe thing you have to watch out for is keeping such connections
idle for too long. It's almost impossible to avoid such an application being
deployed by someone in a scenario where the client connets trhough one (or
more) NAT gateways to the server. When these routers don't see any activity
between a pair of destinations, they will expire the mapping entry, and then
your connection is gone.

The same is true for any kind of stateful firewall even if NAT
isn't being used (and people use NAT quite mindlessly because "that's
how it is always being done). If you want to keep a TCP connection
open, you have to include application layer keepalives (preferably
using a randomized interval) and application-transparent (disconnect
and) reconnect (and restart sending of what was supposed to be sent
by the time the problem was detected[*].

[*] You only have to when you want this to work reliably. That's
something the guy who committed hibernate considered to be 'an
optional feature', consequently, his orm framework doesn't do that by
default but nobody ever didn't get a job with RedHat just because of
publishing 'designed by a retarded, typing monkey' open source
software ...


The client application is multi-threaded; each thread makes a socket
connection and keeps it open. Client threads continuously send requests
to the server, and then wait for a pthread condition when the reply
(terminated with \n) comes in, or it time times out.

There is already a dedicated socket and a dedicated thread, yet
the dedicated client thread has to wait for yet /another/ thread to
bang the condition variable? Sounds screwy.

(Or you mean that the client API for sending a request submits the
request, and then waits for the client thread to receive the reply
and hit the condition variable?)

A problem occurred when a client actually timed-out and the data came in
right after that, so then the next client that sent a request got the
reply for the previous, and so on from that point on. (The client
supplies the read buffer.)

What you have to do is unambiguously identify the requests with a request ID
and have the replies carry a matching request ID.

The client should wait not just for any reply, but for a reply with a
particular ID.

The reply coming in with the previous ID will be discarded. Ideally the socket
thread knows that there is a new request waiting for a new ID, and can just
toss the old reply away.

Because you're using a reliable stream protocol, the ID's don't actually have
to go into the protocol. But you locally have to match requests with replies
somehow.

Suppose that ten rapid requests are made, with very short timeouts, and they
all fail with a timeout. Then another request is made. The thread should know
that this is request #11, so it can throw away the replies for the requests
1 through 10 which were sent out to the server.

I was just wondering if 1) the basic concept of leaving sockets
connected was flawed and 2) if maybe anybody out there has done anything

Timing out such that you leave the flow of replies in an ambiguous
state is flawed.
.



Relevant Pages

  • Re: [PHP] $_SERVER["REMOTE_ADDR"] returning ::1
    ... Proxies can be implemented as shared clusters such that any request going through the cluster could appear to come from one of a number of IPs (i.e. the client is not tied to a single proxy appliance). ... This is unlikely however because I believe most ISPs will do everything they can to issue a connection with the same IP when the lease expires but it's not something you can rely on. ... Request it with an IPv6 domain/IP and REMOTE_ADDR will also be IPv6. ...
    (php.general)
  • Re: Can extra processing threads help in this case?
    ... established a connection to the machine. ... THE CLIENT MACHINE! ... client request if fully received the connection is lost, ... I'm curious how you get a "verifiably valid" email ...
    (microsoft.public.vc.mfc)
  • Re: Async TCPClients
    ... especially if there will be a limited number of client connections, ... can establish a connection, so some asynchronous code is still required. ... be able to handle more than one request simealtaneously. ... o.BeginAsync immediately after o.EndAsync to start listening for another ...
    (microsoft.public.dotnet.general)
  • Re: async i/o completion routines, threading question
    ... Our code was using GetRequestStream() to post the request synchronously, ... I have both client and server logging and it's 13 seconds between ... HttpListener stuff on the server side, but 13 seconds to open a connection ...
    (microsoft.public.dotnet.framework)
  • Re: breaking the model
    ... > The forms data then is in the Request object. ... HTTP Request; in this case, the form POST Request from the Page. ... client and server. ...
    (microsoft.public.dotnet.framework.aspnet)