Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 在子进程中打开文件_C_File_Tcp_Fork - Fatal编程技术网

C 在子进程中打开文件

C 在子进程中打开文件,c,file,tcp,fork,C,File,Tcp,Fork,我正在做一个tcp服务器,客户端文件传输程序。客户端将从服务器请求文件名。服务器将返回文件的内容。但当我给出一个不存在的文件名时,它应该给出一个文件不存在的错误。但服务器只是挂起。它不打印任何东西。我在没有使用fork()系统调用的情况下尝试了相同的程序,结果成功了。请帮我解决这个问题 客户端程序: #include <sys/types.h> #include <sys/socket.h> #include <strings.h> #include <

我正在做一个tcp服务器,客户端文件传输程序。客户端将从服务器请求文件名。服务器将返回文件的内容。但当我给出一个不存在的文件名时,它应该给出一个文件不存在的错误。但服务器只是挂起。它不打印任何东西。我在没有使用fork()系统调用的情况下尝试了相同的程序,结果成功了。请帮我解决这个问题

客户端程序:

#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

/* Server */
int main()
{
// Create the structure
struct sockaddr_in server;

// Create a socket
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

// Initialize the structure
bzero(&server, sizeof(server));

server.sin_port = htons(7008);
server.sin_family = AF_INET;
inet_aton("127.0.0.1", &server.sin_addr);

// Connect to server
connect(sockfd, (struct sockaddr*)&server, sizeof(server));

char msg[256];      // 256 bytes of data

strcpy(msg, "myfile"); // Here if myfile is a file
                       // that doesnot exist 
                       // Server is supposed to return an error

// Send file name to file
send(sockfd, msg, strlen(msg) + 1, 0);
// Receive data
printf("Waiting for data: \n");
recv(sockfd, msg, 256, 0);
printf("%s", msg);
close(sockfd);
} 
#包括
#包括
#包括
#包括
#包括
#包括
/*服务器*/
int main()
{
//创建结构
服务器中的结构sockaddr_;
//创建一个套接字
int sockfd=套接字(AF_INET、SOCK_流、IPPROTO_TCP);
//初始化结构
bzero(&server,sizeof(server));
server.sin_port=htons(7008);
server.sinu family=AF\u INET;
inet_aton(“127.0.0.1”和server.sin_addr);
//连接到服务器
连接(sockfd,(struct sockaddr*)和服务器,sizeof(服务器));
char msg[256];//256字节的数据
strcpy(msg,“myfile”);//如果myfile是一个文件,则在此处
//那根本不存在
//服务器应该返回一个错误
//将文件名发送到文件
发送(sockfd,msg,strlen(msg)+1,0);
//接收数据
printf(“等待数据:\n”);
recv(sockfd,msg,256,0);
printf(“%s”,msg);
关闭(sockfd);
} 
服务器程序:

#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

/* Server */
int main()
{
    // Create the structure
    struct sockaddr_in server, client;

    // Create a socket
    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    // Initialize the structure
    bzero(&server, sizeof(server));

    server.sin_port = htons(7008);
    server.sin_family = AF_INET;
    inet_aton("127.0.0.1", &server.sin_addr);

    // Bind socket to port
    bind(sockfd, (struct sockaddr*)&server, sizeof(server));

    // Listen for connection
    listen(sockfd, 5);

    while(1)
    {
        int client_length = sizeof(client);
        int nsockfd = accept(sockfd, (struct sockaddr*)&client, &client_length);

        char msg[256];      // 256 bytes of data
        int len;

        // Create a child process
        if(fork() == 0)
        {

            len = recv(nsockfd, msg, 256, 0);
            printf("Received %s\n", msg);
            char filename[40];

            // Generate the filename
            strcpy(filename,"./files/");
            strcat(filename,msg);
            printf("File to open %s", filename);

            // File pointer
            FILE *fp;

                // Server should return "File not found"
                    // But it simply hangs there
                    // It doesnt show any response
            if(!(fp = fopen(filename, "r")))
            {
                printf("Error opening file");
                strcpy(msg, "File not found");
                send(sockfd, msg, strlen(msg) + 1, 0);
                strcpy(msg, "CLOSED");
                send(sockfd, msg, strlen(msg) + 1, 0);
            }
            else
            {
                // Read from the file
                fgets(msg, 256, fp);

                printf("%s", msg);
                send(nsockfd, msg, strlen(msg) + 1, 0);
                strcpy(msg, "CLOSED");
                send(nsockfd, msg, strlen(msg) + 1, 0);
                fclose(fp);
            }
            // Receive data
            close(nsockfd);
            break;

        }
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
/*服务器*/
int main()
{
//创建结构
服务器、客户端中的结构sockaddr_;
//创建一个套接字
int sockfd=套接字(AF_INET、SOCK_流、IPPROTO_TCP);
//初始化结构
bzero(&server,sizeof(server));
server.sin_port=htons(7008);
server.sinu family=AF\u INET;
inet_aton(“127.0.0.1”和server.sin_addr);
//将套接字绑定到端口
绑定(sockfd,(struct sockaddr*)&server,sizeof(server));
//监听连接
听(sockfd,5);
而(1)
{
int client_length=sizeof(客户端);
int nsockfd=accept(sockfd,(struct sockaddr*)和client,以及client_length);
char msg[256];//256字节的数据
内伦;
//创建子进程
如果(fork()==0)
{
len=recv(nsockfd,msg,256,0);
printf(“收到%s\n”,消息);
字符文件名[40];
//生成文件名
strcpy(文件名“./files/”);
strcat(文件名,msg);
printf(“打开%s的文件”,文件名);
//文件指针
文件*fp;
//服务器应返回“未找到文件”
//但它只是挂在那里
//它没有显示任何反应
如果(!(fp=fopen(文件名,“r”))
{
printf(“打开文件时出错”);
strcpy(msg,“未找到文件”);
发送(sockfd,msg,strlen(msg)+1,0);
strcpy(msg,“已关闭”);
发送(sockfd,msg,strlen(msg)+1,0);
}
其他的
{
//从文件中读取
fgets(msg,256,fp);
printf(“%s”,msg);
发送(nsockfd,msg,strlen(msg)+1,0);
strcpy(msg,“已关闭”);
发送(nsockfd,msg,strlen(msg)+1,0);
fclose(fp);
}
//接收数据
关闭(nsockfd);
打破
}
}
}

您正试图发送到
sockfd
,它是绑定(侦听)套接字的文件描述符。您应该改为发送到
nsockfd
,它是已接受(通信)套接字的文件描述符:

if(!(fp = fopen(filename, "r")))
{
    printf("Error opening file");
    strcpy(msg, "File not found");

/*  send(sockfd, msg, strlen(msg) + 1, 0);
         ^^^^^^--- wrong file descriptor
    strcpy(msg, "CLOSED");
    send(sockfd, msg, strlen(msg) + 1, 0);
         ^^^^^^--- wrong file descriptor    */

    // should instead be:
    send(nsockfd, msg, strlen(msg) + 1, 0);
    strcpy(msg, "CLOSED");
    send(nsockfd, msg, strlen(msg) + 1, 0);
}