PDA

View Full Version : [Help] Client Server Network Packets


Menta2K
07.11.2006, 00:13
I noticed somthing strange and cant find any explanation for that ...

Game server some times send 2 game packets in 1 TCP packet and that is ok but look at the packet below:

================= START Enqueue ================
0 1 2 3 4 5 6 7 8 9 A B C D E F
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
0000 03 00 94 17 00 FB CE CE CE 6F 03 57 D0 28 1D 1D .........o.W.(..
0010 1D BC D0 84 03 FB 49 07 B3 64 ......I..d
================= END Enqueue ==================
from that we have
First game Packet
Len 3 Bytes
Packet Data 0x94 (Packet ID 0x63 == Request Quest List)
Second Game Packet
Len 17
Pacet ID ID 0x03 == EnterWorld
Total Bytes should be 3+17=20
Then what the hell is last 6 bytes
03 FB 49 07 B3 64
If that is a checksum whey the previus packet dont have checksum at the end like this one :
================= START Enqueue ================
0 1 2 3 4 5 6 7 8 9 A B C D E F
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
0000 07 00 FF CA CA CA 6B ......k
================= END Enqueue ==================
Or maybe is a first part of incomplete game packet that will come in next TCP packet but then len of that packet should be 64259 and that is imposible

mcrabben7
07.11.2006, 01:49
TCP is a streaming protocol, lol. That's why you send len in the packet and read only len bytes for each packet.

There is no error....

Menta2K
07.11.2006, 08:40
i am not using network straming i am using async sockets and reading all the data that client sends put in a byte queue then check packet priority a dequeue .. nvm i am idiot len of the second packet is not 17 ists 23 .. i was too sleepy
tnx anyway

mcrabben7
07.11.2006, 09:44
TCP is a Streaming Protocol, please go read what that means. It doesn't matter if you use Sync or Async sockets, the TCP packets will come at you in order, but they will have no packet breaks. In essence, you will get one giant, never-ending stream of bytes. This is why you have to read the len from the packet and only read len bytes. Otherwise, you will never know where the start and end of a packet are.

I did socket programming in C++ using TCP, so I'm not talking up a storm without experience.

Menta2K
07.11.2006, 13:31
TCP is a Streaming Protocol, please go read what that means. It doesn't matter if you use Sync or Async sockets, the TCP packets will come at you in order, but they will have no packet breaks. In essence, you will get one giant, never-ending stream of bytes. This is why you have to read the len from the packet and only read len bytes. Otherwise, you will never know where the start and end of a packet are.

I did socket programming in C++ using TCP, so I'm not talking up a storm without experience.

Sorry but i cannot agree with that
its depends how you are using the sockets
In example
in C if you are using
int recv(int sockfd, void *buf, int len, unsigned int flags);
return value is how much data (bytes ) actualy comes to that socket and now here is the "Solved" question recv recived 26 bytes of data and in this 26 bytes we have 2 game packets.

Met45
07.11.2006, 13:58
but in general mcrab is right tcp is always a stream dude...

mcrabben7
07.11.2006, 20:23
From Wikipedia under Transmission Control Protocol:
The Transmission Control Protocol (TCP) is a virtual circuit protocol that is one of the core protocols of the Internet protocol suite, often simply referred to as TCP/IP. Using TCP, applications on networked hosts can create connections to one another, over which they can exchange streams of data. The protocol guarantees reliable and in-order delivery of data from sender to receiver. TCP also distinguishes data for multiple connections by concurrent applications (e.g. Web server and e-mail server) running on the same host.

TCP supports many of the Internet's most popular application protocols and resulting applications, including the World Wide Web, e-mail and Secure Shell.

In the Internet protocol suite, TCP is the intermediate layer between the Internet Protocol (IP) below it, and an application above it. Applications often need reliable pipe-like connections to each other, whereas the Internet Protocol does not provide such streams, but rather only unreliable packets. TCP does the task of the transport layer in the simplified OSI model of computer networks. The other main transport-level Internet protocol is UDP.


Applications send streams of octets (8-bit bytes) to TCP for delivery through the network, and TCP divides the byte stream into appropriately sized segments (usually delineated by the maximum transmission unit (MTU) size of the data link layer of the network the computer is attached to). TCP then passes the resulting packets to the Internet Protocol, for delivery through a network to the TCP module of the entity at the other end. TCP checks to make sure that no packets are lost by giving each packet a sequence number, which is also used to make sure that the data are delivered to the entity at the other end in the correct order. The TCP module at the far end sends back an acknowledgement for packets which have been successfully received; a timer at the sending TCP will cause a timeout if an acknowledgement is not received within a reasonable round-trip time (or RTT), and the (presumably lost) data will then be re-transmitted. The TCP checks that no bytes are damaged by using a checksum; one is computed at the sender for each block of data before it is sent, and checked at the receiver.

Also, versus UDP:
The User Datagram Protocol (UDP) is one of the core protocols of the Internet protocol suite. Using UDP, programs on networked computers can send short messages sometimes known as datagrams to one another.

UDP does not provide the reliability and ordering guarantees that TCP does. Datagrams may arrive out of order or go missing without notice. Without the overhead of checking if every packet actually arrived, UDP is faster and more efficient for many lightweight or time-sensitive purposes. Also, its stateless nature is useful for servers that answer small queries from huge numbers of clients. Compared to TCP, UDP is required for broadcast (send to all on local network) and multicast (send to all subscribers).

Common network applications that use UDP include the Domain Name System (DNS), streaming media applications, Voice over IP, Trivial File Transfer Protocol (TFTP) and online games.

UDP is a packet-based protocol using datagrams and non-socket-based connections. UDP is a send and hope to God it gets there protocol.

TCP is a stream protocol because it makes sure you get all the packet data in order, on the same socket, with reliability. It does not break the stream up in any way. If you send 5600 bytes in 2 seconds and go to do a socket read, you will get 5600 bytes of data. This could be 1, 2, 500 packets. TCP does not care, because it only handles the stream. That's why it is up to you to read properly.

I'll show you some example C++ Winsock Receive code:
short NetworkingMS::recvData(SOCKET RecvSocket, std::string * strToRecv)
{
short len = MAX_DATA_SIZE;
char buffer[1024];

if ((len = recv(RecvSocket, buffer, len, 0)) == SOCKET_ERROR)
{
std::cerr << "recv: " << WSAGetLastError() << "\n";
}

if( len > 0 )
{
std::string temp(buffer);
temp.resize(len);
*strToRecv = temp;
return len;
}
else if(len == 0)
{
std::cerr << "recv: remote host closed the connection.\n";
return len;
}
else
return -1;
}

Just read len from the packet and return it to a packet handler switch where you read the header from the next 2 bytes. My example code reads data up to max length, which isn't what you want, but this was part of a Library I made for one of my classes. I handled 2 packets in 1 stream inside my server code seperately.

Sorry if I came across as trying to be rude or something, but you were wrong and I had to try to explain to you the difference between a stream and packet protocol.