Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
2.71/5 (3 votes)
See more:
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

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




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 :(
Posted
Updated 7-Oct-18 2:20am
v2
Comments
Jochen Arndt 1-Sep-13 7:21am    
I guess you got an error when executing the file, not when compiling.

If so, call perror() to get a specific error message.

You should also pass file_size to the sendto() function rather than strlen(file_buffer). strlen() will only work with text files and when the buffer has been initialized with null bytes.
Richard MacCutchan 1-Sep-13 7:42am    
You should not use UDP for this type of communication, unless you add your own protocol to identify the sequence of messages. Remember UDP does not guarantee the order of messages, or even if they will be delivered.
Member 10200096 1-Sep-13 8:50am    
yes but i have to do it using udp itself :)
Richard MacCutchan 1-Sep-13 12:06pm    
Then you also have to add a lot more code so you can identify the order of each message received, and also request a retransmit when a message does not get delivered. TCP will do all that for you.
Richard MacCutchan 1-Sep-13 12:06pm    
What is the error and where does it occur?

Done that !

there was something wrong with file handle
Instead of using fopen ,use of open() ,write() and read() do the work Smile | :)
 
Share this answer
 
can anyone tell me how to compile this code ,where we are specifying the file directory???????????
 
Share this answer
 
Comments
Dave Kreskowiak 7-Oct-18 8:59am    
First, you posted this as a solution to a question asked five years ago.

Next, the entire premise of this code is bullshit as file transfers should NEVER use UDP. UDP does not guarantee delivery of packets nor does it even guarantee the order they are delivered! Does that sound like a good network protocol to transfer files that will not tolerate missing data or data out of order?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900