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. 3 2788
"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.
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
}
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 } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Sagaert Johan |
last post by:
i get this error if i write this :
using System.Net.Sockets;
....
....
TcpClient tcp;
tcp=new TcpClient();
|
by: Sagaert Johan |
last post by:
this code
using System;
using System.Net.Sockets;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |