Thread: Server-client file transfer using UDP in C

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Unhappy Server-client file transfer using UDP in C

    I have to make a server-client file transfer 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 . Here is the Server Code

    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);
    }

    Client code:-
    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)+1, 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);
    }
    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 reading the file . Please do heelp me


    Last edited by keypad; 09-01-2013 at 03:48 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But on compiling
    Compiling is when you do
    gcc -o server server.c

    Running (or executing) the program is when you do this.
    ./server


    > it gives error in reading the file
    None of your posted code contains the error message "error in reading the file".

    Copy/paste your console logs, don't guess and paraphrase.

    > char new_file[]="copied";
    > strcat(new_file,buff);
    How many characters can you append to new_file?
    If you think the answer is anything other than zero, then you need to go back to some C basics.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Reply to salem

    Quote Originally Posted by Salem View Post
    > But on compiling
    Compiling is when you do
    gcc -o server server.c

    Running (or executing) the program is when you do this.
    ./server


    > it gives error in reading the file
    None of your posted code contains the error message "error in reading the file".

    Copy/paste your console logs, don't guess and paraphrase.

    > char new_file[]="copied";
    > strcat(new_file,buff);
    How many characters can you append to new_file?
    If you think the answer is anything other than zero, then you need to go back to some C basics.
    Sorry about that . Yes as you are saying it gives the error in sending the file.
    Yes this is an error but i am new to C so please help me out?

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by keypad View Post
    Sorry about that . Yes as you are saying it gives the error in sending the file.
    Yes this is an error but i am new to C so please help me out?
    I don't interpret what Salem said as an error in sending the file. I assume you are talking about your invalid use of string literals that he pointed out. Basically, what you did is store a constant string in memory, then you tried to write to read-only data. To fix this, do one of two things, either dynamically allocate memory for more characters to a character pointer that won't be constant, or specify how many characters you want the new_file array to be able to hold.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why is it too hard to simply copy/paste whatever is printed in your console when it crashes?

    > if(sendto(sd,file_buffer,strlen(file_buffer),0, (struct sockaddr *)&cliaddr, &len)<0)
    Here are some things wrong with this.
    1. fread() does NOT append a \0 to the buffer, which is a problem if you come along and try and do strlen() on it. You'll get a length all right (or a crash), but it'll be pure dumb luck if it's the answer you want. You've already measured the length, so use it.
    2. Rather than just saying "it's an error", try printing the actual error.
    Eg.
    Code:
      if(sendto(sd,file_buffer,strlen(file_buffer),0,  (struct sockaddr *)&cliaddr, &len)<0){
        perror("error in sending the file\n");
        exit(1);
      }
    perror() will tell you WHY it failed - at least enough to go and look up the reason in the manual page for sendto()

    > if(fread(file_buffer,file_size,1,fp)<=0)
    How are you making sure the file length is less than the buffer length?

    > bzero(file_buffer,sizeof(file_buffer));
    It's too late for that now, you've already finished with the buffer.


    > Basically, what you did is store a constant string in memory, then you tried to write to read-only data.
    This is way off base as well.
    It's not a question of the string being read-only, it's a question of it having NO space to be able to append any data to it.

    This is bad
    char new_file[]="copied";
    strcat(new_file,buff);

    This is better
    char new_file[100]="copied";
    strcat(new_file,buff);

    Better still would be range-checking the length of the data to be copied.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  2. File Transfer Code (Unable to keep server open)
    By jacob_76505 in forum C Programming
    Replies: 0
    Last Post: 11-09-2012, 06:55 PM
  3. IRC Client + DCC P2P File Transfer
    By Vincent Wouters in forum Networking/Device Communication
    Replies: 0
    Last Post: 11-05-2011, 07:20 AM
  4. Client-server application for hybrid file transfer
    By ejjjjj in forum C Programming
    Replies: 6
    Last Post: 05-16-2010, 02:27 PM
  5. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM

Tags for this Thread