Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. tcpclient stream read

tcpclient stream read

Scheduled Pinned Locked Moved C#
sysadminhelpquestionannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikemilano
    wrote on last edited by
    #1

    i'm working on a server/client app and i'm running into problems. i would like the client to update when the server sends data to it. the problem is with Stream.Read() . This method will hang the application until either data is received, or an exception is generated. is there a way to check if data exists before you call the read method? this is a snippet of the code i'm using:

    TcpClient tcpclnt = new TcpClient();         
    tcpclnt.Connect("10.10.10.10',8001);
    Stream stm = tcpclnt.GetStream();
    ....
    
    listen()
    {
      try
      {
        byte[] bb = new byte[100];
        int k = stm.Read(bb,0,100);
    
        for ( int i=0 ; i
    
    L T 3 Replies Last reply
    0
    • M mikemilano

      i'm working on a server/client app and i'm running into problems. i would like the client to update when the server sends data to it. the problem is with Stream.Read() . This method will hang the application until either data is received, or an exception is generated. is there a way to check if data exists before you call the read method? this is a snippet of the code i'm using:

      TcpClient tcpclnt = new TcpClient();         
      tcpclnt.Connect("10.10.10.10',8001);
      Stream stm = tcpclnt.GetStream();
      ....
      
      listen()
      {
        try
        {
          byte[] bb = new byte[100];
          int k = stm.Read(bb,0,100);
      
          for ( int i=0 ; i
      
      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      threads :) leppie::AllocCPArticle("Zee blog");
      Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

      1 Reply Last reply
      0
      • M mikemilano

        i'm working on a server/client app and i'm running into problems. i would like the client to update when the server sends data to it. the problem is with Stream.Read() . This method will hang the application until either data is received, or an exception is generated. is there a way to check if data exists before you call the read method? this is a snippet of the code i'm using:

        TcpClient tcpclnt = new TcpClient();         
        tcpclnt.Connect("10.10.10.10',8001);
        Stream stm = tcpclnt.GetStream();
        ....
        
        listen()
        {
          try
          {
            byte[] bb = new byte[100];
            int k = stm.Read(bb,0,100);
        
            for ( int i=0 ; i
        
        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        threads ;) leppie::AllocCPArticle("Zee blog");
        Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

        N 1 Reply Last reply
        0
        • M mikemilano

          i'm working on a server/client app and i'm running into problems. i would like the client to update when the server sends data to it. the problem is with Stream.Read() . This method will hang the application until either data is received, or an exception is generated. is there a way to check if data exists before you call the read method? this is a snippet of the code i'm using:

          TcpClient tcpclnt = new TcpClient();         
          tcpclnt.Connect("10.10.10.10',8001);
          Stream stm = tcpclnt.GetStream();
          ....
          
          listen()
          {
            try
            {
              byte[] bb = new byte[100];
              int k = stm.Read(bb,0,100);
          
              for ( int i=0 ; i
          
          T Offline
          T Offline
          TimK
          wrote on last edited by
          #4

          Run the "listener" in its own thread. This will overcome the freeze while waiting for the server to accept your connection. You may like to have a look at the NetworkStream.DataAvailable property, this property returns true if data is available on the stream to be read; otherwise, false.

          1 Reply Last reply
          0
          • L leppie

            threads ;) leppie::AllocCPArticle("Zee blog");
            Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            leppie wrote: threads You said this twice, are you silently suggesting *multi-threading*? :laugh: -Nick Parker

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups