关闭

linux select()详解( 二)-- UDP最简实例

标签: selectudplinux
310人阅读 评论(0) 收藏 举报
分类:

通过本文你会了解到:
1. UDP server的实例
2. UDP client的实例
3. UDP server和client的运行测试

UDP server实例(server.c)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>

//macro
#define BUF_SIZE 1024 
#define PORT 1025 
#undef handle_error
#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while(0)

int main(int argc, char *argv[])
{
    struct sockaddr_in server_addr;
    fd_set read_fds;
    socklen_t len;
    char buf[BUF_SIZE];
    int server_fd = -1;
    int ret;

    if((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        handle_error("socket");

    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*any ip*/

    if(bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
        handle_error("bind");

    printf("accepting connections on port %d\n", PORT);

    while(1) {
        FD_ZERO(&read_fds);
        FD_SET(server_fd ,&read_fds);

        ret = select(server_fd + 1, &read_fds, NULL, NULL, NULL);

        if(ret == -1 && errno == EINTR)
            continue;

        if(ret == -1)
            handle_error("select");

        if(ret == 0) /*timeout, if you set timeout.*/
            continue;

        if(FD_ISSET(server_fd, &read_fds)) {
            memset(buf, 0, BUF_SIZE);
            len = sizeof(server_addr);
            if(recvfrom(server_fd, (void *)buf, BUF_SIZE, 0, (struct sockaddr *)&server_addr, &len) > 0) {
                printf("Receive packet from %s:%d\nData:%s\n", 
                        inet_ntoa(server_addr.sin_addr),
                        ntohs(server_addr.sin_port),
                        buf);
            } else {
                handle_error("recvfrom");
            }
        }
    }

    close(server_fd);

    return 0;
}

UDP client实例(client.c)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>

//macro
#define BUF_SIZE 1024 
#define PORT 1025 
#define SEND_PACK_CNT 3
#define SRV_IP "127.0.0.1" 
#undef handle_error
#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while(0)

int main(int argc, char *argv[])
{
    struct sockaddr_in server_addr;
    char buf[BUF_SIZE];
    int server_fd = -1;
    int i;


    if((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        handle_error("socket");

    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &server_addr.sin_addr) == 0)
        handle_error("inet_aton");

    for(i = 0; i < SEND_PACK_CNT; i++) {
        sprintf(buf, "Packet %d", i);
        sendto(server_fd, buf, strlen(buf) + 1, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
    }

    close(server_fd);

    return 0;
}

运行测试
编译
gcc server.c -o server
gcc client.c -o client

server端运行结果

#./server
accepting connections on port 1025
/*下列内容时运行client后,server端收到的数据包后的打印信息*/
Receive packet from 127.0.0.1:34833
Data:Packet 0
Receive packet from 127.0.0.1:34833
Data:Packet 1
Receive packet from 127.0.0.1:34833
Data:Packet 2
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13006次
    • 积分:513
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:0篇
    • 译文:3篇
    • 评论:1条
    文章分类
    最新评论