简单的IPv6 UDP/TCP socket编程 -- 两台Linux实现简单的ipv6通信

配置:

1.两台linux用网线直接相连

2.手动配置两台linux的ipv6地址为:

ifconfig eth0 add 2001:da8:e000::1:1:1

ifconfig eth0 add 2001:da8:e000::1:1:2

如下所示:

[root@EmbedSky /]# ifconfig
eth0      Link encap:Ethernet  HWaddr 10:23:45:67:89:AB  
          inet addr:192.168.142.21  Bcast:192.168.142.255  Mask:255.255.255.0
          inet6 addr: 2001:da8:e000::1:1:1/0 Scope:Global       //手动添加的地址
          inet6 addr: fe80::1223:45ff:fe67:89ab/64 Scope:Link //ipv6中的Link地址相当于ipv中169...私有地址
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:339 errors:0 dropped:0 overruns:0 frame:0
          TX packets:141 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:39003 (38.0 KiB)  TX bytes:9530 (9.3 KiB)
          Interrupt:51 Base address:0x4000 

3.接下来用ping6地址来测试:

[root@EmbedSky /]# ping6 2001:da8:e000::1:1:2
PING 2001:da8:e000::1:1:2 (2001:da8:e000::1:1:2): 56 data bytes
64 bytes from 2001:da8:e000::1:1:2: seq=0 ttl=64 time=4.788 ms
64 bytes from 2001:da8:e000::1:1:2: seq=1 ttl=64 time=1.268 ms
4.wireshark工具抓包可得:

简单的IPv6 UDP/TCP socket编程 -- 两台Linux实现简单的ipv6通信_第1张图片

网络编程

具体的编程细节不解释了,代码测试通过......

UDP通信

server端:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char **argv)
{
	struct sockaddr_in6 s_addr;
	struct sockaddr_in6 c_addr;
	int sock;
	socklen_t addr_len;
	int len;
	char buff[128];
	char buf_ip[128];

	if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		exit(errno);
	} else
		printf("create socket.\n\r");

	memset(&s_addr, 0, sizeof(struct sockaddr_in6));
	s_addr.sin6_family = AF_INET6;

	if (argv[2])
		s_addr.sin6_port = htons(atoi(argv[2]));
	else
		s_addr.sin6_port = htons(4444);

	if (argv[1])
		inet_pton(AF_INET6, argv[1], &s_addr.sin6_addr);
	else
		s_addr.sin6_addr = in6addr_any;

	if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) {
		perror("bind");
		exit(errno);
	} else
		printf("bind address to socket.\n\r");

	addr_len = sizeof(c_addr);
	while (1) {
		len = recvfrom(sock, buff, sizeof(buff) - 1, 0,
					   (struct sockaddr *) &c_addr, &addr_len);
		if (len < 0) {
			perror("recvfrom");
			exit(errno);
		}

		buff[len] = '\0';
		printf("receive from %s: buffer:%s\n\r",
				inet_ntop(AF_INET6, &c_addr.sin6_addr, buf_ip, sizeof(buf_ip)), 
				buff);
	}
	return 0;
}

client端:

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

int main(int argc, char **argv)
{
	struct sockaddr_in6 s_addr;
	int sock;
	int addr_len;
	int len;
	char buff[128];

	if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		exit(errno);
	} else
		printf("create socket.\n\r");

	s_addr.sin6_family = AF_INET6;
	if (argv[2])
		s_addr.sin6_port = htons(atoi(argv[2]));
	else
		s_addr.sin6_port = htons(4444);

	if (argv[1])
		inet_pton(AF_INET6, argv[1], &s_addr.sin6_addr);
	else {
		printf("usage:./command ip port\n");
		exit(0);
	}

	addr_len = sizeof(s_addr);
	strcpy(buff, "hello i'm here");
	len = sendto(sock, buff, strlen(buff), 0,
				 (struct sockaddr *) &s_addr, addr_len);
	if (len < 0) {
		printf("\n\rsend error.\n\r");
		return 3;
	}

	printf("send success.\n\r");
	return 0;
}

测试如下:

server端:

[root@zx bacnet-router]# ./server 2001:da8:e000::1:1:2 4444
create socket.
bind address to socket.
receive from 2001:da8:e000::1:1:1: buffer:hello i'm here
receive from 2001:da8:e000::1:1:1: buffer:hello i'm here
receive from 2001:da8:e000::1:1:2: buffer:hello i'm here
receive from 2001:da8:e000::1:1:2: buffer:hello i'm here

client端:

[root@zx bacnet-router]# ./client 2001:da8:e000::1:1:2 4444
create socket.
send success.
[root@zx bacnet-router]# ./client 2001:da8:e000::1:1:2 4444
create socket.
send success.
[root@zx bacnet-router]# 

[root@EmbedSky /bacnet-router]# ./arm-client 2001:da8:e000::1:1:2 4444
create socket.
send success.
[root@EmbedSky /bacnet-router]# ./arm-client 2001:da8:e000::1:1:2 4444
create socket.
send success.
[root@EmbedSky /bacnet-router]# 

TCP通信:

server端:

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

#define MAXBUF 1024

int main(int argc, char **argv)
{
    int sockfd, new_fd;
    socklen_t len;

    /* struct sockaddr_in my_addr, their_addr; */ // IPv4
    struct sockaddr_in6 my_addr, their_addr; // IPv6

    unsigned int myport, lisnum;
    char buf[MAXBUF + 1];

    if (argv[1])
        myport = atoi(argv[1]);
    else
        myport = 7838;

    if (argv[2])
        lisnum = atoi(argv[2]);
    else
        lisnum = 2;

    /* if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { */ // IPv4
    if ((sockfd = socket(PF_INET6, SOCK_STREAM, 0)) == -1) { // IPv6
        perror("socket");
        exit(1);
    } else
        printf("socket created\n");

    bzero(&my_addr, sizeof(my_addr));
    /* my_addr.sin_family = PF_INET; */ // IPv4
    my_addr.sin6_family = PF_INET6;    // IPv6
    /* my_addr.sin_port = htons(myport); */ // IPv4
    my_addr.sin6_port = htons(myport);   // IPv6
    if (argv[3])
        /* my_addr.sin_addr.s_addr = inet_addr(argv[3]); */ // IPv4
        inet_pton(AF_INET6, argv[3], &my_addr.sin6_addr);  // IPv6
    else
        /* my_addr.sin_addr.s_addr = INADDR_ANY; */ // IPv4
        my_addr.sin6_addr = in6addr_any;            // IPv6

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

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

    while (1) {
        len = sizeof(struct sockaddr);
        if ((new_fd =
             accept(sockfd, (struct sockaddr *) &their_addr,
                    &len)) == -1) {
            perror("accept");
            exit(errno);
        } else
            printf("server: got connection from %s, port %d, socket %d\n",
                   /* inet_ntoa(their_addr.sin_addr), */ // IPv4
                   inet_ntop(AF_INET6, &their_addr.sin6_addr, buf, sizeof(buf)), // IPv6
                   /* ntohs(their_addr.sin_port), new_fd); */ // IPv4
                   their_addr.sin6_port, new_fd); // IPv6

        bzero(buf, MAXBUF + 1);
        strcpy(buf, "This is sever to client!\n");
        len = send(new_fd, buf, strlen(buf), 0);
        if (len < 0) {
            printf
                ("message '%s' error number:%d,error mes:'%s'\n", buf, errno, strerror(errno));
        } else
            printf("message'%s'success length: %d\n", buf, len);

        bzero(buf, MAXBUF + 1);
        len = recv(new_fd, buf, MAXBUF, 0);
        if (len > 0)
            printf("receive message:'%s',length:%d\n", buf, len);
        else
            printf
                ("message error number:%d,error mes:'%s'\n", errno, strerror(errno));
    }

    close(sockfd);
    return 0;
}

client端:

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

#define MAXBUF 1024

int main(int argc, char **argv)
{
    int sockfd, len;
    /* struct sockaddr_in dest; */ // IPv4
    struct sockaddr_in6 dest;      // IPv6
    char buffer[MAXBUF + 1];

    if (argc != 3) {
        printf
            ("参数格式错误!正确用法如下:\n\t\t%s IP地址 端口\n\t比如:\t%s 127.0.0.1 80\n此程序用来从某个 IP 地址的服务器某个端口接收最多 MAXBUF 个字节的消息",
             argv[0], argv[0]);
        exit(0);
    }
    /* socket for tcp */
    /* if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { */ // IPv4
    if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {      // IPv6
        perror("Socket");
        exit(errno);
    }
    printf("socket created\n");

    /* 初始化服务器端(对方)的地址和端口信息 */
    bzero(&dest, sizeof(dest));
    /* dest.sin_family = AF_INET; */  // IPv4
    dest.sin6_family = AF_INET6;     // IPv6
    /* dest.sin_port = htons(atoi(argv[2])); */ // IPv4
    dest.sin6_port = htons(atoi(argv[2]));     // IPv6
    /* if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0) { */ // IPv4
    if ( inet_pton(AF_INET6, argv[1], &dest.sin6_addr) < 0 ) {                 // IPv6
        perror(argv[1]);
        exit(errno);
    }
    printf("address created\n");

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

    /* 接收对方发过来的消息,最多接收 MAXBUF 个字节 */
    bzero(buffer, MAXBUF + 1);
    /* 接收服务器来的消息 */
    len = recv(sockfd, buffer, MAXBUF, 0);
    if (len > 0)
        printf("接收消息成功:'%s',共%d个字节的数据\n",
               buffer, len);
    else
        printf
            ("消息接收失败!错误代码是%d,错误信息是'%s'\n",
             errno, strerror(errno));

    bzero(buffer, MAXBUF + 1);
    strcpy(buffer, "这是客户端发给服务器端的消息\n");
    /* 发消息给服务器 */
    len = send(sockfd, buffer, strlen(buffer), 0);
    if (len < 0)
        printf
            ("消息'%s'发送失败!错误代码是%d,错误信息是'%s'\n",
             buffer, errno, strerror(errno));
    else
        printf("消息'%s'发送成功,共发送了%d个字节!\n",
               buffer, len);

    /* 关闭连接 */
    close(sockfd);
    return 0;
}

你可能感兴趣的:(网络知识)