473,320 Members | 1,955 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,320 software developers and data experts.

TcpClient reading problem

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.
Nov 17 '05 #1
2 3430
David Dvali wrote:
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
Nov 17 '05 #2
Can you tell me how can I use unblocked sockets?

"Jianwei Sun" <js***********@gmail.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
David Dvali wrote:
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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
1
by: MuZZy | last post by:
HI, How do i get a remote TcpClient address here? // ======================================= TcpListener l = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080); TcpListener.Start(); While...
15
by: jin | last post by:
hi, i'm trying using the tcpClient to get a html file from net, instead of using WebClient or WebRequest, the main part of the source code is like this: private void tcpconnect() { tcp=new...
2
by: Kirk | last post by:
Hello, I'm trying to get the current date/time from a (S)NTP server. It works with UdpClient class, but not with TcpClient class. I want to use TcpClient because i can set timeout values for...
0
by: Torsten Brasch | last post by:
Hi All and Happy New Year ;) I have a very strange problem with System.Net.Sockets.TcpClient(). For some reason, the number of bytes I can receive is limited to 5460 bytes. I made sure that the...
2
by: Jerry Spence1 | last post by:
I'm having a problem with the following excerpt: dim tcpCli As New TcpClient Dim ns As NetworkStream = tcpCli.GetStream Dim sw As New StreamWriter(ns, System.Text.Encoding.Default) Dim sr...
2
by: DrZoop | last post by:
Long story short: I am misusing the TCPClient class. I am sending 400-500 byte messages, and data is being lost. How can the TCPClient class be misused to lose data? Background notes: -I know...
4
by: Andrew Jackson | last post by:
I am writing a newsgroup client. I have the protocol figured out. But I get slow transfer speeds off any of the network objects read the data from For example one of the commands for a news...
2
by: Zytan | last post by:
I just had the problem occur again, with NetworkStream.Write() doing its thing with a timeout... and it just sits and waits and waits and waits... it never times outs. So, I shut the server down...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.