Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在C中创建基本套接字连接,并在accept()上运行到无限循环中_C_Sockets_Http - Fatal编程技术网

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

尝试在C中创建基本套接字连接,并在accept()上运行到无限循环中,c,sockets,http,C,Sockets,Http,我有一个任务,需要创建一个简单的HTTP服务器来处理GET请求,并从保存此代码可执行文件的目录中的目录返回信息。在整理HTTP请求之前,我正在尝试在套接字之间建立连接。但是,当我尝试使用accept()将客户端连接到服务器时,它会触发一个无限循环,gdb显示以下消息: ../sysdeps/unix/sysv/linux/accept.c:26 26 ../sysdeps/unix/sysv/linux/accept.c: No such file or directory. ../s

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

../sysdeps/unix/sysv/linux/accept.c:26 26 ../sysdeps/unix/sysv/linux/accept.c: No such file or directory. ../sysdeps/unix/sysv/linux/accept.c:26 26../sysdeps/unix/sysv/linux/accept.c:没有这样的文件或目录。
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
如果(argc>1){
perror(“错误,不应该有命令行参数”);
出口(0);
}
int-sockfd=0;
int clientfd=0;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))
accept()
在失败时返回-1。如果
if
将把任何非零值视为
true
条件

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

//设置侦听套接字。。。
printf(“插座正在等待连接”);
而(1){

sockSize=sizeof(client);//什么无限循环?你有一个循环,你忘了在消息末尾添加
\n
,所以即使我在while循环中添加打印后退出(0),可执行文件仍然是无限循环,即使accept是独立编写的。
while((clientfd=accept(…)
即使在
accept()
失败并返回-1时,也将计算为“true”并进入循环。请注意,0是有效的fd值,可由
accept()
返回。包括
err.h
并更改
printf(…)
向a
发出警告,查看失败的原因。仔细看,您的程序似乎有其逻辑倒退。它应该是(;){if((client_fd=accept(…)==-1)fail_或_break;…否则(fork and)在client_fd上处理HTTP请求…;close(client_fd)}
(或没有(;)的
如果要处理单个客户端)。
#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 %d\n", 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;
}