0

I have the following simple UDP server client. When the client sends something to the server it works fine but I can't get the server to send back reply to client. I'm not sure if the problem is the server reply or client read or both. I have commented where the reply from server/read from client are.

client

#define PORT     8080 
#define MAXLINE 1024 

// Driver code 
int main() { 
    int sockfd; 
    char buffer[MAXLINE]; 
    char *hello = "Hello from client"; 
    struct sockaddr_in   servaddr; 

    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
        perror("socket creation failed"); 
        exit(EXIT_FAILURE); 
    } 

    memset(&servaddr, 0, sizeof(servaddr)); 

    // Filling server information 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_port = htons(PORT); 
    //servaddr.sin_addr.s_addr = INADDR_ANY; 
    servaddr.sin_addr.s_addr = inet_addr("192.168.0.111"); //server address to send message to

    int n, len; 

    sendto(sockfd, (const char *)hello, strlen(hello), 
        MSG_CONFIRM, (const struct sockaddr *) &servaddr, 
            sizeof(servaddr)); 
    printf("Hello message sent.\n"); 
    //**NOT READING THE REPLY FROM SERVER HERE**
    n = recvfrom(sockfd, (char *)buffer, MAXLINE, 
                MSG_WAITALL, (struct sockaddr *) &servaddr, 
                &len); 
    buffer[n] = '\0'; 
    printf("Server : %s\n", buffer); 

    close(sockfd); 
    return 0; 
} 

server

    #define PORT     8080 
    #define MAXLINE 1024 

    // Driver code 
    int main() { 
        int sockfd; 
        char buffer[MAXLINE]; 
        char *hello = "Hello from server"; 
        struct sockaddr_in servaddr, cliaddr; 


    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
        perror("socket creation failed"); 
        exit(EXIT_FAILURE); 
    } 

    memset(&servaddr, 0, sizeof(servaddr)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 

    // Filling server information 
    servaddr.sin_family = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = INADDR_ANY; //ip of all local interfaces i.e. ip of wlan, ethernet etc
    servaddr.sin_port = htons(PORT); 

    // Bind the socket with the server address 
    if ( bind(sockfd, (const struct sockaddr *)&servaddr, 
            sizeof(servaddr)) < 0 ) 
    { 
        perror("bind failed"); 
        exit(EXIT_FAILURE); 
    } 

    int len, n; 
    n = recvfrom(sockfd, (char *)buffer, MAXLINE, 
                MSG_WAITALL, ( struct sockaddr *) &cliaddr, 
                &len); 
    buffer[n] = '\0'; 
    printf("Client : %s\n", buffer); 
    //**Client not receiving this message****
    sendto(sockfd, (const char *)hello, strlen(hello), 
        MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
            len); 
    printf("Hello message sent.\n"); 

    return 0; 
} 
Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • That's not your entire code. – Steve Oct 26 '18 at 03:57
  • Have you tested known working programs (such as `nc`) with these ports? It would be a good idea to first rule out issues with firewall / routing on your machines. You can then use them to test individual parts of your program (e.g. just client or server). You should also check the return value of `sendto` and `recvfrom` in case an error occurred. – paddy Oct 26 '18 at 04:01
  • Your code (with a bit of cleanup) works for me. Is port 8080 free on your machine? Is the IP address OK? I changed this to `127.0.0.1`. One other issue is the client `n = recvfrom()`, and then `buffer[n] = '\0'` - The `recvfrom()` function returns `-1` on error. – Kingsley Oct 26 '18 at 04:01
  • By the way, [you shouldn't use MSG_CONFIRM flag for the initial datagram](https://stackoverflow.com/a/42105469/1553090) – paddy Oct 26 '18 at 04:04
  • If the port or ip doesnt work then wouldnt the send from client to server also wouldnt work? Right now client -> server works everytime but server back to client doesnt work. – Lightsout Oct 26 '18 at 04:05

1 Answers1

0

sendto last parameter from server is wrong.it is not len instead of

sendto(sockfd, (const char *)hello, strlen(hello), 
    MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
        len); 

put

sendto(sockfd, (const char *)hello, strlen(hello), 
    MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
        sizeof(cliaddr));