Exception:System.Net.Sockets.SocketException:

L

larspeter

Hi all.

I have a problem with TcpClient ... I am conneting to a server with
TcpClient and returning the answer through a webservice.

It actully all works fine. BUT if I make a lot of (re)connection
(hitting the submit button) then I start to recieve a an error:

IOException:System.IO.IOException: Der kunne ikke læses data fra
transportforbindelsen (LPF: could not read data from the transport
connection): An existing connection was forcibly closed by the remote
host ---> System.Net.Sockets.SocketException: En eksisterende
forbindelse blev tvangsafbrudt af en ekstern vært ved
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags) ved
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size) --- Slut på staksporing af indre undtagelser --- ved
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size) ved SocketApplication.TrafikSocket.Connect(String server,
Int32 port, String message) i c:\www\WebSocket\App_Code
\TrafikSocket.cs:linje 63


It starts failing in the line: Int32 bytes = stream.Read(data, 0,
data.Length); The program that I am sending the reqeust to has to be
restartet before the system answers again. Nothing in that program is
hanging and the Port is listining...

Any one has any idea where to debug that problem?! Currently I have no
idea :-(


public string Connect(String server, Int32 port, String
message)
{

// variablerne er benyttes
TcpClient client = null;
NetworkStream stream = null;

try
{

client = new TcpClient(server, port);

// Translate the passed message into ASCII and store
it as a Byte array.
Byte[] data =
System.Text.Encoding.UTF8.GetBytes(message);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
stream = client.GetStream();

// String to store the response UTF7 representation.
String responseData = String.Empty;

// Send the message to the connected TcpServer.
if (stream.CanWrite)
{
stream.Write(data, 0, data.Length);

// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];


// Read the first batch of the TcpServer response
bytes.
if (stream.CanRead)
{
Int32 bytes = stream.Read(data, 0,
data.Length);
responseData =
System.Text.Encoding.UTF7.GetString(data, 0, bytes);
}
}

// Close everything.
stream.Close();
client.Close();

return responseData;
}
....
 
U

UL-Tomten

The program that I am sending the reqeust to has to be restartet before
the system answers again.

This suggests the problem is in the server, and not your client. I
assume restarting the client doesn't change anything?

(Disclaimer: I didn't look at your code.)
 

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