在子进程中打开文件

时间:2014-05-03 14:29:22

标签: c file tcp fork

我正在做一个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);
} 

服务器程序:

#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;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在尝试发送到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);
}