c - 如何使用 tcp/ip 将文件及其文件名从客户端发送到服务器?

标签 c sockets ubuntu file-io

我正在使用 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;
}

最佳答案

您试图用这段代码完成什么确实不清楚。

如果我是正确的,上面发布的代码正在从客户端接收数据,循环通过分别在“文件名”和“文件体”数组中存储一些数据来处理数据。然后,您尝试打开每个文件名并将其相应的正文数据存储在文本文件中。

我认为问题在于内部 for 循环,您在每次迭代时都打开文件。由于您使用第二个参数作为“w”(可写)打开文件,因此您将进行覆盖,从而删除文件的现有内容。这可能会导致它看起来没有从客户端收到文件。

尝试使用第二个参数作为“a”(追加)而不是可写来打开文本文件,并在每次迭代时关闭文件。还可以考虑移动该语句:

    write_size = fwrite(pBuf, 1, buffersize, text);

在内部 while 循环中。

关于c - 如何使用 tcp/ip 将文件及其文件名从客户端发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34488030/

相关文章:

php - 在系统上的所有文件中查找一个字符串

ubuntu - SSH 连接到 natty 操作超时的 EC2 实例

c - LPCXpresso错误CreateProcess:没有这样的文件或目录

c++ - 为什么预增量运算符在 C 中给出右值?

c - glibc 检测到 : realloc(): invalid next size

Java P2P 网络,对不同的入站和出站连接使用相同的端口

c - 如何使用 C 设置 eclipse 进行大学工作

c - 在c socket 中,为什么我的服务器不能接收全部内容?

ruby - 在UDPSocket#bind中救援ERRNO::EADDRINUSE

Ubuntu 上托盘消息的 Java 外观