使用套接字传输和修改文件

时间:2016-04-22 10:55:29

标签: c sockets network-programming

以下是我的服务器和客户端代码

Server.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);


}

Client.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;
}


在此,客户端将文件NAME发送到服务器。 服务器将文件发送到客户端 客户端存储文件(带有修改)并建议修改,服务器将修改添加到文件中。 但是,我无法从客户端收到MODIFICATIONS(“代码中的修改字符串”)。

有人可以建议代码中有什么问题。 有时文件也会部分发送。

1 个答案:

答案 0 :(得分:1)

如名称SOCK_STREAM所示,TCP是流式协议,没有消息边界。很容易发生一次拨打recv没有给你一个完整的消息,或者如果另一端是快速的,更多而不是一条消息(或者甚至是它们的组合,如一个和一部分消息)。如果你想知道一个消息何时结束而另一个消息何时开始,你需要在TCP之上实现一个处理这些事情的简单协议。

这可能解释了你问题的部分内容。