Reputation: 2584
I'm using the .NET Socket class.
Basically my program is to send XML commands via TCP to my server (running some 3rd party services), and it will send back an XML response.
What I wanted to do is to send and receive multiple times without closing the socket, because the server seems to slow down if I open and close the socket too many times in a short period of time.
Stripped down version of my code:
//Connects to server
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint("123.123.123.123", 8199);
socket.Connect(ipe);
//Send my message
int sent = sock.Send(buffer, buffer.Length, SocketFlags.None); //buffer contains my XML data
//Receives respond from server
byte[] buf = new byte[65536];
int ret = socket.Receive(buffer, 0, 65536, SocketFlags.None);
//Send my message again without closing the socket
sent = sock.Send(buffer, buffer.Length, SocketFlags.None); //buffer contains my XML data
//Receives respond from server failed..
ret = socket.Receive(buffer, 0, 65536, SocketFlags.None);
It send/receive works the first time, but not the second time. The second time it throws an exception "An existing connection was forcibly closed by the remote host". What do I need to do before I can issue another send/receive?
Thank you so much for reading.
Upvotes: 1
Views: 4129
Reputation: 4663
The problem might be these
The server might have closed the connection since you might have a send an invalid packet. check whether the data you are sending is valid. Debug or have a tcp/ip packet montior in the server side and examine the packet you receive.
Upvotes: 1