Proper way to create packets along with sending and receiving them in socket programming using C?

The pointer pkt is NOT defined in your application. You have two options: 1) Declare pkt as a normal variable.

The pointer pkt is NOT defined in your application. You have two options: 1) Declare pkt as a normal variable struct packet pkt; pkt. SrcID = 01; .... send(sockfd, &pkt, sizeof(struct packet), 0); 2) The second approach is useful when your packet contains a header followed by a payload: char bufferMAX_PACKET_SIZE; struct packet *pkt = (struct packet *) buffer; char *payload = buffer + sizeof(struct packet); int packet_size; /* should be computed as header size + payload size */ pkt->srcID = 01; ... packet_size = sizeof(struct packet) /* + payload size */ ; send(sockfd, pkt, packet_size, 0); .... UPDATED (to answer your comment): First, you should know that receiving from a TCP socket MAY NOT provide the whole packet.

You need to implement loop (as suggested by Nemo) to read the whole packet. Since you prefer the second option, then you need two loops. The first loop is to read the packet header to extract the payload size and the second loop to read the data.In case of UDP, you don't need to worry about partial receiving.

Here is a sample code (without looping) where sockfd is a UDP socket: char bufferMAX_PACKET_SIZE; struct packet *pkt = (struct packet *) buffer; char *payload = buffer + sizeof(struct packet); int packet_size; /* should be computed as header size + payload size */ ..... /* read the whole packet */ if (recv(sockfd, pkt, MAX_PACKET_SIZE, 0) srcID */ /* you can get data by processing payload variable */ Remember: * you need to implement serialization as mentioned by other users * UDP is unreliable transport protocol while TCP is a reliable transport protocol.

Thanks for your reply. I would like to go with the second option (header+data). At the receiver when I want to store the data part of packet, I can do recvd_data.

SrcID = myID; something like that, right? – user537670 Jul 22 at 5:05 I was looking for concepts on serialization in "GOOGLE" specifically for socket programming....Is ther any good guide available...... – user537670 Jul 22 at 18:45.

Packet Structure When you are going to send the data across the network then you need to consider having a fixed size header followed by a variable length payload. 1 byte header Variable byte Payload The header should give you the size of data you are going to send so that the receiver will always read a fixed size header and then determine the packet length and read the rest of the bytes. Eg: int nRet = recv(nSock,(char*)pBuffer,MESSAGE_HEADER_LENGTH,0); if (nRet ==MESSAGE_HEADER_LENGTH) { int nSizeOfPayload = //Get the length from pBuffer; char* pData = new Char(nSizeOfPayload ); int nPayloadLen = recv(nSock, (char*)pData,nSizeOfPayload ,0); } Variable length data in payload If your structure is having string you should always have the size of the string appended before the string.

Endianess If you are sending the packet to two different applications running in different machines you need to agree before hand on how you are representing your bytes i. E whether you are going to send MSB first or LSB first.

Directly writing structs out to the network is seductively clean, simple, neat... and unfortunately, wrong. (This doesn't stop a lot of people, including many who should know better, from doing it anyway). There's several reasons for this1: Data representation.

This includes the sizes of various types, concerns like endianness, and floating point formats. You can't assume that these are the same on the other end of the connection as they are on your end; they vary a lot by architecture. Structure layout.

Compilers typically add invisible padding between members of structs when they have uneven size - and the layout of this padding also varies by architecture. Bitfields are another structure layout variable. Canonicalisation.In particular, pointer members are meaningless to send across the socket - they don't mean anything to the other side.

You have to send what it points to instead. Another aspect of this is sending an entire array when only some of it is filled with meaningful values - the uninitialised members might leak memory contents that you don't want them to (and sending them is wasteful, anyway). Partial reads.

You end up needing to use a char * pointer when reading anyway, because you might have to resume reading partway through a struct (eg. Your struct might be 830 bytes long, but the first read returned only 500 bytes). The right way to go about this is to use a process called serialisation: for each data structure you want to send across the network, you have a function which canonicalises the contents and packs them into a char buffer in a defined format.

This includes converting integers to a specific endianness (functions like htonl() are useful here). There's also a corresponding function that unpacks a char buffer into the struct form, used on the recieving side. There are several existing libraries of code for serialisation - for example, Google's Protocol Buffers.

The other workable alternative is to serialise your data structures into a textual format, like JSON. This is very good for troubleshooting, because it means that your network protocol is somewhat human-readable.1. Anticipating some objections: Yes, there are workarounds for most of those issues.

But that's just what they are: workarounds, that often rely on compiler-specific features, cancel out most of the simplicity benefits, and still aren't completely reliable anyway.

You've got some errors with variable names and types (I believe you mean send(sockfd, &pkt, sizeof(packet), 0), and you don't want to make pkt a pointer), but apart from that, what you're trying to do will work.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions