0

我正在尝试使用 C 套接字编程将文件从客户端发送到服务器。但是在服务器端,我无法接收从客户端发送的文件。我附上下面的代码。

服务器:

/*  Create a connection queue and wait for clients.  */

listen(server_sockfd, 5);
while(1) {
    char ch;

    printf("server waiting\n");

/*  Accept a connection.  */

    client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_address,cli);
    if(client_sockfd > 0)
    printf("client is connected\n");
/*  We can now read/write to client on client_sockfd.  */
  char *fh;
    recv(client_sockfd,fh,1024+1,0);
    printf("server recieved %s",fh);

/*        read(client_sockfd, &ch, 1);
    ch++;
    write(client_sockfd, &ch, 1); */
   return close(client_sockfd);
}
}
4

1 回答 1

3

您需要检查退货recv

if ((nbytes = recv(client_sockfd,fh,1024+1,0)) > 0)

并结束你的缓冲区'\0'

fh[nbytes] = '\0';
printf("server recieved %s",fh);

此外,使用幻数也不是一个好主意1024+1

于 2013-10-15T10:17:38.063 回答