我正在尝试使用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);
}
}
这可能是一个更好的问题在stackoverflow。 – Shadur
代码中有几个未定义的变量(例如'file'和'message')。请更新此代码,至少编译。 – ugoren
'char * buffer [1024];' - 你真的想要一个1024个指针的数组吗? –