471,319 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Exception:System.Net.Sockets.SocketException:

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;
}
....

Sep 14 '07 #1
1 4138

On Sep 14, 12:07 pm, larspe...@faerge.dk wrote:
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.)

Sep 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by rosydwin | last post: by

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.