以下是我的服务器和客户端代码
服务器.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#define PORT 9999
#define MAXBUFF 3
void sig_chld(int n){
int status;
while(waitpid(-1,&status,WNOHANG)>0);
}
void main(){
int sockfd,clientfd;
char buffer[1024]={0};
char path[1024]={0};
char client_IP[INET_ADDRSTRLEN];
void sig_chld(int);
struct sockaddr_in server,client;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("sockfd err\n");
}
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=INADDR_ANY;
memset(server.sin_zero, '\0', sizeof server.sin_zero);
if(bind(sockfd,(struct sockaddr *)&server,sizeof(server))==-1){
perror("Not bind\n");
exit(errno);
}
if(listen(sockfd,10)==-1){
perror("Not listen");
exit(errno);
}else{
printf("Listening\n");
}
signal(SIGCHLD, sig_chld);
int le=sizeof(client);
while(1){
if((clientfd=accept(sockfd,(struct sockaddr *)&client,&le))==-1){
if(errno==EINTR)
continue;
else{
perror("accept\n");
exit(errno);
}
}
if(fork()==0){
close(sockfd);
bzero(buffer,sizeof(buffer));
bzero(path,sizeof(path));
inet_ntop(AF_INET,&(client.sin_addr),client_IP,sizeof(client));
printf("IP of client is %s and port is %d\n",client_IP,client.sin_port);
//receiving the pah of the file
recv(clientfd,path,sizeof(path),0);
FILE *fp;
fp = fopen(path, "a+");
char *s=NULL;
//sending the file
while(fgets(buffer,sizeof(buffer),(FILE*)fp)){
int n=0;
n=send(clientfd,buffer,strlen(buffer)+1,0);
printf("%d %d\n",strlen(buffer),n);
bzero(buffer,sizeof(buffer)); //imp
printf("In while loop\n");
}
bzero(buffer,sizeof(buffer));
//waiting for a modification
//not able to recieve from client on this line
recv(clientfd, buffer, sizeof(buffer), 0);
printf("After recv");
fputs(buffer,fp);
exit(0);
}
close(clientfd);
}
close(sockfd);
}
客户端.c
/****************** CLIENT CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket;
char buffer[1024]={0};
bzero(buffer,sizeof(buffer));
struct sockaddr_in serverAddr;
socklen_t addr_size;
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(9999);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
//sending the Name of the file
send(clientSocket,"text",strlen("text")+1,0);
FILE *fp;
fp = fopen("client.txt", "a+");
//receiving the file
int n=0;
while((n=recv(clientSocket, buffer, sizeof(buffer), 0))>0){
/*---- Print the received message ----*/
fputs(buffer,fp);
printf("Data received %sand n is: %d\n",buffer,n);
bzero(buffer,sizeof(buffer));
}
//sending a modification
//Unable sending
send(clientSocket,"modification",strlen("modification")+1,0);
close(clientSocket);
return 0;
}
在此客户端将文件名称发送到服务器。
服务器将文件发送给客户端
客户端存储文件(有修改)并建议修改,服务器将修改添加到文件中。
但是我无法从客户端接收 MODIFICATIONS(“代码中的修改字符串”)。
有人可以建议代码中有什么问题吗? 有时文件也会部分发送。
最佳答案
正如名称 SOCK_STREAM
所示,TCP 是一种流协议(protocol),没有消息边界。很容易发生这样的情况:对 recv
的一次调用不会给您一条完整的消息,或者如果另一端速度很快,则多于一条消息(甚至是它们的组合,就像一条又一条部分消息)。如果您想知道一条消息何时结束而另一条消息何时开始,您需要在 TCP 之上实现一个简单的协议(protocol)来处理这些事情。
这可能可以解释您问题的部分内容。
关于c - 使用socket进行文件传输和修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36791895/