P
Peter Pippinger
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 "UNBEKANNTER PROZESS".
Sometimes i get for example "#UNBEKANNTER 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">ASCII-Zeichen</param>
/// <returns>ASCII-Wert</returns>
///
***************************************************************************
*****************************
public static byte Asc(char src)
{
// ASCII Wert eines Zeichens zurückgeben
return (System.Text.ASCIIEncoding.ASCII.GetBytes(src +
"")
[0]);
}
///
***************************************************************************
*****************************
/// <summary>
/// Gibt ein ASCII-Zeichen für einen Wert zurück
/// </summary>
/// <param name="src">ASCII-Wert</param>
/// <returns>ASCII-Zeichen</returns>
///
***************************************************************************
*****************************
public static char Chr(byte src)
{
// Zeichen zu einem ASCII Wert zurückgeben
return (System.Text.ASCIIEncoding.ASCII.GetChars(new
byte[] { src })[0]);
}
///
***************************************************************************
*****************************
/// <summary>
/// Sendet einen String über einen Socket
/// </summary>
/// <param name="socket">Socket</param>
/// <param name="stringData">String</param>
///
***************************************************************************
*****************************
public void write_string_on_socket(Socket socket, String
stringData)
{
// Länge des Strings festlegen, die übertragen wird
stringData = Chr((byte)stringData.Length).ToString() +
stringData;
// Nachricht an Server senden
socket.Send(Encoding.ASCII.GetBytes(stringData));
}
///
***************************************************************************
*****************************
/// <summary>
/// Liest einen Sting über einen Socket
/// </summary>
/// <param name="socket">Socket</param>
/// <returns>String</returns>
///
***************************************************************************
*****************************
public string read_string_from_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 "UNBEKANNTER PROZESS".
Sometimes i get for example "#UNBEKANNTER 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">ASCII-Zeichen</param>
/// <returns>ASCII-Wert</returns>
///
***************************************************************************
*****************************
public static byte Asc(char src)
{
// ASCII Wert eines Zeichens zurückgeben
return (System.Text.ASCIIEncoding.ASCII.GetBytes(src +
"")
[0]);
}
///
***************************************************************************
*****************************
/// <summary>
/// Gibt ein ASCII-Zeichen für einen Wert zurück
/// </summary>
/// <param name="src">ASCII-Wert</param>
/// <returns>ASCII-Zeichen</returns>
///
***************************************************************************
*****************************
public static char Chr(byte src)
{
// Zeichen zu einem ASCII Wert zurückgeben
return (System.Text.ASCIIEncoding.ASCII.GetChars(new
byte[] { src })[0]);
}
///
***************************************************************************
*****************************
/// <summary>
/// Sendet einen String über einen Socket
/// </summary>
/// <param name="socket">Socket</param>
/// <param name="stringData">String</param>
///
***************************************************************************
*****************************
public void write_string_on_socket(Socket socket, String
stringData)
{
// Länge des Strings festlegen, die übertragen wird
stringData = Chr((byte)stringData.Length).ToString() +
stringData;
// Nachricht an Server senden
socket.Send(Encoding.ASCII.GetBytes(stringData));
}
///
***************************************************************************
*****************************
/// <summary>
/// Liest einen Sting über einen Socket
/// </summary>
/// <param name="socket">Socket</param>
/// <returns>String</returns>
///
***************************************************************************
*****************************
public string read_string_from_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;
}