socket programming in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sebastian.harko@gmail.com

    #1

    socket programming in C#

    Hello,
    I have a question on socket programming in C#.
    I have this code and it doesn't work correctly.

    It sends the message correctly but it does not get the message that
    the server sends back ( the program bellow gets blocked ). I know for
    sure
    that the server sends back a message ( I'm using a traffic sniffer and
    I can
    see what's being sent and received ) .'
    ....
    So what's wrong here ?

    best regards


    TcpClient client = new TcpClient(hostN ame, pacsPort);

    try
    {
    NetworkStream s = client.GetStrea m();
    StreamReader sr = new StreamReader(s) ;
    StreamWriter sw = new StreamWriter(s) ;
    sw.AutoFlush = true;
    sw.Write(messag e);
    string response = sr.ReadLine(); // program blocks
    here
    Console.Write(r esponse);
    sw.Close();
    sr.Close();
    s.Close();
    client.Close();
    }
    catch (Exception e)
    {
    Console.WriteLi ne("Error.");
    }

  • Jon Skeet [C# MVP]

    #2
    Re: socket programming in C#

    On Jul 25, 2:56 pm, sebastian.ha... @gmail.com wrote:
    Hello,
    I have a question on socket programming in C#.
    I have this code and it doesn't work correctly.
    >
    It sends the message correctly but it does not get the message that
    the server sends back ( the program bellow gets blocked ). I know for
    sure
    that the server sends back a message ( I'm using a traffic sniffer and
    I can
    see what's being sent and received ) .'
    ...
    So what's wrong here ?
    Does the server include a carriage return or line feed?

    Jon

    Comment

    • sebastian.harko@gmail.com

      #3
      Re: socket programming in C#

      I don't know ... How could I check and what could I do about it ?

      >
      Does the server include a carriage return or line feed?
      >
      Jon

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: socket programming in C#

        Hi,

        <sebastian.hark o@gmail.comwrot e in message
        news:1185372871 .205302.237680@ 19g2000hsx.goog legroups.com...
        >I don't know ... How could I check and what could I do about it ?
        The problem is that you are decorating the NetworkStream with a
        StreamReader, ReadLine() blocks and keep reading until an
        Environment.New Line string is received, if it's never received it will be
        blocked for ever.

        Do this, read one byte at a time and dump it in a StringBuilder or some
        similar structure. Also you must know if the source is sending UNICODE (2
        bytes per char) or ASCII (1 byte per char).


        Comment

        • Peter Duniho

          #5
          Re: socket programming in C#

          On Wed, 25 Jul 2007 11:10:18 -0700, Ignacio Machin ( .NET/ C# MVP )
          <machin TA <"laceupsolutio ns.com>"wrote:
          Do this, read one byte at a time and dump it in a StringBuilder or some
          similar structure. [...]
          For testing purposes, this might be appropriate. Otherwise, no...don't do
          this.

          It is unclear from the original post exactly what the data is to look
          like. If it _will_ include a CR/LF to terminate a line, then using
          ReadLine() isn't all that bad. If the format of the data is unknown,
          other than knowing for sure that it's some kind of character format, then
          using the Read() method would be better. You will have to loop, as Read()
          will return each time there's _some_ data that's been received.

          This all assumes that the character encoding for the stream is the same on
          both ends, of course, as Ignacio points out.

          Pete

          Comment

          • leon.friesema@frost-nospam-bits.nl

            #6
            Re: socket programming in C#

            sebastian.harko @gmail.com wrote on 7/26/2007 1:46:47 AM
            >Hello,
            >I have a question on socket programming in C#.
            >I have this code and it doesn't work correctly.
            >It sends the message correctly but it does not get the message that
            >the server sends back ( the program bellow gets blocked ). I know for
            >sure
            >that the server sends back a message ( I'm using a traffic sniffer and
            >I can
            >see what's being sent and received ) .'
            >....
            >So what's wrong here ?
            >best regards
            TcpClient client = new TcpClient(hostN ame, pacsPort);
            try
            {
            NetworkStream s = client.GetStrea m();
            StreamReader sr = new StreamReader(s) ;
            StreamWriter sw = new StreamWriter(s) ;
            sw.AutoFlush = true;
            sw.Write(messag e);
            string response = sr.ReadLine(); // program blocks
            >here
            Console.Write(r esponse);
            sw.Close();
            sr.Close();
            s.Close();
            client.Close();
            }
            catch (Exception e)
            {
            Console.WriteLi ne("Error.");
            }
            ><hr>
            You want to connect to the client before you attach the streamreader and -writer. Besides that it is good practice to set the encoding of the streamreader to ASCII.

            Leon

            Comment

            Working...