gpt4 book ai didi

c - 在子进程中打开文件

转载 作者:行者123 更新时间:2023-11-30 17:34:15 24 4
gpt4 key购买 nike

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

}
}
}

最佳答案

您正在尝试发送到 sockfd,它是绑定(bind)(监听)套接字的文件描述符。您应该发送到 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);
}

关于c - 在子进程中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23445717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com