Note that there are some explanatory texts on larger screens.

plurals
  1. POWhats wrong with my UDP Client Server?
    primarykey
    data
    text
    <p>I have a UDP Server that should sit and wait for a client to connect to it, and send it a string of a filename. Right now I just want it to echo back the filename to the client. Here is my code</p> <p>Server</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;netdb.h&gt; #define MAXBUFLEN 1024 // Usage: ./server PORT int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server; size_t clientSize; struct sockaddr_storage client; char buf[MAXBUFLEN]; int portno = atoi(argv[1]); int numbytes; printf("Port: %d\n", portno); // Create UDP Socket if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Can't create socket"); exit(-1); } // Configure socket memset(&amp;server, 0, sizeof server); server.sin_family = AF_INET; // Use IPv4 server.sin_addr.s_addr = INADDR_ANY; // My IP server.sin_port = htons(portno); // Server Port // Bind socket if ((bind(sockfd, (struct sockaddr *) &amp;server, sizeof(server))) == -1) { close(sockfd); perror("Can't bind"); } while (1) { printf("Waiting for data...\n"); // Receive data from Client clientSize = sizeof(client); numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1,0, (struct sockaddr *) &amp;client, &amp;clientSize); buf[numbytes] = '\0'; printf("client sent: %s\n", buf); // Rely to client sendto(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *) &amp;client, &amp;clientSize); } printf("Closing"); close(sockfd); return 0; } </code></pre> <p>Client</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;netdb.h&gt; #define MAXBUFLEN 1024 //Usage: ./client PORT IP FILE int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server; // struct sockaddr_storage client; char buf[MAXBUFLEN]; int portno = atoi(argv[1]); char *serverIP = argv[2]; char *filename = argv[3]; int numbytes; printf("Port: %d, IP:%s, File:%s\n", portno, serverIP, filename); // Create UDP Socket if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Can't create socket"); exit(-1); } // The server IP and Port memset(&amp;server, 0, sizeof server); server.sin_family = AF_INET; // Use IPv4 server.sin_port = htons(portno); // Server Port struct hostent *hp = gethostbyname(serverIP); memcpy((char*)&amp;server.sin_addr, (char*)hp-&gt;h_name, hp-&gt;h_length); printf("Sent %s\n", filename); // No need to bind, just send request for file int serverSize = sizeof(server); sendto(sockfd, argv[3], strlen(argv[3]), 0, (struct sockaddr *) &amp;server, serverSize); printf("Waiting for reply\n"); recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *) &amp;server, serverSize); buf[numbytes] = '\0'; printf("server sent: %s\n", buf); close(sockfd); } </code></pre> <p>So far the Server just prints out </p> <pre><code>Waiting for data... </code></pre> <p>only once... so its never getting a request.</p> <p>and the Client prints out:</p> <pre><code>./client 8083 127.0.0.1 hello Port: 8083, IP:127.0.0.1, File:hello Sent hello Waiting for reply server sent: </code></pre> <p>Please help, I'm new to the world of C and UDP Sockets.</p> <p>Oh yeah, and I already read Beej's Guide... please don't just post that as an answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload