gpt4 book ai didi

c - Socket发送和获取txt文件c

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

所以我有一个server.c和一个client.cclient.c会提示输入文件名,然后将文件名发送到服务器并检查该文件是否存在,如果存在则将该文件发送到客户端。我的问题是,当服务器获取正确的文件名并发送给客户端时,客户端无法接收该文件。

这是服务器.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<pthread.h>

#define PORT 5000

void *multiconnection(void *);

int main(int argc , char *argv[])
{
int s_sock , c_sock;
int sin_size;
int *addSocket;
struct sockaddr_in server;
struct sockaddr_in client;

/* initial */
s_sock = socket(AF_INET , SOCK_STREAM , 0);
if (s_sock < 0)
{
perror("socket cannot be created");
}
printf("Socket is successfully created\n");

/* Fill the socket address struct */
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;

/* Bind a special Port */
if(bind(s_sock,(struct sockaddr *)&server, sizeof(server)) < 0)
{
perror("binding Error");
exit(1);
}
printf("binding success\n");

/* Listen to connect */
listen(s_sock , 3);

/* Accept connections */
printf("Waiting clients connections:::\n");

sin_size = sizeof(struct sockaddr_in);
while((c_sock = accept(s_sock, (struct sockaddr *)&client, (socklen_t*)&sin_size)) )
{
/* create new sockets for mulitiple clients */
pthread_t sniffer_thread;
addSocket = malloc(1);
*addSocket = c_sock;

if( pthread_create(&sniffer_thread,NULL, multiconnection,(void*) addSocket) < 0)
{
perror("Threads error");
exit(1);
}
}

if (c_sock < 0)
{
perror("accept failed");
exit(1);
}

return 0;
}


void *multiconnection(void *s_sock)
{
/* Get the socket descriptor */
int sock = *(int*)s_sock;
int read;
char *msg , c_msg[2000];

/* Receive a message from client */
while(read = recv(sock , c_msg , sizeof(c_msg) , 0) > 0 )
{
printf("Recieve file name from client: %s\n", c_msg);
if (fopen(c_msg,"r") == NULL)
write(sock , "File does not exsit" , 20);
else
{
write(sock ,"File exsit", 20);
/* if file exsit, send to clients */
char* file_pth = "/home/nelson/Desktop/SYSPROG_asg_04A_Tang/hello.txt";
char buff[1024];
printf("Sending %s to the Client...\n", file_pth);
FILE *fp = fopen(file_pth, "r");
if(fp == NULL)
{
perror("Open file error");
exit(1);
}

int file_size;
while(file_size = fread(buff, sizeof(char), sizeof(buff), fp) > 0)
{
if(send(sock, buff, file_size, 0) < 0)
{
perror("Fail to send file");
exit(1);
}
printf("Buff : %s",buff);
}
write(sock , "You Recieved the file" , 50);
printf("file is sent to client!\n");
//close(sock);
}
}

if(read < 0)
perror("fail to recieve");


free(s_sock);

return 0;
}

这是我的client.c,现在我只是想从文本文件中获取内容,但它不能。

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

#define PORT 5000
#define localAddr "127.0.0.1"
int main(int argc , char *argv[])
{
int s_sock;
char msg[512];
char reply_msg[512];
char buff[512];
struct sockaddr_in server;

/* initial */
s_sock = socket(AF_INET , SOCK_STREAM , 0);

if (s_sock < 0)
{
perror("socket cannot be created");
}
printf("Socket is successfully created\n");

/* Fill the socket address struct */
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr(localAddr);

/* Connect to server */

if (connect(s_sock,(struct sockaddr *)&server, sizeof(server)) < 0)
{
perror("connection error");
//exit(1);
}

printf("Connected\n");

while(1)
{
printf("Enter file name you wish to get from server: ");
scanf("%s" , msg);

/* send file name */
if(send(s_sock,msg,sizeof(msg),0) < 0)
{
perror("Sending error");
//exit(1);
}

/* Receive message from sever */
if( recv(s_sock,reply_msg , 512 , 0) < 0)
{
perror("Receive message error");
//exit(1);
}

printf("Server reply : %s\n",reply_msg );


/* Receive a file */
if (strcmp(reply_msg,"File exsit") == 0)
{
//memset(reply_msg, 0, sizeof(reply_msg));
printf("Receiving file from Server\n");
char* file_path = "/home/nelson/Desktop/SYSPROG_asg_04A_Tang/final.txt";
FILE *fp = fopen(file_path, "a");

if(fp == NULL)
printf("File %s Cannot be opened.\n", file_path);

char buff[512];
//recv(s_sock,buff , sizeof(buff) , 0);
printf("buff : %s\n", buff);
printf("buff : %s\n", reply_msg);
/*int w_size = fwrite(reply_msg, sizeof(char), sizeof(reply_msg), fp);
if(w_size < sizeof(reply_msg))
{
perror("Writing error.\n");
}

if (sizeof(reply_msg) == 0 || sizeof(reply_msg) != 512)
{
break;
}
if(sizeof(reply_msg) < 0)
{
perror("error");
}
printf("Ok received from server!\n");
fclose(fp); */
}else
printf("\n");
}

close(s_sock);
return 0;
}

最佳答案

My problem is that when the server get correct file name and send to client, the client cannot receive the file.

造成这个问题的根本原因当然是你注释掉了客户端中的recv()调用:

   //recv(s_sock,buff , sizeof(buff) , 0);

关于c - Socket发送和获取txt文件c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191479/

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