Linux Socket Programming-UDP

Source: Internet
Author: User

The following is the code for the UDP server:

/* SERVER.C */#include <stdio.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h > #include <ctype.h> #include "wrap.h" #define MAXLINE 80#define serv_port 8000int Main (void) {struct sockaddr_in SERVADDR, cliaddr;socklen_t cliaddr_len;int sockfd;char buf[maxline];char str[inet_addrstrlen];int i, N;SOCKFD = Wrap_ Socket (af_inet, SOCK_DGRAM, 0); Bzero (&servaddr, sizeof (SERVADDR)); servaddr.sin_family = Af_inet;servaddr.sin_ ADDR.S_ADDR = htonl (inaddr_any); servaddr.sin_port = Htons (Serv_port); Wrap_bind (SOCKFD, (struct sockaddr*) & SERVADDR, sizeof (SERVADDR));p rintf ("accepting connections ... \ n"); while (1) {Cliaddr_len = sizeof (CLIADDR); n = recvfrom (SOCKFD, buf, MAXLINE, 0, (struct sockaddr *) &cliaddr, &cliaddr_len); if (n = =-1) {perr_exit ("Recvfrom error");} printf ("received from%s at Port%d\n", Inet_ntop (Af_inet, &cliaddr.sin_addr, str, sizeof (STR)), Ntohs (cliaddr.sin_ Port)); for (i = 0; i < n; ++i) {Buf[i] = ToUpper (Buf[i]);} n = sendto (sOCKFD, buf, N, 0, (struct sockaddr *) &cliaddr, sizeof (CLIADDR)), if (n = =-1) perr_exit ("SendTo error!");}} 

Client code:

/* client.c */#include <stdio.h> #include <string.h> #include <unistd.h> #include <netinet/in.h > #include <arpa/inet.h> #include "wrap.h" #define MAXLINE 80#define serv_port 8000int Main (int argc, char *argv[] {struct sockaddr_in servaddr;int sockfd, N;char buf[maxline];char str[inet_addrstrlen];socklen_t SERVADDR_LEN;SOCKFD = Wrap_socket (af_inet, SOCK_DGRAM, 0); Bzero (&servaddr, sizeof (SERVADDR)); servaddr.sin_family = Af_inet;inet_pton (Af_inet, "127.0.0.1", &servaddr.sin_addr); servaddr.sin_port = Htons (Serv_port); while (Fgets (buf, MAXLINE, stdin) ! = NULL) {n = sendto (SOCKFD, buf, strlen (BUF), 0, (struct sockaddr *) &servaddr, sizeof (SERVADDR)), if (n = =-1) perr_exit ( "SendTo error") n = recvfrom (SOCKFD, buf, MAXLINE, 0, NULL, 0), if (n = =-1) perr_exit ("Recvfrom error"); Wrap_write (Stdout_f Ileno, buf, n);} Wrap_close (SOCKFD); return 0;}

Because the server and the client do not have the conditions to establish a connection and then communicate, the server can have concurrency capabilities.

Use CTRL + C to close the server, and then run server,client to contact the server.

Linux Socket Programming-UDP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.