Re: Strange server socket behaviour



The remote client will certainly close the connection if your client socket
does not. Take a look at the following 2 articles on using an asynchronous
server socket:

http://www.codeguru.com/csharp/csharp/cs_misc/sampleprograms/article.php/c7695/#more
http://www.codeguru.com/Csharp/Csharp/cs_network/sockets/article.php/c8781/

The following tutorial may also be of help:
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=709

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

"Massimo" <barone@xxxxxxxxx> wrote in message
news:OClkJJMQHHA.468@xxxxxxxxxxxxxxxxxxxxxxx
I'm facing quite a strange problem with a network server application.

This is quite a complex project, involving some embedded roaming clients
sending data to a central server using GPRS modems, and only the server
part is being developed using .NET: the clients uses C-language firmware
running on microcontrollers.

I'm not quite sure the clients' TCP/IP libraries really follow every
existing standard, so I can't be sure the problem isn't there (I'm quite
sure of the opposite, actually)... but they can connect without troubles
to any other TCP/IP network server in the world (ok, maybe this is
exagerating a bit, but you get the idea), so I think there must be some
problem in the server code.

The server is quite simple: it just sits there, waits for client
connections, accepts them and starts reading from the sockets until a
server shutdown is requested or the connection breaks. It never sends any
data because the client-server protocol is unidirectional, and it never
closes the connection, unless preliminary authentication fails or an error
is detected.

This is (roughly) the server's code:


----------
int firstpacketsize = 10;
int port = 42;
Socket serversocket = null;

void Start()
{
serversocket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

serversocket.Bind(new IPEndPoint(IPAddress.Any,port));

serversocket.Listen(10);

serversocket.BeginAccept(AcceptCallback,null);

return;
}

void AcceptCallback(IAsyncResult ar)
{
Socket clientsocket = serversocket.EndAccept(ar);

string ip = (clientsocket.RemoteEndPoint as
IPEndPoint).Address.ToString();

Console.WriteLine("Connection started from {0}",ip);

byte[] packet = new byte[firstpacketize];

Console.WriteLine("Waiting for first packet");

int r =
clientsocket.Receive(packet,Protocol.AuthPacketSize,SocketFlags.None);

// ...

// Some error checking and client authentication code

// ...

// Now a new ClientConnection object is created and starts its own
// asynchronous data reading and processing

// ...

serversocket.BeginAccept(AcceptCallback,null);

return;
}
----------


This code works perfectly when the client is a .NET program, but crashes
when a connection is made by the "real" client. A SocketException is
thrown during the first clientsocket.Receive(), complaining about the
connection being forcibly closed by the remote host. But the worse is
still to come: after this error (and the subsequent clientsocket.Close()),
serversocket.BeginAccept() too throws an exception!

The client, of course, didn't close anything... or at least didn't want
to. I think there must be some nasty bug in the client's TCP/IP libraries,
because the server works flawlessly with a .NET client... but, as I said,
the clients seem to work ok when connecting to other network servers, so
maybe the problem's here and I'm not correctly handling some error that
usually doesn't happen but sometimes do.

What can I do to understand what's really happening here?

Do you see any flaw in my server code which could account for this
behaviour upon clientsocket.Receive()?

And how can an error in clientsocket.Receive() cause another error in
serversocket.BeginAccept()?!?

I'm really puzzled here...


Massimo



.



Relevant Pages

  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #3]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... kernel socket family. ... presentation side is left to the client. ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... Make it possible for the client socket to be used to go to more than one ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #2]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... Make it possible for the client socket to be used to go to more than one ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • Re: Writing a windows service with a socket interface.
    ... It's FAR easier than direct socket ... > and sends a response back to the client. ... Or perhaps if the IP address of the server was modified ... > connection open, I would implement a sort of connection pool of course. ...
    (microsoft.public.dotnet.framework)
  • Cable modem/router causes error 10054, "Connection was forcibly reset by remote host"
    ... If there is no activity on the socket for five minutes, ... no resets are issued by our server or anything else visible on the wire. ... same to be true with an FTP client accessing an FTP site totally unrelated ... client, made five unsuccessful attempts to use its existing connection, then ...
    (microsoft.public.win32.programmer.networks)

Loading