472,993 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 7257
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
7
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a...
3
by: Alessandro | last post by:
Hi all! I'm trying to write a file transfer application using TCP, but I'm having some problem to connect the client and the server. Sniffing with Ethereal I can see the packets sent from the...
0
by: Glich via DotNetMonster.com | last post by:
#using <mscorlib.dll> #using <System.dll> #include "stdlib.h" #include "stdio.h" using namespace System; using namespace System::Text; using namespace System::IO; using namespace...
1
by: Chin Fui | last post by:
I am now doing my final year project using VB.NET. The project is about implement a multiplayer network game. But now I am stuck in the connection part, no idea in how to start to write the network...
0
by: Al Wilkerson | last post by:
Hey, Has anyone ever got a "Unable to read data from transport connected" message after reading data from a streamreader composed of a networkstream. For example: Server TcpListener...
1
by: Terrance | last post by:
I'm trying to create a small messenger program that uses the tcpclient and tcplistenter objects. When I start the application and run the thread that fires the tcplistener; once the client sends...
5
by: Nobody | last post by:
Hi all, I try to write a small client that can handle some TCP communication message. I was wondering how I could use the TcpClient class to manage it. At the first time, I don't want to...
0
by: id10t error | last post by:
Hello, I am trying to write soem code that will send data to an Oneil printer over our network. I know the printer works and is setup correctly because I can print to it from there software. In...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.