Server-client file transfer using UDP in C

14,758

Solution 1

You will have to extend the logic at both places. Basically for the server code, you can use the buff as the filename and check if it exists. You can use "fopen (buff, "r" )" -- if this returns NULL, then the file does not exist and in that case you might want to send the client a note saying that the file does not exist. Something like this:

FILE *istream;
strncpy(fileName, buff, ret_len + 1);
if ( (istream = fopen(fileName, "r" ) ) == NULL ){
    printf ( "file non-existant!\n" );
    strncpy(buff, "Does not Exist", strlen("Does Not Exist"));
} else {
    printf ( "file exists!\n" );
    strncpy(buff, "Does Exist", strlen("Does Exist"));
    fclose (istream );
}

ret_len = sendto(sd, buff, strlen(buff), 0, (struct sockaddr *)&cliaddr, sizeof(struct sockaddr));
if (ret_len < 0) {
fprintf(stderr, "sendto() failed [ret value: %d]\n", ret_len);
return -1;
}

If the file exists, then read the file and send the content to the file.

The client must do a recvfrom() from the server after it sends the buffer to the server! Something like this. Note that you can use a numeric value (-1 or 0) to indicate if the file exists or not. And if the file exists, you can probably pass the file length in the initial message -- this way, the client would know how much data it needs to read.

// send  msg to server
sendto(sockfd, buff, strlen(buff) + 1, 0, (struct sockaddr *)&servaddr, sizeof(struct sockaddr));

// recveive from server
ret_len = recvfrom(sockfd, buff, 1024, 0, NULL, 0);
printf("Received from server: %s \n",buff);

if (strncmp(buff, "Does not Exist", strlen("Does not Exist")) == 0)
    printf("File does not exist\n");
else if (strncmp(buff, "Does Exist", strlen("Does Exist")) == 0)
    printf("File does exist\n");
else
    printf("Unknown message\n");

Btw, in the above code, we need to add 1 to account for the NUL character. Also, once this step is done, the client code should call recvfrom() in a loop to receive data from the server, as communicated in the earlier step.

Note that if the file is too big, then you probably want to read it only as much as the message you want to send -- with UDP you can send upto 64K but you should ideally send only upto 1 MUT (around 1500 bytes).

Solution 2

I wasn't sure if you had to develop your own client/server or if you could use others? There are a few other open source UDP based file transfer systems like UDT, UFTP, Tsunami-UDP, and even Google's QUIC.

Share:
14,758
user2714823
Author by

user2714823

Updated on June 04, 2022

Comments

  • user2714823
    user2714823 almost 2 years

    I have to make a server-client file tranfer using UDP . I have created a basic server which receives message sent by client . That's all. Now comes the major part :-

    1. The message sent by client is the name of file .
    2. Now the server checks whether there exists this file or not .
    3. If there exists it sends the file to the client and it also keeps the count of the     number of requests of file made by the client .
    

    I am new to this and i don't get it how to proceed further .

    Here is the Server Code

    #include<sys/socket.h>
    #include<arpa/inet.h>
    #include<stdio.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<sys/types.h>
    #include<string.h>
    
    
    
    
    int main()
    {
    
      char buff[2000];
      char file_buffer[2000];
      int sd,connfd,len;
    
      struct sockaddr_in servaddr,cliaddr;
    
      sd = socket(AF_INET, SOCK_DGRAM, 0);
    
      if(sd==-1)
        {
          printf(" socket not created in server\n");
          exit(0);
        }
      else
        {
          printf("socket created in  server\n");
        }
    
      bzero(&servaddr, sizeof(servaddr));
    
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = INADDR_ANY;
      servaddr.sin_port = htons(7802);
    
      if ( bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0 )
        printf("Not binded\n");
      else
        printf("Binded\n");
    
    
    
      len=sizeof(cliaddr);
    
      recvfrom(sd,buff,1024,0,
       (struct sockaddr *)&cliaddr, &len);
    
      printf("%s\n",buff);
      /* */
      FILE *fp;
      fp=fopen(buff,"r");
      if(fp==NULL)
        {
          printf("file does not exist\n");
        }
    
      fseek(fp,0,SEEK_END);
      size_t file_size=ftell(fp);
      fseek(fp,0,SEEK_SET);
      if(fread(file_buffer,file_size,1,fp)<=0)
        {
          printf("unable to copy file into buffer\n");
          exit(1);
        }
      if(sendto(sd,file_buffer,strlen(file_buffer),0,  (struct sockaddr *)&cliaddr, &len)<0)    {
        printf("error in sending the file\n");
        exit(1);
      }
      bzero(file_buffer,sizeof(file_buffer));
    
    
      /* */
      close(sd);
    
      return(0);
    }
    

    Here is the client code

    #include <stdio.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <resolv.h>
    #include<netinet/in.h>
    #include<sys/types.h>
    
    
    
    int main()
    {
        char buff[2000];
        int sockfd,connfd,len;
    
    struct sockaddr_in servaddr,cliaddr;
    
    // create socket in client side
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    
    if(sockfd==-1)
    {
        printf(" socket not created in client\n");
        exit(0);
    }
    else
    {
        printf("socket created in  client\n");
    }
    
    
    bzero(&servaddr, sizeof(servaddr));
    
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
    servaddr.sin_port = htons(7802);  // Port address
    
    
        printf("Type ur  UDP client message\n");
        scanf("%s",buff);
    
    // send  msg to server
    
    sendto(sockfd, buff, strlen(buff), 0,
              (struct sockaddr *)&servaddr, sizeof(struct sockaddr));
          char file_buffer[2000];
    
        if (recvfrom(sockfd,file_buffer,2000,0,  (struct sockaddr *)&servaddr, sizeof(struct sockaddr))<0)
        {
          printf("error in recieving the file\n");
          exit(1);
        }
    
      char new_file[]="copied";
      strcat(new_file,buff);
      FILE *fp;
      fp=fopen(new_file,"w+");
      if(fwrite(file_buffer,1,sizeof(file_buffer),fp)<0)
        {
          printf("error writting file\n");
          exit(1);
        }
    
    
      //close client side connection
        close(sockfd);
    
    return(0);
    }
    

    I have edited the programme and created a new buffer file_buffer , The server reads the data from file and writes into it and send to the client , On the other end client receive the data and make duplicate of this file and write into it. But on compiling it gives error in writting the file :(