欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

UDP server Code

程序员文章站 2023-11-30 09:08:34
The programs are an echo server and client for UDP datagrams. ......

code example:

the following programs demonstrate the use of getaddrinfo(), gai_strerror(), freeaddrinfo(), and  getnameinfo().  the programs are an echo server and client for udp datagrams.

服务器端程序:
 1 ###server program
 2 
 3        #include <sys/types.h>
 4        #include <stdio.h>
 5        #include <stdlib.h>
 6        #include <unistd.h>
 7        #include <string.h>
 8        #include <sys/socket.h>
 9        #include <netdb.h>
10 
11        #define buf_size 500
12 
13        int
14        main(int argc, char *argv[])
15        {
16            struct addrinfo hints;
17            struct addrinfo *result, *rp;
18            int sfd, s;
19            struct sockaddr_storage peer_addr;
20            socklen_t peer_addr_len;
21            ssize_t nread;
22            char buf[buf_size];
23 
24            if (argc != 2) {
25                fprintf(stderr, "usage: %s port\n", argv[0]);
26                exit(exit_failure);
27            }
28 
29            memset(&hints, 0, sizeof(struct addrinfo));
30            hints.ai_family = af_unspec;    /* allow ipv4 or ipv6 */
31            hints.ai_socktype = sock_dgram; /* datagram socket */
32            hints.ai_flags = ai_passive;    /* for wildcard ip address */
33            hints.ai_protocol = 0;          /* any protocol */
34            hints.ai_canonname = null;
35            hints.ai_addr = null;
36            hints.ai_next = null;
37 
38            s = getaddrinfo(null, argv[1], &hints, &result);
39            if (s != 0) {
40                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
41                exit(exit_failure);
42            }
43 
44            /* getaddrinfo() returns a list of address structures.
45               try each address until we successfully bind(2).
46               if socket(2) (or bind(2)) fails, we (close the socket
47               and) try the next address. */
48 
49            for (rp = result; rp != null; rp = rp->ai_next) {
50                sfd = socket(rp->ai_family, rp->ai_socktype,
51                        rp->ai_protocol);
52                if (sfd == -1)
53                    continue;
54 
55                if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
56                    break;                  /* success */
57 
58                close(sfd);
59            }
60 
61            if (rp == null) {               /* no address succeeded */
62                fprintf(stderr, "could not bind\n");
63                exit(exit_failure);
64            }
65 
66            freeaddrinfo(result);           /* no longer needed */
67 
68            /* read datagrams and echo them back to sender */
69 
70            for (;;) {
71                peer_addr_len = sizeof(struct sockaddr_storage);
72                nread = recvfrom(sfd, buf, buf_size, 0,
73                        (struct sockaddr *) &peer_addr, &peer_addr_len);
74                if (nread == -1)
75                    continue;               /* ignore failed request */
76 
77                char host[ni_maxhost], service[ni_maxserv];
78 
79                s = getnameinfo((struct sockaddr *) &peer_addr,
80                                peer_addr_len, host, ni_maxhost,
81                                service, ni_maxserv, ni_numericserv);
82                if (s == 0)
83                    printf("received %zd bytes from %s:%s\n",
84                            nread, host, service);
85                else
86                    fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
87 
88                if (sendto(sfd, buf, nread, 0,
89                            (struct sockaddr *) &peer_addr,
90                            peer_addr_len) != nread)
91                    fprintf(stderr, "error sending response\n");
92            }
93        }

客户端程序:

 1  ###client program
 2 
 3        #include <sys/types.h>
 4        #include <sys/socket.h>
 5        #include <netdb.h>
 6        #include <stdio.h>
 7        #include <stdlib.h>
 8        #include <unistd.h>
 9        #include <string.h>
10 
11        #define buf_size 500
12 
13        int
14        main(int argc, char *argv[])
15        {
16            struct addrinfo hints;
17            struct addrinfo *result, *rp;
18            int sfd, s, j;
19            size_t len;
20            ssize_t nread;
21            char buf[buf_size];
22 
23            if (argc < 3) {
24                fprintf(stderr, "usage: %s host port msg...\n", argv[0]);
25                exit(exit_failure);
26            }
27 
28            /* obtain address(es) matching host/port */
29 
30            memset(&hints, 0, sizeof(struct addrinfo));
31            hints.ai_family = af_unspec;    /* allow ipv4 or ipv6 */
32            hints.ai_socktype = sock_dgram; /* datagram socket */
33            hints.ai_flags = 0;
34            hints.ai_protocol = 0;          /* any protocol */
35 
36            s = getaddrinfo(argv[1], argv[2], &hints, &result);
37            if (s != 0) {
38                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
39                exit(exit_failure);
40            }
41 
42            /* getaddrinfo() returns a list of address structures.
43               try each address until we successfully connect(2).
44               if socket(2) (or connect(2)) fails, we (close the socket
45               and) try the next address. */
46 
47            for (rp = result; rp != null; rp = rp->ai_next) {
48                sfd = socket(rp->ai_family, rp->ai_socktype,
49                             rp->ai_protocol);
50                if (sfd == -1)
51                    continue;
52 
53                if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
54                    break;                  /* success */
55 
56                close(sfd);
57            }
58 
59            if (rp == null) {               /* no address succeeded */
60                fprintf(stderr, "could not connect\n");
61                exit(exit_failure);
62            }
63 
64            freeaddrinfo(result);           /* no longer needed */
65 
66            /* send remaining command-line arguments as separate
67               datagrams, and read responses from server */
68 
69            for (j = 3; j < argc; j++) {
70                len = strlen(argv[j]) + 1;
71                        /* +1 for terminating null byte */
72 
73                if (len + 1 > buf_size) {
74                    fprintf(stderr,
75                            "ignoring long message in argument %d\n", j);
76                    continue;
77                }
78 
79                if (write(sfd, argv[j], len) != len) {
80                    fprintf(stderr, "partial/failed write\n");
81                    exit(exit_failure);
82                }
83 
84                nread = read(sfd, buf, buf_size);
85                if (nread == -1) {
86                    perror("read");
87                    exit(exit_failure);
88                }
89 
90                printf("received %zd bytes: %s\n", nread, buf);
91            }
92 
93            exit(exit_success);
94        }

 come from url: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html