473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

System.Net.Sockets.Socket closing prematurly

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.

Nov 17 '05 #1
3 2785

"Tom Opgenorth" <Tom Op*******@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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.
Nov 17 '05 #2
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.Tc p,
SocketOptionName.KeepAlive, true);
_serverSocket.SetSocketOption(SocketOptionLevel.Tc p,
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
}
Nov 17 '05 #3
I cant see anything offhand by looking at the code snippet. Can you run your
app with logging turned on and try to see who/what is calling Socket.Close()
or Socket.Dispose()?

To see how to turn on logging, go to
http://blogs.msdn.com/feroze_daud/ar...12/416922.aspx
--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

"Tom Opgenorth" <To**********@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com...
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.Tc p,
SocketOptionName.KeepAlive, true);
_serverSocket.SetSocketOption(SocketOptionLevel.Tc p,
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
}

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr.Kadzija | last post by:
i have a client-server application. client and server should communicate via tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the problem is that: both client and server will send...
4
by: Sagaert Johan | last post by:
i get this error if i write this : using System.Net.Sockets; .... .... TcpClient tcp; tcp=new TcpClient();
0
by: Sagaert Johan | last post by:
this code using System; using System.Net.Sockets; namespace ConsoleApplication1 { /// <summary> /// Summary description for Class1. /// </summary>
0
by: Tom Opgenorth | last post by:
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...
2
by: Lenard Gunda | last post by:
Hi, I have the following problem when I am working with sockets in C#. I do not remember running into the same problem some while ago, when working with sockets from C++ (using native code, not...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
4
by: Funke | last post by:
Assume that in C#, I create a server socket (listener) and code to start new threads with each connection using BeginAccept(). After some time, I have three threads running, each with their own...
3
by: Daniel | last post by:
Hello, I can't seem to get my sockets code to work right. Here is what I have inside my RequestHandler handle() function: total_data= data = True...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.