System.Net.Sockets.Socket closing prematurly

G

Guest

I'm experiencing a problem with sockets, and I'm really hoping someone can
help me, please and thank you.

I've written a TCP Server, which listens on a port for an incoming
connection. When the client connects, the connection is NEVER to be closed
by the server. The client will send messages as necessary. After each
message the server is to send an acknowledgement back to the client. That is
the client's indicator to send the next message. Messages and
acknowledgements always start with a 0x0B, and end with the bytes 0x1C and
0x0D.

So, in pseudo code, what I have is this:
1. Server opens socket on port and starts listening.
2. Client connects, sends message
3. Server receives message, and writes ack back to client
4. Client sends next message to server, repeating cycle
5. Client finished with messages for now, keeps connection open.

What I'm seeing happen is this:
Step 1, 2 work fine. After Step 3, when the server sends the message to the
client, I notice that socket.connected = false. The client then blows up
trying to send the next file (no connection anymore).

Some general comments about the code:
1. I'm using sockets, and then open a NetworkStream(socket,false) to do the
writing and receiving.
2. Options I'm setting on the server socket when opening:
SocketOptioName.KeepAlive, SocketOptionName.NoDelay.
3. At no point anywhere in my server code do I explicitly call
socket.Close().
4. This is .NET 2.0 Beta 2 (C#).
5. client messages are typically ~ 1.5KB of ASCII
6. server acknowledgement message is typically < 100 bytes.
7. The buffer size for receiving is 2.5KB in size.

Any suggestions/help? Seems to be the same problems if I use the TcpClient
and TcpListener classes. I'm really stumped on this one. According to
everything I've read, the server should NOT be closing the socket when it is
finished sending it's acknowledgement message back.
 
W

Willy Denoyette [MVP]

Tom Opgenorth said:
I'm experiencing a problem with sockets, and I'm really hoping someone can
help me, please and thank you.

I've written a TCP Server, which listens on a port for an incoming
connection. When the client connects, the connection is NEVER to be
closed
by the server. The client will send messages as necessary. After each
message the server is to send an acknowledgement back to the client. That
is
the client's indicator to send the next message. Messages and
acknowledgements always start with a 0x0B, and end with the bytes 0x1C and
0x0D.

So, in pseudo code, what I have is this:
1. Server opens socket on port and starts listening.
2. Client connects, sends message
3. Server receives message, and writes ack back to client
4. Client sends next message to server, repeating cycle
5. Client finished with messages for now, keeps connection open.

What I'm seeing happen is this:
Step 1, 2 work fine. After Step 3, when the server sends the message to
the
client, I notice that socket.connected = false. The client then blows up
trying to send the next file (no connection anymore).

Some general comments about the code:
1. I'm using sockets, and then open a NetworkStream(socket,false) to do
the
writing and receiving.
2. Options I'm setting on the server socket when opening:
SocketOptioName.KeepAlive, SocketOptionName.NoDelay.
3. At no point anywhere in my server code do I explicitly call
socket.Close().
4. This is .NET 2.0 Beta 2 (C#).
5. client messages are typically ~ 1.5KB of ASCII
6. server acknowledgement message is typically < 100 bytes.
7. The buffer size for receiving is 2.5KB in size.

Any suggestions/help? Seems to be the same problems if I use the
TcpClient
and TcpListener classes. I'm really stumped on this one. According to
everything I've read, the server should NOT be closing the socket when it
is
finished sending it's acknowledgement message back.

Impossible to tell without seeing any code.

Please post a complete sample illustrating the issue.

Willy.
 
G

Guest

Here is some code that I hope shows what I'm doing. It isn't 100% what is in
my server, I've groomed it a bit, removing some logging code and
"de-factoring" a method or two. Thanks for any/all help.

_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
_serverSocket.SetSocketOption(SocketOptionLevel.Tcp,
SocketOptionName.KeepAlive, true);
_serverSocket.SetSocketOption(SocketOptionLevel.Tcp,
SocketOptionName.NoDelay, true);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, _port);
_serverSocket.Bind(endpoint);
_serverSocket.Listen(5);
byte[] incoming = new byte[2560];
for (; ; )
{
client = _serverSocket.Accept(); // Get client connection.

MemoryStream ms = null;
NetworkStream nsSend = null;
NetwrkStream nsReceive = null;
int bytesRecvd = 0;

#region Receive the message
// NOTE: This code is actually in a seperate method. Blended
// it here for sake of clarity.
if (wire.Connected)
{
nsReceive = new NetworkStream(client, FileAccess.Read, false);
if (nsReceive.CanRead)
{
ms = new MemoryStream();
do
{
bytesRecvd = nsReceive.Read(incoming, 0, incoming.Length);
ms.Write(incoming, 0, bytesRecvd);
}
while (ns.DataAvailable);
}
}
#endregion

#region Save the incoming message to disk
// Here we do the work of translating the message, and saving it to disk.
AdtTextDecoder decoder = new AdtTextDecoder();
AdtGatewayMessage agm = decoder.Decode(ms.GetBuffer());
SaveIncomingMessage(agm);
#endregion

#region Send the reply to the client
string ack = Common.GetACK(agm.MessageReference, agm.MessageType);
byte[] b = Encoding.ASCII.GetBytes(ack);
// NOTE: This code is actually in a seperate method. Blended
// it here for sake of clarity.
if (client.Connected)
{
nsSend = new NetworkStream(client, FileAccess.Write, false);
if (nsSend.CanWrite)
{
nsSend.Write(b, 0, b.Length);
// It seems that here the socket gets closed.
}
}
#endregion
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top