Packet Receiving...

09/08/2012 00:50 badguy4you#1
I read this on the internet
Quote:
The Problem

One of the most common beginner mistakes for people designing protocols for TCP/IP is that they assume that message boundaries are preserved. For example, they assume a single "Send" will result in a single "Receive".

Some TCP/IP documentation is partially to blame. Many people read about how TCP/IP preserves packets - splitting them up when necessary and re-ordering and re-assembling them on the receiving side. This is perfectly true; however, a single "Send" does not send a single packet.

Local machine (loopback) testing confirms this misunderstanding, because usually when client and server are on the same machine they communicate quickly enough that single "sends" do in fact correspond to single "receives". Unfortunately, this is only a coincidence.

This problem usually manifests itself when attempting to deploy a solution to the Internet (increasing latency between client and server) or when trying to send larger amounts of data (requiring fragmentation). Unfortunately, at this point, the project is usually in its final stages, and sometimes the application protocol has even been published!

True story: I once worked for a company that developed custom client/server software. The original communications code had made this common mistake. However, they were all on dedicated networks with high-end hardware, so the underlying problem only happened very rarely. When it did, the operators would just chalk it up to "that buggy Windows OS" or "another network glitch" and reboot. One of my tasks at this company was to change the communication to include a lot more information; of course, this caused the problem to manifest regularly, and the entire application protocol had to be changed to fix it. The truly amazing thing is that this software had been used in countless 24x7 automation systems for 20 years; it was fundamentally broken and no one noticed.
So my problem is, I am developing a client server application, and i wonder how to prevent packet receiving problems [such the one above]

Here is the way i send a message to the server from my simple client

[CLIENT]
Code:
        TcpClient Client = new TcpClient();
        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000);

        private void btnSend_Click(object sender, EventArgs e)
        {
            NetworkStream clientStream = Client.GetStream();

            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(textMessage.Text);

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }
Assuming that i want to send login data so i decided that my message will look like this
Code:
string msg = "1001:UN=badguy4you&&PW=5001"; //1001 is the id
[SERVER]
Code:
        private void OnReceivePacket(byte[] buffer, IE Client)
        {
            try
            {
                int ID;
                string V = Encoding.ASCII.GetString(buffer).Split(':')[0];
                int.TryParse(V, out ID);

                switch (ID)
                {
                    case 1001:
                        Append("Login Packet Received");
                        break;

                    case 1002:
                        //Other IDs
                        break;

                    default:
                        break;
                }
            }
            catch { }         
        }
So that is i am doing\handling the sent and received messages between my Client and Server. Can you tell me am i doing this in the right way or there is something i am doing wrong ?!

Also What is the difference between Encoding.ASCII and Encoding.UTF8 ?
09/09/2012 03:23 Danial Eugen#2
There is no problem with the way you send receive the messages as i see in your code

And about the Encoding use UTF8 if you are going to use unicode characters
09/09/2012 03:57 EpicNinjaKick#3
No point in commenting - hes banned.
UTF - 8 Unicode
UTF general ci
09/17/2012 01:17 badguy4you#4
Quote:
Originally Posted by EpicNinjaKick View Post
No point in commenting - hes banned.
UTF - 8 Unicode
UTF general ci
So i am not banned [it was 2 weeks due to 3 infractions] and i still need help
09/17/2012 01:32 I don't have a username#5
Take a look at the socket tutorial I've posted (Link in my signature).

Also maybe take a look at the source of Buu (My packet analyzer / proxy).
09/18/2012 19:52 badguy4you#6
Quote:
Originally Posted by I don't have a username View Post
Take a look at the socket tutorial I've posted (Link in my signature).

Also maybe take a look at the source of Buu (My packet analyzer / proxy).
So is the way i am sending and receiving the message at my topic wrong ? also i wonder when you mention a packet it is going to be something like

A0 E7 T3 E4 E6 ....

When i mention a packet i think it is

Encoding.ASCII.GetBytes("ID:MESSAGE");
THEN
Encoding.ASCII.GetString(buffer).Spilit(':')[0]; //ID
Encoding.ASCII.GetString(buffer).Spilit(':')[1]; //Message

i am totally confused how could i send data from my client and then determine which thing i have received on the server

Like
Code:
if(packet.id == 5001) //Do some stuff;
09/18/2012 20:36 I don't have a username#7
Quote:
Originally Posted by badguy4you View Post
So is the way i am sending and receiving the message at my topic wrong ? also i wonder when you mention a packet it is going to be something like

A0 E7 T3 E4 E6 ....

When i mention a packet i think it is

Encoding.ASCII.GetBytes("ID:MESSAGE");
THEN
Encoding.ASCII.GetString(buffer).Spilit(':')[0]; //ID
Encoding.ASCII.GetString(buffer).Spilit(':')[1]; //Message

i am totally confused how could i send data from my client and then determine which thing i have received on the server

Like
Code:
if(packet.id == 5001) //Do some stuff;
Quote:
Originally Posted by badguy4you
Quote:
Originally Posted by I don't have a username View Post
Take a look at the socket tutorial I've posted (Link in my signature).

Also maybe take a look at the source of Buu (My packet analyzer / proxy).
So is the way i am sending and receiving the message at my topic wrong ? also i wonder when you mention a packet is is going to be something like

A0 E7 T3 E4 E6 ....

When i mention a packet i think it is

Encoding.ASCII.GetString(buffer);

i am totally confused how could i send data from my client and then determine which thing i have received on the server

Like
Code:
if(packet.id == 5001) //Do some staff;
Please i need your help with that because i completely lost :(

:handsdown:
I don't really get it... The first thing:
Code:
A0 E7 T3 E4 E6
That's a packet dump. Each value you see there is the bytes in your packet, but converted to hex.

This:
Code:
Encoding.ASCII.GetString(buffer);
is converting the bytes to a string. It's equal to:
Code:
string s = "";
foreach (byte b in buffer)
s += (char)b;
^ StringBuilder would be better here thought, but this is just for you to understand.

To get this:
Code:
if(packet.id == 5001)
You'd have to read the actual values in the packet ex. for conquer the id is an ushort.
An ushort is 2 bytes.

The first 4 bytes is 2 ushorts which is size and id in CO.

That would make it like this:
Code:
size = buffer[0] & buffer[1]
id = buffer[2] & buffer[3]
You'd have to combine those two bytes to a single value. I'd recommend pointers for this.

Ex.
Code:
public ushort ReadUInt16(int offset)
{
return (*(ushort*)(bufferPointer + offset));
}
Now the above code won't work because you have to specify a pointer.

Code:
public byte* bufferPointer
{
get
{
fixed (byte* Ptr = buffer)
return Ptr;
}
}
Notice this rquires "allow unsafe coding".
Go to properties -> build and then check the checkbox.

Uhmm... I figure you already have a packetwriter/packetreader, so the above is just to get an understanding.
09/18/2012 21:08 badguy4you#8
Quote:
Originally Posted by I don't have a username View Post
Uhmm... I figure you already have a packetwriter/packetreader, so the above is just to get an understanding.
No i don't have, i am completely clueless about the packet thing :(