473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 7338
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.