我正在编写一个 POP3 服务器,出于某种奇怪的原因,它只能在我的计算机上运行,而没有其他人的。它只是以错误“传输端点未连接”退出。谷歌显示,大多数具有相同错误消息的问题是由传递套接字 id 而不是连接 id 来读取的,但我没有做任何类似的事情,我实际上是要检查它们是否不同,这是真的我尝试运行它的时间。
void pop_protocol(int connection_descriptor){
pthread_t tid;
pthread_create(&tid, NULL, pop_worker, (void *) &connection_descriptor);
}
void *pop_worker(void *cd) {
int connection_descriptor = *((int *) cd);
socket_write(connection_descriptor, "+OK POP3 server ready\r\n");
}
int sockfd = socket_setup(portno);
while (1) {
int connection = socket_get_new_connection(sockfd);
pop_protocol(connection);
}
int socket_setup(int portno){
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
fprintf(stderr, "Error: Cannot create socket\n");
perror("");
exit(1);
}
struct sockaddr_in address;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
address.sin_port = htons(portno);
if (bind(fd, (struct sockaddr *) &address, sizeof(address)) == -1) {
fprintf(stderr, "Error: Cannot bind socket to port %d\n", portno);
perror("");
exit(1);
}
if (listen(fd, MAXCONNECTION) == -1) {
fprintf(stderr, "Error: Cannot listen on port\n");
perror("");
exit(1);
}
return fd;
}
int socket_get_new_connection(int sockfd){
struct sockaddr_in address;
socklen_t addrlen = sizeof(struct sockaddr_in);
int new_socket = -1;
int count = 0;
while (new_socket < 0 && count < ACCEPTCOUNTER) {
new_socket = accept(sockfd, (struct sockaddr *) &address, &addrlen);
}
// Attempt timed out. Exit
if (count == ACCEPTCOUNTER) {
fprintf(stderr, "Error: Cannot accept new connections");
perror("");
exit(1);
}
return new_socket;
}
如果有人能对这个问题提供任何见解,我将不胜感激。