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;
}
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;
}
Comment