通过c for windows中的tcp流套接字发送文件



我很难弄清楚如何退出接收器端的循环。我有一个tcp流套接字连接,我发送从文件中读取的内容并将其放入缓冲区。接收端只是不断地循环写入相同的信息。我确信这是因为我没有递减nCv,所以它永远不会到达if(nCv==0)语句,但我不知道如何递减它。我正在为发送方和接收方发布while循环,希望有人能给我指明正确的方向。

发送器

    /* prepare file to send */
pf = fopen("input.txt", "rb");
if(pf == NULL)
{
    printf("The file you want to send was not found");
    return(1);
}
else
{
    while (!feof(pf))
    {
        nRead = fread(bufferin, sizeof(char), 256, pf);
        if (nRead <= 0)
            printf("ERROR reading file");
        while (nRead > 0)
        {
            nSent = send(filesender_socket, bufferin, nRead, 0);
            if (nSent < 0)
            {
                    printf("ERROR sending from socket = %dn", WSAGetLastError());
                    break;
            }
            if (nSent == 0)
                printf("DISCONNECTED writing to socket");
            //pBuf += nSent;
            //nRead -= nSent;
        }
    }
} //end of if statement

    // Close all open sockets
#ifdef WIN
  //retcode = closesocket(jsender_socket);
  retcode = closesocket(filesender_socket);
 if (retcode < 0)
{
 printf("*** ERROR - closesocket() failed n");
exit(-1);

}#endif

接收器

    //create a new socket for file transfer
  filesocket = socket(AF_INET, SOCK_STREAM, 0);
  if (filesocket < 0)
  {
    printf("*** ERROR - socket() failed n");
    exit(-1);
  }
  // >>> Step #2 <<<
  // Fill-in my socket's address information
  receiver_addr.sin_family = AF_INET;                 // Address family to use
  receiver_addr.sin_port = htons(PORT_FILE);           // Port number to use
  receiver_addr.sin_addr.s_addr = htonl(INADDR_ANY);  // Listen on any IP address
  filebindcode = bind(filesocket, (struct sockaddr *)&receiver_addr,sizeof(receiver_addr));
  if (filebindcode < 0)
  {
    printf("*** ERROR - file socket bind() failed n");
    exit(-1);
  }
    printf("receiver accepting connectionsn");
    //2 DEBUG LINES
    printf("received a connection from: %s port %dn",
    inet_ntoa(sender_addr.sin_addr), ntohs(sender_addr.sin_port));
 if (newsockfd = listen(filesocket, 2) < 0) {
    printf("newsock in listen %dn", newsockfd);
     perror("listen failed");
     exit(1);
 }
 //listen(filesocket, 2);
 //while(1)   //while loop for to accept files
 //{
 printf("nwaiting for accept() to complete n");
  newsockfd = accept(filesocket, (struct sockaddr *) &sender_addr, &addr_len);
  printf("newsock return %d", newsockfd);
    if (newsockfd < 0)
          {
        printf("*** ERROR - accepting() failed n");
        exit(-1);
      }
    // start receiving file

    fp = fopen("output.txt", "wb");
    if (fp == NULL) 
    {
        printf("File not found!n");
        return NULL;
    }
    else 
    {
        printf("created file output.txtn");
    }   
    //receive file 
    while(1)
    {
         nRecv = recv(newsockfd, bufferin, 256, 0);
        if (nRecv < 0)
        {
                printf("ERROR reading from socket = %dn", WSAGetLastError());
                break;
        }
        if (nRecv == 0)
            break;
        while (nRecv > 0)
        {
             printf("%s", bufferin); // debug
             nWritten = fwrite(bufferin, sizeof(char), nRecv, fp);
            if (nWritten <= 0)
                printf("ERROR writing to file");
            //nRecv -= nWritten;
        }
    }
    printf("File Transfer completenn");     //} //end of while

recv()的返回值如下:

  • >0——接收的字节数
  • 0—没有数据(仅限异步套接字)或其他套接字已完全关闭
  • <0—出现错误

在发送方代码中,您永远不会关闭套接字,因此接收方会等待更多数据。

不知道为什么,但现在我评论掉了两行,因为它们不起作用,现在它们起作用了。。

取消注释以下行

                pBuf += nSent;
            nRead -= nSent;

以及接收器侧的

                nRecv -= nWritten;

它就像一个符咒。

谢谢你的帮助。

最新更新