473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 "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;
}
Mar 27 '08 #1
6 1805
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.

Mar 27 '08 #2
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.
Mar 27 '08 #3
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.
Mar 27 '08 #4
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

--
http://barrkel.blogspot.com/
Mar 27 '08 #5
On Thu, 27 Mar 2008 07:18:12 -0700, Chris Dunaway <du******@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
Mar 27 '08 #6
On Mar 27, 10:29 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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
Mar 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: thomasamillergoogle | last post by:
I am building a xml file to store a single TCP/IP raw packet. I need to store "special characters" on the file I am working on. When I say "special characters" i am talking about tabs,...
2
by: CK | last post by:
I am a "newbie" to python and today I had the need to write a program which generated a lot of tcp connections to a range of addresses (10.34.32.0/22) in order to troubleshoot a problem with a...
3
by: thomasamillergoogle | last post by:
I am building a web page to show a TCP/IP raw packet. I need to show "special characters" on the web page I am working on. When I say "special characters" i am talking about tabs, whitespace,...
4
by: sunil | last post by:
I am creating a XML document which opens fine in IE. Implies MSXML thinks it is a well formed document. But when I try to load this document in VB.net using the following code Dim doc As New...
1
by: Najm Hashmi | last post by:
Hi all , I am trying to create a store procedure and I get the following error: SQL0104N An unexpected token "END-OF-STATEMENT" was found following "END". Expected tokens may include: "JOIN...
3
by: user_5701 | last post by:
Hello, I'm getting an error with a Docmd.Transferspreadsheet line of code: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel2000, "tblTest", pathAndFilename, True The above line...
2
by: Matt | last post by:
I wrote the tcp socket client-server program that the server will echo the message received from the client. In client program: char sendBuf; while(1) { cout << "Enter message:";...
10
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
3
by: Alan | last post by:
Dear All, I'm trying to write a C++ that reads in hexadecimal characters from a text file. For this I use the >> to read in a character at a time inside a while loop that waits for eof to be...
19
by: roh | last post by:
Hi Folks, We have e-Biz application running on WebSphere Application Server Ver 6.0 using DB2 UDB Ver 8.1 . We are facing following issue in our application , any help from your side will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.