UNIX Network programming: UDP lack of flow control (improved version)

Source: Internet
Author: User
Tags exit include sigint signal socket

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/

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.