Now we look at the impact of UDP on datagram transmission without any traffic control. First we modify the DG_CLI function to send a fixed number of datagrams and no longer read from the standard input. As follows, it writes 2000 UDP datagrams of 1400 bytes in size to the server.
Client program CLI.C:
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #includ
E <string.h> #define Serv_port 3333 #define MAXLINE 1024 #define ERR_EXIT (m) \ do \ {\ Perror (m); \ exit (exit_failure);
\} while (0) typedef struct SOCKADDR SA; #define NDG/* Datagrams to send */#define DGLEN 1400/* length of each datagram/void Dg_c
Li (FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) {int i;
Char Sendline[dglen];
for (i = 0; i < ndg i++) {sendto (SOCKFD, Sendline, Dglen, 0, pservaddr, Servlen);
int main (int argc, char **argv) {int sockfd;
struct sockaddr_in servaddr;
if (argc!= 2) Err_exit ("Usage:udpcli <IPaddress>");
Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port = htons (Serv_port);
Inet_pton (Af_inet, argv[1], &servaddr.sin_addr);
SOCKFD = socket (af_inet, SOCK_DGRAM, 0);
DG_CLI (stdin, SOCKFD, (SA *) &servaddr, sizeof (SERVADDR));
Exit (0); }
We then modify the server program to accept the datagram and count it, and no longer send the datagram back to the customer. The following is the new Dg_echo function. When we terminate the server with a terminal break key (equivalent to sending it a SIGINT signal), the server displays the number of datagrams received and terminates.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/