纯IPv4和纯IPv6交互的代码demo
时间: 2024-05-07 20:19:24 浏览: 100
纯IPv4和纯IPv6交互的代码demo可以分为两部分:IPv4服务器与IPv6客户端的交互和IPv6服务器与IPv4客户端的交互。
1. IPv4服务器与IPv6客户端的交互
IPv4服务器端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int server_sockfd, client_sockfd;
socklen_t server_len, client_len;
struct sockaddr_in server_address, client_address;
char buf[1024];
// 创建IPv4套接字
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 命名套接字
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
// 创建监听队列
listen(server_sockfd, 5);
while (1) {
printf("server waiting\n");
// 接受连接
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
// 读取数据
read(client_sockfd, buf, sizeof(buf));
printf("received: %s\n", buf);
// 发送数据
write(client_sockfd, "hello, world\n", 13);
// 关闭连接
close(client_sockfd);
}
return 0;
}
```
IPv6客户端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int sockfd;
socklen_t len;
struct sockaddr_in6 address;
int result;
char buf[1024];
// 创建IPv6套接字
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
// 初始化IPv6地址
memset(&address, 0, sizeof(address));
address.sin6_family = AF_INET6;
address.sin6_port = htons(9734);
inet_pton(AF_INET6, "::1", &address.sin6_addr);
// 连接服务器
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if (result == -1) {
perror("oops: client");
exit(1);
}
// 发送数据
write(sockfd, "hello, server", 13);
// 读取数据
read(sockfd, buf, sizeof(buf));
printf("received: %s\n", buf);
// 关闭连接
close(sockfd);
return 0;
}
```
2. IPv6服务器与IPv4客户端的交互
IPv6服务器端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int server_sockfd, client_sockfd;
socklen_t server_len, client_len;
struct sockaddr_in6 server_address, client_address;
char buf[1024];
// 创建IPv6套接字
server_sockfd = socket(AF_INET6, SOCK_STREAM, 0);
// 命名套接字
server_address.sin6_family = AF_INET6;
server_address.sin6_addr = in6addr_any;
server_address.sin6_port = htons(9734);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
// 创建监听队列
listen(server_sockfd, 5);
while (1) {
printf("server waiting\n");
// 接受连接
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
// 读取数据
read(client_sockfd, buf, sizeof(buf));
printf("received: %s\n", buf);
// 发送数据
write(client_sockfd, "hello, world\n", 13);
// 关闭连接
close(client_sockfd);
}
return 0;
}
```
IPv4客户端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int sockfd;
socklen_t len;
struct sockaddr_in address;
int result;
char buf[1024];
// 创建IPv4套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 初始化IPv4地址
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(9734);
inet_pton(AF_INET, "127.0.0.1", &address.sin_addr);
// 连接服务器
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if (result == -1) {
perror("oops: client");
exit(1);
}
// 发送数据
write(sockfd, "hello, server", 13);
// 读取数据
read(sockfd, buf, sizeof(buf));
printf("received: %s\n", buf);
// 关闭连接
close(sockfd);
return 0;
}
```
阅读全文