我正在嘗試使用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個指針的數組嗎? –