• linux_c_udp_example


    udp_server

     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include <unistd.h>
     4 #include <arpa/inet.h>
     5 #include <sys/socket.h>
     6 #include <stdio.h>
     7 
     8 #define BUF_SIZE 30
     9 
    10 void err_handler(char *msg);
    11 
    12 int main(int argc, char **argv) {
    13 
    14     int srv_sock;
    15     char msg[BUF_SIZE];
    16     int str_len;
    17     socklen_t cli_adr_sz;
    18 
    19     struct sockaddr_in srv_addr, cli_addr;
    20     if (argc != 2) {
    21         printf("Usage : %s <port>
    ", argv[0]);
    22         exit(1);
    23     }
    24     srv_sock = socket(PF_INET, SOCK_DGRAM, 0);
    25     if (srv_sock == -1) {
    26         err_handler("UDP socket() error");
    27     }
    28     memset(&srv_addr, 0, sizeof(srv_addr));
    29     srv_addr.sin_family = AF_INET;
    30     srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    31     srv_addr.sin_port = htons(atoi(argv[1]));
    32 
    33     if (bind(srv_sock, (struct sockaddr *) &srv_addr, sizeof(srv_addr)) == -1) {
    34         err_handler("bind() error");
    35     }
    36 
    37     while (1) {
    38         cli_adr_sz = sizeof(cli_addr);
    39         str_len = recvfrom(srv_sock, msg, BUF_SIZE, 0, (struct sockaddr *) &cli_addr, &cli_adr_sz);
    40         sendto(srv_sock, msg, str_len, 0, (struct sockaddr *) &cli_addr, cli_adr_sz);
    41         printf("recv and send ...
    ");
    42     }
    43 
    44     close(srv_sock);
    45     return 0;
    46 }
    47 
    48 void err_handler(char *msg) {
    49     fputs(msg, stderr);
    50     fputc('
    ', stderr);
    51     exit(1);
    52 }
    View Code

    udp_client

     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include <unistd.h>
     4 #include <arpa/inet.h>
     5 #include <sys/socket.h>
     6 #include <stdio.h>
     7 
     8 #define BUF_SIZE 30
     9 
    10 void err_handler(char *msg);
    11 
    12 int main(int argc, char **argv) {
    13 
    14     int sock;
    15     char msg[BUF_SIZE];
    16     int str_len;
    17     socklen_t adr_sz;
    18 
    19     struct sockaddr_in srv_addr, from_addr;
    20     if (argc != 3) {
    21         printf("Usage : %s <IP> <PORT>
    ", argv[0]);
    22         exit(1);
    23     }
    24 
    25     sock = socket(PF_INET, SOCK_DGRAM, 0);
    26     if (sock == -1) {
    27         err_handler("socket() error");
    28     }
    29 
    30     memset(&srv_addr, 0, sizeof(srv_addr));
    31     srv_addr.sin_family = AF_INET;
    32     srv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    33     srv_addr.sin_port = htons(atoi(argv[2]));
    34 
    35     while (1) {
    36         fputs("Input msg(Q to quit):", stdout);
    37         fgets(msg, BUF_SIZE, stdin);
    38         if (!strcmp(msg, "q
    ") || !strcmp(msg, "Q
    ")) {
    39             break;
    40         }
    41         sendto(sock, msg, strlen(msg), 0, (struct sockaddr *) &srv_addr, sizeof(srv_addr));
    42         adr_sz = sizeof(from_addr);
    43         str_len = recvfrom(sock, msg, BUF_SIZE, 0, (struct sockaddr *) &srv_addr, &adr_sz);
    44         msg[str_len] = 0;
    45         printf("Msg form srv : %s", msg);
    46     }
    47     close(sock);
    48     return 0;
    49 }
    50 
    51 void err_handler(char *msg) {
    52     fputs(msg, stderr);
    53     fputc('
    ', stderr);
    54     exit(1);
    55 }
    View Code

     build

    gcc -o usrv udp_srv.c -g -Wall
    gcc -o ucli udp_cli.c -g -Wall

  • 相关阅读:
    javascript深入理解js闭包
    【STL源码学习】STL算法学习之一
    【STL源码学习】细品vector
    【C++深入浅出】智能指针之auto_ptr学习
    【字典树应用】联想用户最想输入的词汇
    【数据结构】非常有用的hash表
    【WIN32进阶之路】:内存映射文件
    【WIN32进阶之路】:线程同步技术纲要
    win32进阶必备:多线程同步之互斥锁
    【C++深入浅出】设计模式学习之单例模式
  • 原文地址:https://www.cnblogs.com/--just-lbk/p/11715044.html
Copyright © 2020-2023  润新知