#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    UDP:FTP :: no client server interaction happening


    Hi,

    I am new to c/networking and trying to do basic UDP file transfer.I have used the code from codeproject.com (http://www.codeproject.com/Questions/646310/Server-client-file-transfer-using-UDP-in-C). I am getting no error in compiling or executing but there is no response from either the client or server.It seems there is no connection happening. I am pasting the modified code. Please help me understand.Really stuck!!!

    udpclient.c

    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;
    int len2; 
    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
    len=sizeof(struct sockaddr);
    if(sendto(sockfd, buff, 2000, 0,(struct sockaddr *)&servaddr, len)<0)
              {
              	printf("error sendTO..%s",strerror(errno));
              }
              else
              {
              	printf("happening sendTo");
              }
              printf("after sendTo");
          char file_buffer[2000];
     
        if (recvfrom(sockfd,file_buffer,2000,0,  (struct sockaddr *)&servaddr, &len)<0)
        {
          printf("error in recieving the file--%s\n",strerror(errno));
          exit(1);
        }
        else
        {
        	printf("success recvFrom");
        }
     
      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);
    }

    udpserver.c

    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>
    #include <errno.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);
     
      if(recvfrom(sd,buff,1024,0,(struct sockaddr *)&cliaddr, &len)<0)
      {
      	printf ("error in receiev from %s",strerror(errno));
      }
      else
      {
      	printf("success");
      }
     printf("after recfFrom");
      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,2000,0,  (struct sockaddr *)&cliaddr, len)<0)    {
        printf("error in sending the file--%s\n",strerror(errno));
        exit(1);
      }
      else
      {
      	printf("happening sendTo");
      }
      bzero(file_buffer,sizeof(file_buffer));
     
    
      /* */
      close(sd);
     
      return(0);
    }

    Please guide me where I am going wrong.Any help appreciated.

    Thanks & Regards

    Abhinav
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    In the client:
    Code:
    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
    len=sizeof(struct sockaddr);
    if(sendto(sockfd, buff, 2000, 0,(struct sockaddr *)&servaddr, len)<0)
              {
              	printf("error sendTO..%s",strerror(errno));
              }
              else
              {
              	printf("happening sendTo");
              }
              printf("after sendTo");
    Just how is this supposed to obtain and use the server address? The server address is supposed to be in servaddr.sin_addr.s_addr. What do you have in there? 0x00000000 ... at least in my compiler (MinGW gcc) that's what INADDR_ANY is defined to be. Is the IP address of your server by any chance 0.0.0.0? I didn't think so.

    After you scanf the server address from the user, you need to load that address into servaddr.sin_addr.s_addr. Except you're reading in a char string and servaddr.sin_addr.s_addr does not expect a char string, but rather the IP address as an unsigned 32-bit integer, in network byte order. I use inet_addr() for that, though I understand that there are newer ways.

    I discuss the process of setting an address at http://pgm.dwise1.net/sockets/socket...SOCKET_ADDRESS.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    I suppose the only lesson here is you should really know how to program before copy/pasting code off the net.

    You might then at least stand a chance of filtering out all of the crap.
    Code:
      char new_file[]="copied";
      strcat(new_file,buff);
    I wouldn't trust anything from someone who can't even concatenate 2 strings without a buffer overflow.

    I suggest you read Beej's tutorial and then write your own.
    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

IMN logo majestic logo threadwatch logo seochat tools logo