linux c例子 - 网络编程(2) - udp

服务端

#include <stdio.h>          // printf perror
#include <sys/socket.h>     // socket 

#include <netinet/in.h>
#include <arpa/inet.h>

static void help()
{
    printf("./a.out port\n");
}

static void print_sockaddr(struct sockaddr_in *addr)
{
    in_port_t       port;
    char            buf[INET_ADDRSTRLEN], *ip;
    
    port = ntohs(addr->sin_port);
    
    if (inet_ntop(AF_INET, &addr->sin_addr, buf, INET_ADDRSTRLEN) == NULL) {
        perror("inet_ntop");
    }
    
    printf("from %s:%d - ", buf, port);
    
}

int main(int argc, char **argv)
{
    int                 n, rc, fd, afd, flag;
    unsigned short      port;
    char                buf[16];
    socklen_t           slen;
    
    struct sockaddr_in  ser_addr, cli_addr;
    
    if (argc != 2) {
        help();
        return -1;
    }
    
    port = atoi(argv[1]);
    
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }
    
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(port);
    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    rc = bind(fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    if (rc == -1) {
        perror("bind");
        return -1;
    }
    
    slen = sizeof(cli_addr);
    
    while (1) {
        
        n = recvfrom(fd, buf, 15, 0, (struct sockaddr *)&cli_addr, &slen);
        if (n <= 0) {
            perror("recvfrom");
            continue;
        }
        
        buf[n] = 0;
        
        print_sockaddr(&cli_addr);
        
        printf("%s\n", buf);
        
        n = sendto(fd, "got", 3, 0, (struct sockaddr *)&cli_addr, slen);
        if (n <= 0) {
            perror("sendto");
            continue;
        }
    
    }
    
}


客户端

#include <stdio.h>          // printf perror
#include <sys/socket.h>     // socket 

#include <netinet/in.h>
#include <arpa/inet.h>

static void help()
{
    printf("./a.out ip port\n");
}

int main(int argc, char **argv)
{
    int             fd, n;
    in_port_t       port;
    char            *ip;
    char            buf[16];
    
    struct sockaddr_in      ser_addr;
    
    if (argc != 3) {
        help();
        return -1;
    }
    
    ip = argv[1];
    port = atoi(argv[2]);
    
    if (strcmp(ip, "localhost") == 0) {
        ip = "127.0.0.1";
    }
    
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }
    
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(port);
    ser_addr.sin_addr.s_addr = inet_addr(ip);
    
    /* udp 没有连接一说,调用connect只是绑定,可以使用 send,recv
     * 否则只能用 sendto,recvfrom
    */
    if (connect(fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) == -1) {
        perror("connect");
        return -1;
    }
    
    n = send(fd, "hello", 5, 0);
    if (n <= 0) {
        perror("send");
        return -1;
    }
    
    n = recv(fd, buf, 15, 0);
    if (n <= 0) {
        perror("recv");
        return -1;
    }
    
    buf[n] = 0;
    
    printf("recv: %s\n", buf);
    
    close(fd);
    
    return 0;
}




上一篇: linux c例子 - 网络编程(1) - tcp
下一篇: linux c例子 - 网络编程(3) - unix domain - 流
作者邮箱: 203328517@qq.com