我从Jon Erickson的《黑客:剥削的艺术》一书中学习,我对他提供的一个简单的代码样本感到困惑。代码是设置一个简单的服务器,但是当我编译它(没有错误)并运行代码时,它挂起了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "hacking.h"
#define PORT 7890 // the port users will be connecting to
int main(void) {
int sockfd, new_sockfd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in host_addr, client_addr; // my address information
socklen_t sin_size;
int recv_length=1, yes=1;
char buffer[1024];
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
fatal("in socket");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
fatal("setting socket option SO_REUSEADDR");
host_addr.sin_family = AF_INET; // host byte order
host_addr.sin_port = htons(PORT); // short, network byte order
host_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(host_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1)
fatal("binding to socket");
if (listen(sockfd, 5) == -1)
fatal("listening on socket");
while(1) { // Accept loop
sin_size = sizeof(struct sockaddr_in);
new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
if(new_sockfd == -1)
fatal("accepting connection");
printf("server: got connection from %s port %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
send(new_sockfd, "Hello World!\n", 13, 0);
recv_length = recv(new_sockfd, &buffer, 1024, 0);
while(recv_length > 0) {
printf("RECV: %d bytes\n", recv_length);
dump(buffer, recv_length);
recv_length = recv(new_sockfd, &buffer, 1024, 0);
}
close(new_sockfd);
}
return 0;
}我做了一个小的printf()来找出我挂在哪里,结果它就在这一行上
sin_size = sizeof(struct sockaddr_in);我不确定这是与我的环境有关,还是我遗漏了什么。这本书使用的环境不能再更新了(一些旧版本的Ubuntu)。所以我现在用的是最新的。
有人能给我解释一下为什么这个程序不能工作吗?
如果在学习网络章节之前有一些需要了解的基本知识,请一定要告诉我。
发布于 2013-02-14 13:41:24
根据你的线程中所有的评论,我建议使用下面的命令行连接远程登录:telnet localhost 7890
telnet将此主机上要连接到的主机和要连接到的端口作为参数。使用"localhost“类似于使用环回IP 127.0.0.1。
为什么要连接到服务器来解决“挂起”问题?accept是阻塞的,因为您可以在man page或编程环境的任何其他文档中阅读。这意味着该函数在客户端连接之前不会返回。在连接之后,该函数返回一个句柄,指向为连接客户端创建的套接字,该套接字可用于通信。
发布于 2013-02-14 13:27:18
在sizeof接收到来自客户端程序的传入连接后,此程序将不会继续运行,直到在线上执行accept。您的printf显示accept被调用,但被阻塞。
您需要使用正确的选项(IP /端口)编译和运行客户端,才能连接到此服务器程序。
更新如果192.168.42.248来自书中,那么您可能尝试连接到错误的IP。试试telnet 127.0.0.1 7890。
发布于 2013-02-14 13:30:11
这是一台服务器,它将“挂起”,直到您连接到端口7890。这就是程序的全部意义所在(更多细节,由于accept()正在等待连接,它将被阻塞)
假设您正在运行unix,在运行它的同时,尝试在同一台机器上的终端中输入echo "hi there" | nc localhost 7890,您将看到它是如何“解锁”的。
https://stackoverflow.com/questions/14868297
复制相似问题