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. Synchronous socket connections

Synchronous socket connections

Scheduled Pinned Locked Moved C#
questioncsharpcomsysadminhelp
2 Posts 2 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
    m_mond
    wrote on last edited by
    #1

    I'm trying to get a synchronous connection to a server using the various .NET stream classes. It seems that I can send a command to the server successfully, but nothing is returned (or the StreamReader returns before the data can be read off of the socket). Here's the psuedocode/real code :

    static void Main(string[] args)
    {
    string newsServerName = "msnews.microsoft.com";
    int newsServerPort = 119;

    TcpClient newsServer;

    try {
    newsServer = new TcpClient();
    newsServer.Connect( newsServerName, newsServerPort );
    }
    catch( SocketException e )
    {
    string error = "Failed to connect to " + newsServerName + " on port " + newsServerPort;
    Console.WriteLine( error );
    Console.WriteLine( e.Message );
    return;
    }

    NetworkStream netStream;
    StreamReader reader;
    StreamWriter writer;

    try {
    netStream = newsServer.GetStream();
    reader = new StreamReader( netStream );
    writer = new StreamWriter( netStream );

      // Flush the read buffer if there's anything in there from the Connect.
      if ( reader.Peek() > -1 ) {
         while ( reader.Peek() > -1 ) {
            Console.WriteLine( reader.ReadLine() );
         }
      }
    
      string sendMessage = "LIST" + "\\r\\n";
    
      Console.WriteLine( "Sending message." );
      writer.WriteLine( sendMessage );
      writer.Flush();
      Console.WriteLine( "Waiting for response." );
    
    
      while ( reader.Peek() > -1 ){
         Console.WriteLine( reader.ReadLine() );
      }
    

    }
    catch( Exception e ) {
    Console.WriteLine( "Error talking with server." );
    Console.WriteLine( e.Message );
    }
    }

    The last while loop never writes anything out. At the very least I should get the response from the server. How can I make the StreamReader wait until there is data before trying to dump out? I know this can be done with plain Send and Recv on a Socket, but I would like to use the Stream* classes if possible. greg

    Richard DeemingR 1 Reply Last reply
    0
    • M m_mond

      I'm trying to get a synchronous connection to a server using the various .NET stream classes. It seems that I can send a command to the server successfully, but nothing is returned (or the StreamReader returns before the data can be read off of the socket). Here's the psuedocode/real code :

      static void Main(string[] args)
      {
      string newsServerName = "msnews.microsoft.com";
      int newsServerPort = 119;

      TcpClient newsServer;

      try {
      newsServer = new TcpClient();
      newsServer.Connect( newsServerName, newsServerPort );
      }
      catch( SocketException e )
      {
      string error = "Failed to connect to " + newsServerName + " on port " + newsServerPort;
      Console.WriteLine( error );
      Console.WriteLine( e.Message );
      return;
      }

      NetworkStream netStream;
      StreamReader reader;
      StreamWriter writer;

      try {
      netStream = newsServer.GetStream();
      reader = new StreamReader( netStream );
      writer = new StreamWriter( netStream );

        // Flush the read buffer if there's anything in there from the Connect.
        if ( reader.Peek() > -1 ) {
           while ( reader.Peek() > -1 ) {
              Console.WriteLine( reader.ReadLine() );
           }
        }
      
        string sendMessage = "LIST" + "\\r\\n";
      
        Console.WriteLine( "Sending message." );
        writer.WriteLine( sendMessage );
        writer.Flush();
        Console.WriteLine( "Waiting for response." );
      
      
        while ( reader.Peek() > -1 ){
           Console.WriteLine( reader.ReadLine() );
        }
      

      }
      catch( Exception e ) {
      Console.WriteLine( "Error talking with server." );
      Console.WriteLine( e.Message );
      }
      }

      The last while loop never writes anything out. At the very least I should get the response from the server. How can I make the StreamReader wait until there is data before trying to dump out? I know this can be done with plain Send and Recv on a Socket, but I would like to use the Stream* classes if possible. greg

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try this:

      string line;
      while ( (line = reader.ReadLine()) != null && line != "." )
      {
      Console.WriteLine( line );
      }

      NB: .\r\n is the signal for the end of the data.

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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