l***@faerge.dk
2007-09-14 10:07:40 UTC
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;
}
...
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;
}
...