Socket Programming: UDP Client-Server in C
<p>I'm trying to write a client server program using UDP, and wait-and-stop, but I haven't got to that part, I'm still trying to figure it out how the two processes (server and client) communicate, because on my client program, the user needs to enter the server name or IP address, and a port name, and then send an expression that the server should calculate. However, I dug some tutorials in the internet and after coding accordingly (or I thought so) I can't make the client communicate with the server. Below is my code, please enlighten me what I'm doing wrong, if it's the <code>bind()</code>, <code>sendto()</code>, <code>recvfrom()</code> or <code>socket()</code>, or all of them. I can't see what exactly is wrong. I know that the client-side shouldn't run on a infinite loop, but so far I want to make the programs communicate with each other, afterwards I'll polish my code. Thanks!</p>

<p>client-side code:</p>

<pre><code>#include &lt;stdio.h&gt;      // Default System Calls
#include &lt;stdlib.h&gt;     // Needed for OS X
#include &lt;string.h&gt;     // Needed for Strlen
#include &lt;sys/socket.h&gt; // Needed for socket creating and binding
#include &lt;netinet/in.h&gt; // Needed to use struct sockaddr_in
#include &lt;time.h&gt;       // To control the timeout mechanism

#define EXPR_SIZE   1024
#define BUFLEN      512
#define TRUE        1
#define FALSE       0
#define SERVERLEN   1024

int main(int argc, char **argv){

    long portNum;           // Since it's possible to input a value bigger
                            // than 65535 we'll be using long to
                            // avoid overflows
    char expr[EXPR_SIZE];
    char server[SERVERLEN];
    int fd;                 // file descriptor for the connected socket
    int buf[512];
    struct hostent *h;           // information of the host
    unsigned int addrLen;        // address length after getting the port number
    struct sockaddr_in myaddr;   // address of the client
    struct sockaddr_in servaddr; // server's address
    unsigned int exprLen;
    socklen_t slen = sizeof(servaddr);

    printf("Enter server name or IP address:");
    scanf("%s",server);
    printf("Enter port:");
    scanf("%ld",&amp;portNum);
    if ((portNum &lt; 0) || (portNum &gt; 65535)) {
        printf("Invalid port number. Terminating.");
        return 0;
    }
    printf("Enter expression:");
    scanf("%s",expr);

    if((fd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0){
        perror("cannot create socket");
        return 0;
    }

    memset((char *)&amp;myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(0);

    if(bind(fd, (struct sockaddr *)&amp;myaddr, sizeof(myaddr)) &lt; 0){
        perror("cannot bind");
        return 0;
    }

    /*
     // Discovering the port number the OS allocated
     addrLen = sizeof(myaddr);
     if(getsockname(fd, (struct sockaddr *)&amp;myaddr, &amp;addrLen) &lt; 0){
     perror("cannot getsockname");
     return 0;
     }
     printf("local port number = %d\n", ntohs(myaddr.sin_port));
     */

    memset((char*)&amp;servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htonl(portNum);

    exprLen = sizeof(expr);


    while(TRUE){
        printf("Sending message to %s port %ld\n",server, portNum);
        if (sendto(fd, expr, strlen(expr), 0, (struct sockaddr *)&amp;servaddr, slen) &lt; 0) {
            perror("cannot sendto()");
        }
        printf("Success\n");

    }


    return 0;
}
</code></pre>

<p>Server-side code:</p>

<pre><code>#include &lt;stdio.h&gt;      // Default System Calls
#include &lt;stdlib.h&gt;     // Needed for OS X
#include &lt;string.h&gt;     // Needed for Strlen
#include &lt;sys/socket.h&gt; // Needed for socket creating and binding
#include &lt;netinet/in.h&gt; // Needed to use struct sockaddr_in
#include &lt;time.h&gt;       // To control the timeout mechanism

#define EXPR_SIZE   1024
#define BUFLEN      512
#define TRUE        1
#define SERVERLEN   1024

int main(int argc, char **argv){

    struct sockaddr_in myaddr;  // address of the server
    struct sockaddr_in claddr;  // address of the client
    char buf[BUFLEN];
    int fd;
    long recvlen;
    socklen_t clientlen;



    if((fd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0){
        perror("cannot create socket");
        return 0;
    }

    memset((char *)&amp;myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(0);

    if(bind(fd, (struct sockaddr *)&amp;myaddr, sizeof(myaddr)) &lt; 0){
        perror("cannot bind");
        return 0;
    }
    clientlen = sizeof(claddr);

    while (TRUE) {
        recvlen = recvfrom(fd, buf, BUFLEN, 0, (struct sockaddr *)&amp;claddr, &amp;clientlen);
        if (recvlen &lt; 0) {
            perror("cannot recvfrom()");
            return 0;
        }
        printf("Received %ld bytes\n",recvlen);
        buf[recvlen] = 0;
        printf("Received message: \"%s\"\n",buf);

    }

    return 0;
}
</code></pre>

<p>The server program doesn't output anything, while the client outputs until the process is interrupted:</p>

<pre><code>Enter server name or IP address:127.0.0.1
Enter port:30
Enter expression:2+2
Sending message to 127.0.0.1 port 30
cannot sendto(): Can't assign requested address
</code></pre>

<p>I tried changing the server name to localhost, and other ports, but to no avail.</p>