【SylixOS之ipv6收发测试代码】

1、设备ipv6地址通过ifconfig查看

【SylixOS之ipv6收发测试代码】_第1张图片

2、SylixOS自带ping6命令测试ipv6网络通断状态

注意:需要使用-I选项指定收发端口

【SylixOS之ipv6收发测试代码】_第2张图片

3、ipv6的tcp收发测试代码

注意:client端需要使用SO_BINDTODEVICE指定数据收发端口

server:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAXBUF 1024
#define LOCALPORT 8888

int main(int argc, char **argv)
{
    int                   i;
    int                   sockfd, new_fd;
    socklen_t             len;
    struct sockaddr_in6   my_addr, their_addr;
    char buf[MAXBUF + 1];


    if ((sockfd = socket(PF_INET6, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    } else {
        printf("socket created\n");
    }
    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin6_family         = AF_INET6;
    my_addr.sin6_port           = htons(LOCALPORT);
    my_addr.sin6_addr           = in6addr_any;

    if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in6)) == -1) {
        perror("bind");
        exit(1);
    } else {
        printf("binded\n");
    }

    if (listen(sockfd, 10) == -1) {
        perror("listen");
        exit(1);
    } else {
        printf("begin listen\n");
    }

    len = sizeof(struct sockaddr);
    if ((new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &len)) == -1) {
        perror("accept");
        exit(errno);
    }

    i = 0;
    while (1) {
        bzero(buf, sizeof(buf));
        len = recv(new_fd, buf, MAXBUF, 0);
        if (len <= 0) {
            perror("recv");
            exit(-1);
        }
        printf("recv:%d\n", i++);

        len = send(new_fd, buf, MAXBUF, 0);
        if (len <= 0) {
            perror("send");
            exit(-1);
        }
    }

    close(sockfd);
    return 0;
}

client:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#define MAXBUF 1024

#define REMOTEPORT 8888
//#define REMOTEIP "::1"
//#define REMOTEIP "fe80::204:9fff:fea0:45"
//#define REMOTEIP "fe80::20c:29ff:fe31:674c"
//#define REMOTEIP "fe80::2e0:cff:fe07:9b8a"
#define REMOTEIP "fe80::204:9fff:fea0:45"

int main(int argc, char **argv)
{
    int                  sockfd, len, i;
    struct sockaddr_in6  dest;
    char                 buffer[MAXBUF];
    char                 recvBuf[MAXBUF + 1];
    struct ifreq         ifreq;

    if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
        perror("Socket");
        exit(errno);
    }
    printf("socket created\n");

    strlcpy(ifreq.ifr_name, "en2", 16);                                /*  绑定指定的设备              */
    setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifreq, sizeof(ifreq));

    bzero(&dest, sizeof(dest));
    dest.sin6_family   = AF_INET6;
    dest.sin6_port     = htons(REMOTEPORT);
    inet_pton(AF_INET6, REMOTEIP, &dest.sin6_addr);
    printf("address created\n");

    if (connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) != 0) {
        perror("Connect ");
        exit(errno);
    }
    printf("server connected\n");


    for (i = 0; i < MAXBUF; i++) {
        buffer[i] = '0' + i % 10;
    }

    i = 0;
    while (1) {
        len = send(sockfd, buffer, MAXBUF, 0);
        if (len < 0) {
            perror("send");
        }

        bzero(recvBuf, sizeof(recvBuf));
        len = recv(sockfd, recvBuf, MAXBUF, 0);
        if (len < 0) {
            perror("send");
        }
        printf("recv %d:", i);
        printf("%s", recvBuf);
        printf("\n");

        sleep(1);
    }

    close(sockfd);
    return 0;
}

4、ipv6的udp收发测试代码

注意:client端需要使用SO_BINDTODEVICE指定数据收发端口

server:

#include 
#include 
#include 
#include 
#include 
#include 

#define LOCALPORT 8888

int main(int argc,char *argv[])
{
    int                    mysocket,len;
    int                    i               = 0;
    struct sockaddr_in6    addr;
    int                    addr_len;
    char                   msg[1024];
    char                   buf[300];

    if ((mysocket = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
        perror("error:");
        return(1);
    } else {
        printf("socket created ...\n");
        printf("socket id :%d \n",mysocket);
    }

    addr_len                 = sizeof(struct sockaddr_in6);
    bzero(&addr,sizeof(addr));
    addr.sin6_family         = AF_INET6;
    addr.sin6_port           = htons(LOCALPORT);
    addr.sin6_addr           = in6addr_any;

    if (bind(mysocket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("connect");
        return(1);
    } else {
        printf("bink ok .\n");
        printf("local port : %d\n", LOCALPORT);
    }
    i = 0;
    while(1) {
        bzero(msg,sizeof(msg));
        len = recvfrom(mysocket, msg, sizeof(msg), 0, (struct sockaddr *)&addr, (socklen_t*)&addr_len);
        inet_ntop(AF_INET6, &addr.sin6_addr, buf, sizeof(buf));
        printf("%d:message from ip %s\n", i++, buf);
        if (sendto(mysocket, msg, len, 0, (struct sockaddr *)&addr, addr_len) < 0) {
            printf("error");
            return(1);
        }
    }
}

client:

#include 
#include 
#include 
#include 
#include 
#include 
#include 


#define REMOTEPORT 8888
//#define REMOTEIP "::1"
//#define REMOTEIP "fe80::204:9fff:fea0:45"
//#define REMOTEIP "fe80::20c:29ff:fe31:674c"
//#define REMOTEIP "fe80::2e0:cff:fe07:9b8a"
#define REMOTEIP "fe80::56b2:3ff:fe09:5fc3"


int main(int argc,char *argv[])
{
    int                    mysocket, len;
    int                    i                = 0;
    struct sockaddr_in6    addr;
    int                    addr_len;
    char                   msg[1024];
    char                   msgr[1024];
    struct ifreq           ifreq;

    if ((mysocket = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
        perror("error:");
        return(1);
    } else {
        printf("socket created ...\n");
        printf("socket id :%d \n",mysocket);
        printf("rmote ip : %s\n",REMOTEIP);
        printf("remote port :%d \n",REMOTEPORT);
    }

    strlcpy(ifreq.ifr_name, "en1", 16);                                /*  绑定指定的设备              */
    setsockopt(mysocket, SOL_SOCKET, SO_BINDTODEVICE, &ifreq, sizeof(ifreq));

    addr_len           = sizeof(struct sockaddr_in6);
    bzero(&addr, sizeof(addr));
    addr.sin6_family   = AF_INET6;
    addr.sin6_port     = htons(REMOTEPORT);
    inet_pton(AF_INET6, REMOTEIP, &addr.sin6_addr);


    for (i = 0; i < 1024; i++) {
        msg[i] = '0' + i % 10;
    }

    i = 0;
    while(1) {
        if (sendto(mysocket, msg, sizeof(msg), 0, (struct sockaddr *)&addr, addr_len) < 0) {
            printf("error\n");
            return(1);
        }

        bzero(msgr, sizeof(msgr));
        len = recvfrom(mysocket, msgr, sizeof(msgr), 0, (struct sockaddr *)&addr, (socklen_t*)&addr_len);
        printf("%d: Received message\n", i++);

        sleep(1);
    }
}

 

你可能感兴趣的:(SylixOS)