ipv6 udp socket例項

yinshuisiquan發表於2020-11-10
#include <sys/socket.h>
#include <linux/types.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
 
static int serve_new_conn(int sk)
{
    int rd, wr;
    char recv_buf[1024];
    struct sockaddr_in6 client;
    int addr_len = sizeof(struct sockaddr_in6);
 
    printf("New connection\n");
 
    while (1) {
        rd = recvfrom(sk, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&client, &addr_len);
        if (!rd)
            break;
 
        if (rd < 0) {
            perror("Can't read socket");
            return 1;
        }

        recv_buf[rd]='\0';
 
        wr = 0;
        wr = sendto(sk, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&client, sizeof(client));
        if (wr <= 0) {
            perror("Can't write socket");
            return 1;
        }
    }
 
    printf("Done\n");
    return 0;
}
 
static int main_srv(int argc, char **argv)
{
    int sk, port, ret;
    struct sockaddr_in6 addr;
 
    /*
     * Let kids die themselves
     */
 
    signal(SIGCHLD, SIG_IGN);
 
    sk = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (sk < 0) {
        perror("Can't create socket");
        return -1;
    }
 
    port = atoi(argv[1]);
    memset(&addr, 0, sizeof(addr));
    addr.sin6_family = AF_INET6;
    addr.sin6_addr = in6addr_any;
    addr.sin6_port = htons(port);
 
    printf("Binding to port %d\n", port);
 
    ret = bind(sk, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0) {
        perror("Can't bind socket");
        return -1;
    }
 
    printf("Waiting for connections\n");
    ret = serve_new_conn(sk);
    exit(ret);
}
 
static int main_cl(int argc, char **argv)
{
    int sk, port, ret, val = 1, rval = 0, len = 0;
    struct sockaddr_in6 addr;
    int addr_len = sizeof(struct sockaddr_in6);
 
    sk = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (sk < 0) {
        perror("Can't create socket");
        return -1;
    }
 
    port = atoi(argv[2]);
    printf("Connecting to %s:%d\n", argv[1], port);
    memset(&addr, 0, sizeof(addr));
    addr.sin6_family = AF_INET6;
    ret = inet_pton(AF_INET6, argv[1], &addr.sin6_addr);
    if (ret <= 0) {
        perror("Can't convert addr");
        return -1;
    }
    addr.sin6_port = htons(port);
 
    ret = connect(sk, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0) {
        perror("Can't connect");
        return -1;
    }
 
    while (1) {
        len=sendto(sk, &val, sizeof(val), 0, (struct sockaddr *)&addr, sizeof(addr));
        if (len < 0) {
            perror("send data error\n");
            close(sk);
            exit(1);
        }

        len=recvfrom(sk, &rval, sizeof(rval), 0, (struct sockaddr *)&addr, &addr_len);
        if (len < 0) {
            perror("recv data error\n");
            close(sk);
            exit(1);
        }
        printf("PP %d -> %d\n", val, rval);
        sleep(2);
        val++;
    }
}
 
int main(int argc, char **argv)
{
    if (argc == 2)
        return main_srv(argc, argv);
    else if (argc == 3)
        return main_cl(argc, argv);
 
    printf("Bad usage\n");
    return 1;
}

相關文章