By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Exception:System.Net.Sockets.SocketException:

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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.

Browse more C# / C Sharp Questions on Bytes