CoderSource.net
Search
Error with TcpClient whilst using stream.read()
Started by crashed at 02-12-2006 9:39 PM. Topic has 0 replies.

Print Search « Previous Thread Next Thread »
   02-12-2006, 9:39 PM
crashed is not online. Last active: 2/13/2006 2:38:15 AM crashed

Top 75 Posts
Joined on 02-13-2006
Posts 1
Error with TcpClient whilst using 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


   Report 
Codersource.Net » Programming » C# Programming » Error with TcpClient whilst using stream.read()

MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Welcome to Codersource.Net Login | Register | Faq  

Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us