3

I am experimenting with sending a message from a server to a client using UDP sockets... however, my client is not receiving any messages. Any feedback would be greatly appreciated!

EDIT: maybe I should clarify that the message seems to be sending successfully, however when I run the client it gets stuck on waiting for data... any pointers as to why this is happening would be appreciated!

UDP SERVER

#include<stdio.h> 
#include<string.h>
#include<stdlib.h> 
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER "127.0.0.1" 
#define BUFLEN 512  //Max length of buffer
#define PORT 8888   //The port on which to listen for incoming data

void die(char *s)
{
    perror(s);
    exit(1);
}

int main(void)
{
    struct sockaddr_in si_other;

    int s, i, slen = sizeof(si_other);
    char buf[BUFLEN];
    char message[BUFLEN]; 
    //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);

    if (inet_aton(SERVER , &si_other.sin_addr) == 0) 
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    //bind socket to port
    if( bind(s , (struct sockaddr*)&si_other, sizeof(si_other) ) == -1)
    {
        die("bind");
    }
    else
    {
        printf ("Success!\n");
    }   


    while(1)
    {
        printf("Enter message : ");
        gets(message);

        //send the message
        if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
        {
            die("sendto()");
        }
        else
        {
            printf ("Success!\n");
        }

    } 

    close(s);
    return 0;
}

UDP CLIENT

#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>


#define BUFLEN 512  //Max length of buffer
#define PORT 8888   //The port on which to send data

void die(char *s)
{
    perror(s);
    exit(1);
}

int main(void)
{
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other), recv_len;
    char buf[BUFLEN];
    char message[BUFLEN];

    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    while(1)
    { 
        printf("Waiting for data...");
        fflush(stdout);

        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }

        //print details of the client/peer and the data received
        printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Data: %s\n" , buf);        
    } 

    close(s);
    return 0;
}
user1816546
  • 313
  • 4
  • 15
  • `int s, i, slen=sizeof(si_other), recv_len;`: what is i? Try: `socklen_t slen;` and `slen = sizeof si_other` – jhscheer Mar 09 '17 at 15:44
  • I've managed to make it work... I need to call bind() from the client and not the server... – user1816546 Mar 09 '17 at 15:47
  • Write that as an answer then (you are allowed to answer your own questions) and then accept it. – JeremyP Mar 09 '17 at 15:49
  • On an unrelated note, never ever ever on pain of having hot knitting needles stuck in your eyes ever never ever use [gets()](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – JeremyP Mar 09 '17 at 15:51
  • thanks for the tip! :) – user1816546 Mar 09 '17 at 15:52

2 Answers2

1

Call bind() from the client and not the server

user1816546
  • 313
  • 4
  • 15
  • Since you're using `SOCK_DGRAM` you don't need to call `bind()` at all. http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#sendtorecv – jhscheer Mar 09 '17 at 15:56
  • http://stackoverflow.com/questions/8636717/is-it-always-required-to-bind-a-socket – jhscheer Mar 09 '17 at 16:08
  • specific to my code above, this is the only solution I have found. If you can find a way around it, I would be in finding an alternative solution! – user1816546 Mar 09 '17 at 16:12
1

you need to bind your client, not your server.

Niels
  • 13
  • 7