试图在C中创建一个基本的套接字连接,并在accept()上运行到一个无限循环中



我有一个任务,需要创建一个简单的HTTP服务器来处理GET请求,并从保存该代码可执行文件的目录中的一个目录返回信息。在处理HTTP请求之前,我正在尝试在套接字之间建立连接。然而,当我尝试使用accept()将客户端连接到服务器时,它会触发一个无限循环,gdb会显示以下消息:

/sysdeps/unix/sysv/linux/accept.c:2626/sysdeps/unix/sysv/linux/accept.c:没有这样的文件或目录
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
int main(int argc, char* argv[]){
if(argc>1){
perror("Error there should be no command line arguments");
exit(0);
}
int sockfd = 0;
int clientfd = 0;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0){ //create socket and check for error
perror("Error in socket creation");
exit(0);
}
//create sockaddr object to hold info about the socket
struct sockaddr_in server, client;
server.sin_family = AF_INET;
server.sin_port = 0;
server.sin_addr.s_addr = htonl(INADDR_ANY);
socklen_t sockSize = sizeof(server);
//Bind the socket to a physical address exit if there is an error
if((bind(sockfd, (struct sockaddr*)&server, sockSize))<0){
perror("Error binding socket");
exit(0);
}
//Check server details
printf("-------Server Details----------n");
printf("Port number %d | IP ADDRESS %dn", ntohs(server.sin_port), (getsockname(sockfd, (struct sockaddr*)&server, &sockSize)));
if((getsockname(sockfd, (struct sockaddr*)&server, &sockSize)) <0){
perror("There is an error in the sock");
exit(0);
}
if(listen(sockfd, 5) <0){
perror("Error switching socket to listen");
exit(0);
}
while((clientfd = accept(sockfd, (struct sockaddr*)&client, (socklen_t*)&sockSize))){
printf("Socket is awaiting connections");
}
// figure out how to setup client to accept and submit HTTP requests
close(sockfd);
return 0;
}

accept()在失败时返回-1。if任何非零值视为true条件。

您的循环应该更像以下内容:

// setup listening socket...
printf("Socket is awaiting connections");
while (1) {
sockSize = sizeof(client); // <-- add this
if ((clientfd = accept(sockfd, (struct sockaddr*)&client, (socklen_t*)&sockSize)) < 0) {
if (errno != EINTR) {
// fatal error, bail out...
break;
}
continue; // retry...
}
printf("Client connected");
// use clientfd to read HTTP request and send HTTP response...
close(clientfd);
}

相关内容

最新更新