linux网络编程IPv6socket,简单的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工具抓包可得:
網絡編程
具體的編程細節不解釋了,代碼測試通過......
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;
}
總結
以上是生活随笔為你收集整理的linux网络编程IPv6socket,简单的IPv6 UDP/TCP socket编程 -- 两台Linux实现简单的ipv6通信...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux目录挂载到内存,Linux中内
- 下一篇: linux误删视频恢复吗,linux 误