我正在尝试使用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);
}
}
答案 0 :(得分:3)
您需要检查recv
if ((nbytes = recv(client_sockfd,fh,1024+1,0)) > 0)
并使用'\0'
fh[nbytes] = '\0';
printf("server recieved %s",fh);
此外,使用像1024+1