我使用TCP / IP将文件从客户端发送到服务器及其文件名。我已经开发了代码,但我无法接收该文件。我提供的代码供参考。
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<sys/socket.h>
int receive_text(long long int socket)
{
long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;
char verify = '1',c;
int errno;
FILE *text;
char *pch;
char *str="/home/sosdt009/Documents";
char *fname[2];
char *filename[10];
char *filebody[1024];
int i=0;
//Find the size of the text
recv(socket, (char *)&size, sizeof(int), 0);
printf("Size value is:%ld\n",size);
//Send our verification signal
send(socket, &verify, sizeof(char), 0);
printf("Size value is:%ld\n",size);
//Make sure that the size is bigger than 0
if (size <= 0)
{
printf("Error has occurred. Size less than or equal to 0 \n");
return -1;
}
//Loop while we have not received the entire file yet
while (recv_size < size)
{
ioctl(socket, FIONREAD, &buffersize);
//We check to see if there is data to be read from the socket
if (buffersize > 0)
{
char *pBuf = malloc(buffersize);
printf("Buffer value is:%s\n",pBuf);
if (!pBuf)
{
fprintf(stderr, "Memory Error. Cannot allocate!\n");
exit(-1);
}
read_size = recv(socket, pBuf, buffersize, 0);
printf("read size is:%ld\n",read_size);
if (read_size < 0)
{
printf("%s", strerror(errno));
}
//printf ("Splitting string \"%s\" into tokens:\n");
pch = strtok (pBuf,"@");
printf(" value is:%s\n",pch);
while (pch != NULL)
{
filename[i]=pch;
strcpy(str,filename[i]);
printf("the string copy is: %s\n",str);
text = fopen(str, "w");
/*printf ("filename=%s\n",filename[i]);
pch = strtok (NULL, "@");*/
i++;
filebody[i]=pch;
printf ("filebody=%s\n",filebody[i]);
pch = strtok (NULL, "@");
while ((filebody[i] = strtok(NULL, "@")) != NULL)
printf("Next: %s\n",filebody[1024]);
//strcpy(filename[i],"filename");
//strcpy(filebody[i],pBuf);
//strcat(filename[i],filebody[i]);
pBuf=filename[i];
}
//Write the currently read data into our text file
write_size = fwrite(pBuf, 1, buffersize, text);
free(pBuf);
//Increment the total number of bytes read
recv_size += read_size;
}
}
fclose(text);
printf("File successfully Received! \n");
return 1;
}
int main(int argc , char *argv[])
{
long long int socket_desc , new_socket, c, read_size, buffer = 0;
struct sockaddr_in server , client;
char *readin;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 6777 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server ,sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("Bind completed");
//Listen
listen(socket_desc,3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
if((new_socket = accept(socket_desc,(struct sockaddr *)&client,(socklen_t *)&c)))
{
puts("Connection accepted");
}
fflush(stdout);
if (new_socket<0)
{
perror("Accept Failed");
return 1;
}
receive_text(new_socket);
close(socket_desc);
fflush(stdout);
return 0;
}
答案 0 :(得分:1)
您尝试使用此代码完成的工作真的不清楚。
如果我是正确的,上面发布的代码是从客户端接收数据,并且循环通过存储分别在“filename”和“filebody”数组中存储一些数据来处理数据。然后尝试打开每个文件名并将其相应的正文数据存储在文本文件中。
我认为问题在于内部for循环,您在每次迭代时打开文件。由于您使用第二个arg打开文件为“w”(可写),因此您将覆盖,从而删除文件的现有内容。这可能导致它似乎没有从客户端收到文件。
尝试打开文本文件,第二个参数为'a'(追加),而不是可写,并在每次迭代时关闭文件。还要考虑移动声明:
write_size = fwrite(pBuf, 1, buffersize, text);
在内部while循环中。