TcpClient reading problem

D

David Dvali

Hello.
I wnat to read some data from server, I'm using following code:
----------------------------------------------------------------------------------------
TcpClient cl = new TcpClient();
cl.Connect("SomeHost", 9000);

NetworkStream networkStream = cl.GetStream();

if (!networkStream.CanRead)
return;
byte[] myReadBuffer = new byte[1024];
String myCompleteMessage = "";
int numberOfBytesRead = 0;

do
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0,
myReadBuffer.Length);
myCompleteMessage = String.Concat(myCompleteMessage,
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while(networkStream.DataAvailable);

cl.Close();
----------------------------------------------------------------------------------------
When it executes lone:
numberOfBytesRead = networkStream.Read(myReadBuffer, 0,
myReadBuffer.Length);
The applications just heng up:(
What I'm doing wrong?

Thank you.
 
J

Jianwei Sun

David said:
Hello.
I wnat to read some data from server, I'm using following code:
----------------------------------------------------------------------------------------
TcpClient cl = new TcpClient();
cl.Connect("SomeHost", 9000);

NetworkStream networkStream = cl.GetStream();

if (!networkStream.CanRead)
return;
byte[] myReadBuffer = new byte[1024];
String myCompleteMessage = "";
int numberOfBytesRead = 0;

do
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0,
myReadBuffer.Length);
myCompleteMessage = String.Concat(myCompleteMessage,
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while(networkStream.DataAvailable);

cl.Close();
----------------------------------------------------------------------------------------
When it executes lone:
numberOfBytesRead = networkStream.Read(myReadBuffer, 0,
myReadBuffer.Length);
The applications just heng up:(
What I'm doing wrong?

Thank you.
Because it's trying to read something which is not available.

TCPClinet has a property called ReceiveTimeout which Gets or sets the
amount of time a TcpClient will wait to receive data once a read
operation is initiated.

HTH
 
D

David Dvali

Can you tell me how can I use unblocked sockets?

Jianwei Sun said:
David said:
Hello.
I wnat to read some data from server, I'm using following code:
----------------------------------------------------------------------------------------
TcpClient cl = new TcpClient();
cl.Connect("SomeHost", 9000);

NetworkStream networkStream = cl.GetStream();

if (!networkStream.CanRead)
return;
byte[] myReadBuffer = new byte[1024];
String myCompleteMessage = "";
int numberOfBytesRead = 0;

do
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0,
myReadBuffer.Length);
myCompleteMessage = String.Concat(myCompleteMessage,
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while(networkStream.DataAvailable);

cl.Close();
----------------------------------------------------------------------------------------
When it executes lone:
numberOfBytesRead = networkStream.Read(myReadBuffer, 0,
myReadBuffer.Length);
The applications just heng up:(
What I'm doing wrong?

Thank you.
Because it's trying to read something which is not available.

TCPClinet has a property called ReceiveTimeout which Gets or sets the
amount of time a TcpClient will wait to receive data once a read operation
is initiated.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top