Archived

This topic is now archived and is closed to further replies.

amemorex

What exactly is a packet??

Recommended Posts

this is in regards to client/server type programs. actually, i think the title is misleading. i think i understand what a packet is in the programming sense, for instance, you could call this a packet:
  
struct QUERY_PACKET {
  char *guid;
  int  ttl;
};
  
Am I correct? Well, my question is, how are these "packets" sent? Are the variables of data in the packets simply thrown into some string, seperated by character (like ";") like "!search_request;britney spears;5" and sent to a user? if not, how are packets sent and interpreted by another peer?

Share this post


Link to post
Share on other sites
With packet they mean more something like a chunk of data built up in a predetermined way. First 2 bytes respresent the sender, next 2 represent the receiver and the following 1020 bytes are data. That''s a packet in the way of data communication. I put it in a very simple way because most software also adds some CRC code.

Sand Hawk

Member of the Stupid Coders.
http://www.stupidcoders.cjb.net

-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
It doesn''t, unless the user''s information is less than 2 bytes.

It''ll send multiple packets to deliver a chunk of information.

Say you want to send a file that''s 100 bytes long (example here, be merciful). It would be inefficient to send the entire file in one packet, right? What if the packet failed, you''d have to resend the whole thing. Plus, when routers are routing, they would have to process that packet, then move to the next. If the packet are too big, then it''ll cause bottlenecks while others wait to send/receive.

So, it gets broken up in pieces based on the MTU = Max Transmission Size. Every PC has a set value for this, depending on the OS and power users changing it. However, routers have their own limites to the MTU. If it''s bigger than their limit, they''ll break the packet up further (fragment the packet).

Every packet has information where to send the data, who sent it, some control codes for QOS (quality of service) and other necessary info. If you want to learn more, do a search for TCP IP protocol RFC.

Since the data is now encapsulated in a series of packets, if I loose say packets 1 and 4, I don''t have to resend the entire file, I just resend packets 1 and 4. More efficience and easier to recover network problems. Also, by breaking the packets up, the sending and receiving machine can negotiate how many packets to send in a shot, sort of giving some control to how fast or slow you get the packets.

This is a very BIG topic, but I find it interesting.

R.

Share this post


Link to post
Share on other sites
so a simple thing like sending a computer information about you (lets say in like Napster) would be broken up into multiple packets?

i completely understand what you mean by files being split up into multiple chunks, but for smaller things like sending your user info, i don''t see why thats needed

it would seem like it''d be more work and more bandwidth to have to send 3 different packets of just a few simple strings telling someone about your computer

Share this post


Link to post
Share on other sites
A Packet is just a chunk of data in a predetermined format. Whether that's a TCP or UDP packet, or a packet of data in your protocol that you've layered on top of TCP or UDP. Basically, the protocol defines what a packet looks like and how you behave when sending / receiving one. The packet is the information. In reference to representing a user in 2 bytes, if the user data is larger than 2 bytes, you would typically create a packet type to send the user data of one computer to another. Then your 2 bytes could simply be a user ID that the receiving end knows how to look up and retrieve the user data that they received earlier.

And as for splitting up your data, 100 bytes is small, MTU for 10 megabit ethernet is 1500 bytes. Internet MTUs are somewhere around there, sometimes smaller. As for figuring out when to split up a packet, that's a matter of determining frequency of the packet and the overhead associated with splitting it into smaller packets. It's just something you have to balance, size vs. overhead ratio.

Edited by - jonstelly on January 10, 2002 5:39:13 PM

Share this post


Link to post
Share on other sites
quote:
Original post by amemorex
now my main question: how are packets sent?



Packets are usually sent over a network (LAN/WAN?Web etc) with some form of Network API like Winsock (Win/Linux), DirectPlay (Win), Sprockets (Mac) etc...

quote:
Original post by amemorex
are the variables contained in them setup in a long string and then sent?


With winsock you fill a buffer (char array usuall) with the data you want to send and then call either send (TCP) or sendto (UDP). This is a rough explanation (have mercy plz!) you can check MDSN for more info on Winsock or DirectPlay!

Convict@Large

Share this post


Link to post
Share on other sites
There are a bunch of ways to handle sending a packet. Everyone has their favorite method. Serializing them to a byte stream is a good option if you are looking for crossplatform support. Basically, that involves going over all of the members of some type of packet structure and writing them out to a linear memory buffer for sending.

If however you don''t care about crossplatform support, my favorite method is to simply define a linear structure to begin with. For example:

  
typedef struct NETPACKET{
USHORT nType;
USHORT nLength;
USHORT nId;
char * pszText;
}*PNETPACKET;


Is NOT a linear packet because pszText is a pointer, hence will point to a different location in memory. If you were to take the size of the packet, it would be 10 bytes, 2 for each USHORT and 4 for the char pointer (on a 32 bit system).

The following is the same structure written to be linear:

  
#define MAX_TEXT 256

typedef struct NETPACKET{
USHORT nType;
USHORT nLength;
USHORT nId;
char szText[MAX_TEXT];
}*PNETPACKET;


By changing szText into an array, the size of the structure will now be 262 bytes. Basically, if you want to send this data to someone else as a packet, you determine how much of it needs to be sent. Assuming that the szText parameter won''t ALWAYS be 256 bytes, and could often be less than that, you only need to send the structrue up until the NULL terminating character for szText.

As for how you send it, you''ll need to look into Winsock or Directplay, but for either of those, there is a type of send function that takes the address of the buffer you want to send, and in this case, that would be the address of an instance of the above structure (the linear one).

Share this post


Link to post
Share on other sites