我有一个带有客户端和服务器的小程序。服务器启动,连接到客户端,并从客户端接收主机和第二个端口号,然后将一些有关其目录的信息发送回客户端。服务器中发送回数据的功能如下所示:
void sendStuff(char *host, char *port) {
int sockfd, numbytes;
struct addrinfo hints, *servinfo;
struct sockaddr_storage client_info;
socklen_t addr_size;
size_t i = 0;
int rv;
// clear hints
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
memset(&servinfo, 0, sizeof(servinfo));
if ((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
}
// create socket
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if (sockfd == -1) {
perror("There was an error creating your socket.");
close(sockfd);
exit(1);
}
// connect using socket
int conn;
conn = connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
if(conn == -1) {
perror("Error connecting to server.");
close(sockfd);
exit(1);
}
send(sockfd, "hey!", strlen("hey!"), 0);
}
当前正在通过本地主机进行测试,该本地主机在客户端上被打印为16.2.197.67。这似乎不是本地主机IP,所以我想我可能会以某种方式破坏主机。通过telnet连接到等待的套接字有效,但不能通过此功能进行。