Simple UDP socket code, sending and receiving messages

33,724

Solution 1

If you use the function recvfrom()

    ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
             struct sockaddr *src_addr, socklen_t *addrlen);

What this function does is it fills a structure of sockaddr with the IP and port information of the packet that it has just received. For example, if your code that sends first then receives sends a packet to the receiver, the receiver should be able to fill the structure values of sin_addr and sin_port with the correct values. You can then make a call of sendto() with this information in order to send it to the correct machine.

Here's the man pages for these functions:

https://linux.die.net/man/2/recvfrom

https://linux.die.net/man/2/sendto

Solution 2

Try using this:

inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)

127.0.0.1 is the loopback IP. The address is used to establish an IP connection to the same machine, which seems to be your case.

A detailed way to solve the problem can be found here

Share:
33,724
forever_fighting_21
Author by

forever_fighting_21

Updated on May 26, 2020

Comments

  • forever_fighting_21
    forever_fighting_21 almost 4 years

    I am just learning UDP sockets and this is my first code involving it. I have two programs which send and receive messages back and forth. My question is it seems I have to declare which IP address I am sending/receiving from multiple times throughout the code as it changes but I feel there is a better way to do this without changing the inet_addr manually within the codes. From my reading it looks like sendto and recvfrom may be able to help but I am unsure how to use them in this context. If anyone could show me how to fix my simple problem I would greatly appreciate it! Thanks

    CODE 1: Send then Receive

    int main(int argc, char *argv[])
    {
    //initialize socket and structure
    int socket_info;
    struct sockaddr_in server;
    char message[100];
    char incoming_message[100];
    
    printf("Input Message: ");
    fgets(message, 100, stdin);
    
        //create socket
        socket_info = socket(AF_INET, SOCK_DGRAM, 0);
        if (socket_info == -1) {
        printf("Could not create socket");
        }
    
    //assign local values
        server.sin_addr.s_addr = inet_addr("172.21.8.178");
        server.sin_family = AF_INET;
        server.sin_port = htons( 1100 );
    
        //binds connection
        if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {    
    perror("Connection error");
           return 1;
        }
        puts("Bind");
    
        //assign new value to connect to
        server.sin_addr.s_addr = inet_addr("172.21.8.179");
    
        //checks connection 
        if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) <       0) {
        perror("Connection error");
           return 1;
        }
        puts("Connected");
    
        //sends message
    if(send(socket_info, message, strlen(message), 0) <0) {        
    perror("Send failed");
        return 1;
        }
        puts("Message Sent");
    
    //receives message back    
    if(recv(socket_info, incoming_message, sizeof(incoming_message), 0) <0) {    
    puts("Received failed");
        return 1;
        }
        puts("Message received");
        puts(incoming_message);
    
    close(socket_info);
    
    }
    

    CODE 2: Receive then Send

    int main(int argc, char *argv[])
    {
    //initialize socket and structure
    int socket_info;
    struct sockaddr_in server;
    char incoming_message[100];
    
        //create socket
        socket_info = socket(AF_INET, SOCK_DGRAM, 0);
        if (socket_info == -1) {
        printf("Could not create socket");
        }
    
        //assign values
        server.sin_addr.s_addr = inet_addr("172.21.8.179");
        server.sin_family = AF_INET;
        server.sin_port = htons( 1100 );
    
        //checks connection
        if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Connection error");
           return 1;
        }
        puts("Bind");
    
        //Receive an incoming message
    if( recv(socket_info, incoming_message, sizeof(incoming_message), 0) < 0) {      
    puts("Received failed");
        return 1;
        }
        puts("Message received");
        puts(incoming_message);
    
    server.sin_addr.s_addr = inet_addr("172.21.8.178");
    
    if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Connection error");
         return 1;
        }
        puts("Connected");
    
    //Sends message back
    char message[100];
    
    printf("Input Message: ");
    fgets(message, 100, stdin);
    
    if(send(socket_info, message, strlen(message), 0) <0) {        
    perror("Send failed");
        return 1;
        }
        puts("Message Sent");
    
    close(socket_info);
    }