473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TcpClient reading problem

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

NetworkStream networkStream = cl.GetStream();

if (!networkStream .CanRead)
return;
byte[] myReadBuffer = new byte[1024];
String myCompleteMessa ge = "";
int numberOfBytesRe ad = 0;

do
{
numberOfBytesRe ad = networkStream.R ead(myReadBuffe r, 0,
myReadBuffer.Le ngth);
myCompleteMessa ge = String.Concat(m yCompleteMessag e,
Encoding.ASCII. GetString(myRea dBuffer, 0, numberOfBytesRe ad));
}
while(networkSt ream.DataAvaila ble);

cl.Close();
----------------------------------------------------------------------------------------
When it executes lone:
numberOfBytesRe ad = networkStream.R ead(myReadBuffe r, 0,
myReadBuffer.Le ngth);
The applications just heng up:(
What I'm doing wrong?

Thank you.
Nov 17 '05 #1
2 3470
David Dvali wrote:
Hello.
I wnat to read some data from server, I'm using following code:
----------------------------------------------------------------------------------------
TcpClient cl = new TcpClient();
cl.Connect("Som eHost", 9000);

NetworkStream networkStream = cl.GetStream();

if (!networkStream .CanRead)
return;
byte[] myReadBuffer = new byte[1024];
String myCompleteMessa ge = "";
int numberOfBytesRe ad = 0;

do
{
numberOfBytesRe ad = networkStream.R ead(myReadBuffe r, 0,
myReadBuffer.Le ngth);
myCompleteMessa ge = String.Concat(m yCompleteMessag e,
Encoding.ASCII. GetString(myRea dBuffer, 0, numberOfBytesRe ad));
}
while(networkSt ream.DataAvaila ble);

cl.Close();
----------------------------------------------------------------------------------------
When it executes lone:
numberOfBytesRe ad = networkStream.R ead(myReadBuffe r, 0,
myReadBuffer.Le ngth);
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******** ******@TK2MSFTN GP09.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("Som eHost", 9000);

NetworkStream networkStream = cl.GetStream();

if (!networkStream .CanRead)
return;
byte[] myReadBuffer = new byte[1024];
String myCompleteMessa ge = "";
int numberOfBytesRe ad = 0;

do
{
numberOfBytesRe ad = networkStream.R ead(myReadBuffe r, 0,
myReadBuffer.Le ngth);
myCompleteMessa ge = String.Concat(m yCompleteMessag e,
Encoding.ASCII. GetString(myRea dBuffer, 0, numberOfBytesRe ad));
}
while(networkSt ream.DataAvaila ble);

cl.Close();
----------------------------------------------------------------------------------------
When it executes lone:
numberOfBytesRe ad = networkStream.R ead(myReadBuffe r, 0,
myReadBuffer.Le ngth);
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
3034
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 and arrives at the server but from after that i only a couple of KB from the start of the file which cases the picture to display only the very top...
1
2702
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 (true) {
15
12962
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 TcpClient("www.yahoo.com",80); tcp.NoDelay=false; tcp.ReceiveTimeout=60000;
2
3395
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 reading/writing operations. The connect method generate a SocketException (10060 : timeout error) : try {
0
1955
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 server really sends everything I expect it to send. I am actually sending XML messages and this limit is a pain in the ... Here is the code:...
2
1364
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 As New StreamReader(ns)
2
2271
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 the TCP protocol doesn't lose data. Don't tell me that. I am educated. -I am buffering all data being received before checking for messages and...
4
7572
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 client to use is "XOVER articlenumber-" This return string after string of all the news articles from article number on.... Another newsclient, i...
2
4517
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 just to see if THAT will make it at least end the function call and continue (since it's a synchronous call, so the program is delayed until it...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.