IPv4客户端和IPv6服务器端通信的c语言代码demo
时间: 2023-10-02 13:07:07 浏览: 160
客户端和服务器通信C语言源代码
以下是一个简单的示例代码,展示了如何使用C语言在IPv4客户端和IPv6服务器端之间进行通信:
IPv4客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT "8080"
int main(int argc, char *argv[]) {
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
char buf[1024];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo("localhost", PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
printf("client: connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
while (1) {
printf("Enter message to send: ");
fgets(buf, sizeof(buf), stdin);
if (send(sockfd, buf, strlen(buf), 0) == -1) {
perror("send");
exit(1);
}
printf("Waiting for server response...\n");
int numbytes;
if ((numbytes = recv(sockfd, buf, sizeof(buf), 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Server response: %s\n", buf);
}
close(sockfd);
return 0;
}
```
IPv6服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT "8080"
void *get_in_addr(struct sockaddr *sa) {
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(void) {
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
int yes = 1;
char s[INET6_ADDRSTRLEN];
char buf[1024];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed to bind\n");
return 2;
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, 10) == -1) {
perror("listen");
exit(1);
}
printf("server: waiting for connections...\n");
while (1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s\n", s);
while (1) {
int numbytes;
if ((numbytes = recv(new_fd, buf, sizeof(buf), 0)) == -1) {
perror("recv");
exit(1);
}
if (numbytes == 0) {
printf("Connection closed\n");
break;
}
buf[numbytes] = '\0';
printf("Client message: %s", buf);
if (send(new_fd, buf, numbytes, 0) == -1) {
perror("send");
exit(1);
}
}
close(new_fd); // parent doesn't need this
}
return 0;
}
```
注意:在IPv6服务器端代码中,我们使用了一个名为`get_in_addr()`的函数,它将IPv4和IPv6地址转换为通用的地址格式。在IPv4情况下,它返回`sin_addr`字段,而在IPv6情况下,它返回`sin6_addr`字段。
阅读全文