471,350 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Error using TcpClient with Stream.Read()

Hi,

I am using C# in .NET 2.0 and im trying to read a stream from a socket. The
code works on the first attempt but fails on subsequent attempts. It is in a
multithreaded application. It seems the PlayerStream.Read() sets the
PlayerSocket.Connected=false???

I am getting the following error:
System.IO.IOException: Unable to read data from the transport connection: A
connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond. ---> System.Net.Sockets.SocketException:
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
at GameServer.PlayerHandler.Process() in C:\Documents and Settings\
Administrator\My Documents\Visual Studio 2005\Projects\RPNServer2\LobbyServer\
GameServer\serverData.cs:line 394

The code i am using to set up the thread and socket is as follows

//Code that listens for and accepts connection, if handler!=null it then
passes a reference through to the class that runs the thread
TcpClient handler = playerListener.AcceptTcpClient();

Thread.Sleep(200);

//Code that gets called in thread class
this.PlayerSocket = client;

this.thisServer = server;

PlayerStream = PlayerSocket.GetStream();

PlayerStream.ReadTimeout = 250;

bytes = new byte[PlayerSocket.ReceiveBufferSize];

The code i am using to try and read the stream is as follows:

try
{

Trace.WriteLine(DateTime.Now.ToString() + ", playerHandler.Process(), INFO,
Attempting to check for recieved data");

PlayerSocket.Client.Blocking = true;
int BytesRead = PlayerStream.Read(bytes, 0, (int)bytes.Length);
PlayerSocket.Client.Blocking = true;
if (BytesRead > 0) {
Trace.WriteLine(DateTime.Now.ToString() + ", playerHandler.Process(), INFO,
Data has been recieved: " + bytes.ToString());
sb.Append(Encoding.ASCII.GetString(bytes, 0, BytesRead));
}

else

if (sb.Length > 0)
{
ProcessRecievedData();
Trace.WriteLine(DateTime.Now.ToString() + ", playerHandler.Process(), CALL,
ProcessRecievedData()");
}
}

catch (IOException e)
{

Trace.WriteLine(DateTime.Now.ToString() + ", playerHandler.Process(), ERROR,
IOException occured attempting to get data: " +e.ToString());
}

I have been attempting to fix this myself for the past few days but have had
no luck, maybe one of you guys will have some idea.

Rgds,

Chris
Feb 13 '06 #1
1 6811
crashed wrote:
I am using C# in .NET 2.0 and im trying to read a stream from a socket. The
code works on the first attempt but fails on subsequent attempts. It is in a
multithreaded application. It seems the PlayerStream.Read() sets the
PlayerSocket.Connected=false???


It sounds like the other side of the connection has closed it,
possibly. On the other hand, if it's multithreaded and you're trying to
use the socket from multiple threads simultaneously, that could be the
issue.

I suggest you try to produce a short but complete program that
demonstrates the problem, and post it here. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Feb 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Grant Richard | last post: by
3 posts views Thread by Alessandro | last post: by
reply views Thread by Glich via DotNetMonster.com | last post: by
5 posts views Thread by Nobody | last post: by
reply views Thread by id10t error | 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.