unexpected characters in TCP-communication

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Pippinger

    unexpected characters in TCP-communication

    Hello NG,

    i have 2 functions to read and write strings on a TCP-socket.
    The strange thing is, that sometimes there are unexpected characters
    in the string.

    First of all, have i understood it right,
    - that i say in the first byte of the string - i want to send - the
    number of chars, that i will send?
    - that the fist byte of a string - i recieve - declares the number of
    chars, i´ll get?

    One example of my problem:
    I expect one string "UNBEKANNTE R PROZESS".
    Sometimes i get for example "#UNBEKANNT ER PROZESS".
    Which gets me into trouble when working with this string...

    Does someone have an idea, what goes wrong with this?

    *** There is no cleaning-personal in the server-room which could
    stumble over cables ;-) ***

    The functions i use:

    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    /// <summary>
    /// Gibt den Wert eines ASCII-Zeichens zurück
    /// </summary>
    /// <param name="src">ASCI I-Zeichen</param>
    /// <returns>ASCI I-Wert</returns>
    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    public static byte Asc(char src)
    {
    // ASCII Wert eines Zeichens zurückgeben
    return (System.Text.AS CIIEncoding.ASC II.GetBytes(src +
    "")
    [0]);
    }


    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    /// <summary>
    /// Gibt ein ASCII-Zeichen für einen Wert zurück
    /// </summary>
    /// <param name="src">ASCI I-Wert</param>
    /// <returns>ASCI I-Zeichen</returns>
    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    public static char Chr(byte src)
    {
    // Zeichen zu einem ASCII Wert zurückgeben
    return (System.Text.AS CIIEncoding.ASC II.GetChars(new
    byte[] { src })[0]);
    }


    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    /// <summary>
    /// Sendet einen String über einen Socket
    /// </summary>
    /// <param name="socket">S ocket</param>
    /// <param name="stringDat a">String</param>
    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    public void write_string_on _socket(Socket socket, String
    stringData)
    {
    // Länge des Strings festlegen, die übertragen wird
    stringData = Chr((byte)strin gData.Length).T oString() +
    stringData;


    // Nachricht an Server senden
    socket.Send(Enc oding.ASCII.Get Bytes(stringDat a));
    }


    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    /// <summary>
    /// Liest einen Sting über einen Socket
    /// </summary>
    /// <param name="socket">S ocket</param>
    /// <returns>String </returns>
    ///
    *************** *************** *************** *************** *************** ­
    *************** **************
    public string read_string_fro m_socket(Socket socket)
    {
    // Empfangspuffer
    byte[] data = new byte[1024];


    // ermitteln, wieviele Bytes uns der Server schicken
    möchte
    int recv = socket.Receive( data);
    int byte_anzahl = (int)data[0];


    // jetzt den eigentlichen String mit der vorher
    ermittelten Länge holen
    recv = socket.Receive( data);
    string stringData = Encoding.ASCII. GetString(data, 0,
    byte_anzahl);


    // empfangene Nachricht zurückgeben
    return stringData;
    }


  • Chris Dunaway

    #2
    Re: unexpected characters in TCP-communication

    On Mar 27, 8:58 am, Peter Pippinger <peter.pippin.. .@gmx.dewrote:
    Hello NG,
    >
    >
    First of all, have i understood it right,
    - that i say in the first byte of the string - i want to send - the
    number of chars, that i will send?
    - that the fist byte of a string - i recieve - declares the number of
    chars, i´ll get?
    No, I don't believe this is correct. When sending an array of bytes,
    the entire array is sent so there is no need to specify the number of
    bytes to send in the array.

    When receiving, the return value from the Receive command indicates
    the number of bytes received.

    Comment

    • Peter Pippinger

      #3
      Re: unexpected characters in TCP-communication

      No, I don't believe this is correct.  When sending an array of bytes,
      the entire array is sent so there is no need to specify the number of
      bytes to send in the array.
      >
      When receiving, the return value from the Receive command indicates
      the number of bytes received.
      But if i connect my service for example with telnet the first "HELLO"
      of the handshake only appears in telnet, if i my fist byte of the
      sended string has the Value 5 (5 + "HELLO"). If the first Byte of the
      Sting is 4 then only "HELL" will apear in telnet...

      So i think its right, first to send how many characters there will be
      send.

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: unexpected characters in TCP-communication

        On Mar 27, 9:58 am, Peter Pippinger <peter.pippin.. .@gmx.dewrote:
        Hello NG,
        >
        i have 2 functions to read and write strings on a TCP-socket.
        The strange thing is, that sometimes there are unexpected characters
        in the string.
        Most probably cause of the encoding.
        First of all, have i understood it right,
        - that i say in the first byte of the string - i want to send - the
        number of chars, that i will send?
        Yes, that is ok, I would use Int32 instead of byte but that's ok
        - that the fist byte of a string - i recieve - declares the number of
        chars, i´ll get?
        Humm, I think that both cases are the same no?
        If you are sending a variable amount of data you should send
        previously the number of bytes you are going to send next.

        Comment

        • Barry Kelly

          #5
          Re: unexpected characters in TCP-communication

          Barry Kelly wrote:

          Silly bug:
          ---8<---
          int length = chars.Length 255 ? 255 : 0;
          int length = chars.Length 255 ? 255 : chars.Length;
          int resultLen;
          --->8---
          -- Barry

          --

          Comment

          • Peter Duniho

            #6
            Re: unexpected characters in TCP-communication

            On Thu, 27 Mar 2008 07:18:12 -0700, Chris Dunaway <dunawayc@gmail .com>
            wrote:
            >First of all, have i understood it right,
            >- that i say in the first byte of the string - i want to send - the
            >number of chars, that i will send?
            >- that the fist byte of a string - i recieve - declares the number of
            >chars, i´ll get?
            >
            No, I don't believe this is correct. When sending an array of bytes,
            the entire array is sent so there is no need to specify the number of
            bytes to send in the array.
            The Socket.Send() method only sends the bytes you ask it to. It sends no
            other meta-data, such as the length of the transmission, and thus the
            receiver obviously receives no such meta-data that it could use to
            reconstruct the array.
            When receiving, the return value from the Receive command indicates
            the number of bytes received.
            It does indicate the number of bytes received. But that doesn't tell you
            how many bytes were sent.

            Pete

            Comment

            • Chris Dunaway

              #7
              Re: unexpected characters in TCP-communication

              On Mar 27, 10:29 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
              wrote:
              On Thu, 27 Mar 2008 07:18:12 -0700, Chris Dunaway <dunaw...@gmail .com>
              wrote:
              >
              First of all, have i understood it right,
              - that i say in the first byte of the string - i want to send - the
              number of chars, that i will send?
              - that the fist byte of a string - i recieve - declares the number of
              chars, i´ll get?
              >
              No, I don't believe this is correct. When sending an array of bytes,
              the entire array is sent so there is no need to specify the number of
              bytes to send in the array.
              >
              The Socket.Send() method only sends the bytes you ask it to. It sends no
              other meta-data, such as the length of the transmission, and thus the
              receiver obviously receives no such meta-data that it could use to
              reconstruct the array.
              >
              When receiving, the return value from the Receive command indicates
              the number of bytes received.
              >
              It does indicate the number of bytes received. But that doesn't tell you
              how many bytes were sent.
              >
              Pete
              Thanks,

              I was just going by the docs for Send and Receive. I didn't realize
              that a Pascal string was being sent (as alluded by Barry in another
              response) where the string is preceded by the length.

              Cheers,

              Chris

              Comment

              Working...