Thread: Sending/Receiving packets over socket

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30

    Sending/Receiving packets over socket

    Hi, I have an assignment to do (groan) which requires me to write the client side of a network, in the first part I have to write 3 functions, one which sets up a socket and connects to the server, one which sends a packet and one which receives a packet.

    I've done the first function and it connects fine, no problems there. Now i'm trying to implement the send function, I've done as much as i can (up to the point where i feel i understand it but it still won't work!), here is my code:

    Packet is a struct which holds a char array (called data) and another struct called Header which holds various bits of info about the packet.

    Code:
    // Send a packet to the socket
    // Returns number of bytes written.
    int socket_send_packet(int sock, PACKET *pkt, int reliable)
    {
    	int bytes;
    	printf("Message sent: %s \n", pkt->data);
    	bytes = send(sock, pkt, sizeof(pkt), 0);
    	if(bytes < 0){
    		printf("Error: send\n");
    		exit(1);
    	}
    	printf("Number of bytes sent: %i\n", bytes);
    	return bytes;
    }
    The char[] in the packet is correct (as shown by the first printf), the number of bytes sent is correct as well. The send command doesn't produce an error, but nothing is received the other end

    Anyone know what's going on?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by boblettoj99 View Post
    Hi, I have an assignment to do (groan) which requires me to write the client side of a network, in the first part I have to write 3 functions, one which sets up a socket and connects to the server, one which sends a packet and one which receives a packet.

    I've done the first function and it connects fine, no problems there. Now i'm trying to implement the send function, I've done as much as i can (up to the point where i feel i understand it but it still won't work!), here is my code:

    Packet is a struct which holds a char array (called data) and another struct called Header which holds various bits of info about the packet.

    Code:
    // Send a packet to the socket
    // Returns number of bytes written.
    int socket_send_packet(int sock, PACKET *pkt, int reliable)
    {
    	int bytes;
    	printf("Message sent: %s \n", pkt->data);
    	bytes = send(sock, pkt, sizeof(pkt), 0);
    	if(bytes < 0){
    		printf("Error: send\n");
    		exit(1);
    	}
    	printf("Number of bytes sent: %i\n", bytes);
    	return bytes;
    }
    The char[] in the packet is correct (as shown by the first printf), the number of bytes sent is correct as well. The send command doesn't produce an error, but nothing is received the other end

    Anyone know what's going on?
    Can you post the definition of PACKET please...

    One thing you can try is to pass in the actual struct rather than a pointer then use &pkt in send. This will force it to operate from a local copy of the packet.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Code:
    typedef struct
    {
        HEADER header;  // the packet header (described above)
        char data[MAX_DATA];    // the contents of the packet
    } PACKET;
    The code is prewritten, my job is merely to fill in stubs so i don't think i'm allowed to change what is passed into the function unfortunately

    EDIT: you'll probably want this too:

    Code:
    #define PACKET_SIZE 64
    #define HEADER_SIZE sizeof(HEADER)
    #define MAX_DATA PACKET_SIZE-HEADER_SIZE
    
    typedef struct
    {
        char is_data;   // 1 if this is a data packet, 0 for an ack
        int fragment;   // fragment number (zero indexed)
        char last;      // 1 if this is the last packet in the message, 0 otherwise
        int datalen;    // length of data in bytes - only used for last packet
    } HEADER;

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Numerous possibilities exist...

    Are the is_data or last flags being set correctly?

    Another possibility is data alignment... do both the client and server align data the same way?
    Take a look at your header definition... you may need #pragma align(x) to force proper alignment of your structs.

    send() wants to operate on a char buffer... you may need to cast it as (char)pkt or (char*)&pkt) to get it to send.

    I've seen several cases where send() and sendto() fail with absolutely no error warnings at all.

    I'm sure there are others....

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    bytes = send(sock, pkt, sizeof(pkt), 0);
    Perhaps you should print out the value of sizeof(pkt) as a test. I'm thinking you'll be surprised.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Quote Originally Posted by rags_to_riches View Post
    Code:
    bytes = send(sock, pkt, sizeof(pkt), 0);
    Perhaps you should print out the value of sizeof(pkt) as a test. I'm thinking you'll be surprised.
    It gives me the size of the string length held in pkt->data, should it be something different?

    EDIT: I've modified the print out so it gives all the header values, here is some output (ping is the string held in pkt->data)
    Code:
    printf("Message sent: %s Size: %i\nHeader: %i, %i, %i, %i\n", pkt->data, sizeof(pkt), pkt->header.is_data, pkt->header.fragment, pkt->header.last, pkt->header.datalen);
    Message sent: ping Size: 4
    Header: 1, 0, 1, 4
    Number of bytes sent: 4
    Last edited by boblettoj99; 10-22-2010 at 09:52 AM.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    4 is not the length of the string "ping". The length should be 5... you're forgetting the terminating '\0'.

    You're getting 4 because sizeof(pkt) is returning the size of a pointer in your environment, which happens to be a 32-bit value. You need to dereference the packet to get the size of the packet, so try sizeof(*pkt) instead.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsme86 View Post
    4 is not the length of the string "ping". The length should be 5... you're forgetting the terminating '\0'.

    You're getting 4 because sizeof(pkt) is returning the size of a pointer in your environment, which happens to be a 32-bit value. You need to dereference the packet to get the size of the packet, so try sizeof(*pkt) instead.
    Or he could use sizeof(PACKET)

  9. #9
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Aha, that made a big difference, it seems to actually be sending cos i'm getting a reply!
    Hoorayyyyy...now to sort out the segfault in the receive function...fun times

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the local socket program occur core dump?
    By chenayang in forum Linux Programming
    Replies: 16
    Last Post: 08-16-2010, 08:39 AM
  2. Sendto packets to a listening TCP socket
    By Phoenix_Rebirth in forum Linux Programming
    Replies: 0
    Last Post: 11-29-2009, 09:47 PM
  3. Problem with socket descriptors
    By McKracken in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 08:51 AM
  4. single Socket for listening and sending/receiving, Simultaneously?
    By Aidman in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-01-2003, 11:17 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM