0
Reply

Error with tcpclient and stream.read() urgent help plz!!!!

Chris

Chris

Feb 13 2006 12:45 AM
2.1k

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