Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i'm trying to get a server/client app to work. i want my client app to read anything the server is sending it.

 

i've put the Stream.Read method inside a timer, but it just hangs when i run it. the exception doesn't even trigger. am i going about this wrong?

// initialization
TcpClient tcpclnt = new TcpClient();         
tcpclnt.Connect("10.10.10.10',8001);
Stream stm = tcpclnt.GetStream();

.....
this.timer1.Interval = 1000;			
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();

...
private void timer1_Tick(object sender, System.EventArgs e)
{
 try
 {
   byte[] bb = new byte[100];
   int k = stm.Read(bb,0,100);

   for ( int i=0 ; i<k ; i++ )
     txtBox.Text += (Convert.ToChar(bb[i]));	
 }
 catch(Exception p)
 {
   MessageBox.Show(p.ToString());
 }
}

 

on the server side, i'm using this to send

Socket s = new Socket();
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("string"));

Posted

i've researched a little more i found that the read method will wait until it gets data or an exception is generated. 'blocking' is the term i believe.

 

the problem is that the server is not always sending data.

i want the client to update only when data is sent .. but when i call the read method, the entire program just hangs until it receives data.

 

how would i let it listen for data while the program does other things?

  • *Gurus*
Posted

I don't use the TcpClient class myself, I use the Socket class which I believe TcpClient wraps. The Socket class has asynchronous methods to go along with the synchronous ones, and to avoid blocking you must use those instead.

 

The method you want is BeginRead()

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...