APD
APD

Reputation: 169

Getting garbage characters after transfering file contents via UDP

Develop a client/server based application using UDP to execute the program at remote server. i.e. the client sends the executable file to the server, server executes the file , stores the result in a file and sends back to the client.

I have coded 2 files

//sends an executable file name to the server; server executes it //an echo program; this is the client's code

      #include <sys/types.h>
      #include <sys/socket.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <netinet/in.h>
     #include <arpa/inet.h>
    #include<fcntl.h>
    #include<unistd.h>
     #include<stdlib.h>
     void usage(const char *progname)
      {
        fprintf(stderr, "Usage: %s <server-IP> <server-port>\n",          
                progname);
       }
     int main(int argc, char *argv[])
      {

      if(argc != 3) 
      {
       usage(argv[0]);
       exit(1);
      }
     int len, result, sockfd,clilen;
       int n;
      struct sockaddr_in seraddr;
      char buf[310], buf1[500]={0};
      sockfd = socket(AF_INET, SOCK_DGRAM, 0);
       if(sockfd<0) 
             { // Check error condition
              perror("socket failed"); return -1;
             }


     bzero(&seraddr, sizeof(seraddr));

    seraddr.sin_family = AF_INET;
    seraddr.sin_addr.s_addr = INADDR_ANY;
    seraddr.sin_port=htons(atoi(argv[2]));
    int ap;

            clilen=sizeof(struct sockaddr_in);
    //read contents of file into buf; send to server
    int fd = open("Executable_Client.c", O_RDWR);
        int n1 = read(fd, buf, sizeof(buf));

         n = write(sockfd,buf,n1);
         ap=sendto(sockfd,buf,n1,0,(const struct sockaddr 
         *)&seraddr, sizeof(struct sockaddr));

       int n2 = recvfrom(sockfd, buf1, sizeof(buf1), 0, (struct     
                         sockaddr *)&seraddr, &clilen); 
        n = read(sockfd,buf1, n2);

        printf("The output is: %s\n", buf1);
        return 0;
        }

and

Server.c

//this program accepts an executable file from the client, and executes it //an echo program; this is the server's code

      #include<stdio.h>
      #include<string.h>
      #include<sys/types.h>
      #include<sys/socket.h>
      #include<netinet/in.h>
      #include<fcntl.h>
      #include<unistd.h>  
      #include<fcntl.h>
      #include<unistd.h> 
      #include<stdlib.h>

      void usage(const char *progname)
      {
        fprintf(stderr, "Usage: %s <server-port>\n", progname);
      }


      int main(int argc, char *argv[])
      {

       if(argc != 2)
       {
         usage(argv[0]);
         exit(1);
       } 

    int sockfd, newsockfd,portno, client, n, ans,clilen;
    char buf[310], buf1[256] = {0};

    struct sockaddr_in seraddr, cliaddr;
    int i, value;
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    if(sockfd<0) 
    { 
     perror("socket failed"); return -1;
     }

     int size=sizeof(struct sockaddr);
      bzero(&seraddr,size);

    seraddr.sin_family = AF_INET;
    seraddr.sin_addr.s_addr =inet_addr("127.0.0.1");
    seraddr.sin_port = htons(atoi(argv[1]));


    int a=bind(sockfd, (struct sockaddr *)&seraddr, size);
            if (a<0)   
            {
             perror("bind"); close(sockfd); return -1;
            }

             clilen=sizeof(struct sockaddr_in);

    int fd1 = open("Executable_Server.c", O_RDWR);
    //write contents into Executable_Server.c
    int n1 = recvfrom(sockfd, buf, sizeof(buf), 0, (struct    
                      sockaddr *)&seraddr, &clilen);
            n = write(fd1, buf, n1);

    system("gcc Executable_Server.c -o execute.out");
    system("./execute.out>Output.txt");//>"Output.txt");

    //read contents from Output.txt
    int fd = open("Output.txt", O_RDWR);
    int n3 = read(fd, buf1, n1);

    //send contents to client

    n = write(sockfd, buf1,n3);
            n = sendto(sockfd, buf1, n3, 0, (const struct   
                       sockaddr *)&seraddr, sizeof(struct
                       sockaddr));
   }

also, Executable_Client.c 's code is

//this file's executeable will be sent to the server, from the client; this file reads the contents of a file

      #include<stdio.h>
      #include<stdlib.h>
       int main()
       {
        int a, b, c;
         a = 3;
         b = 4;

        c = a+b;

        printf("a+b = %d\n", c);
              return 0;
      }

Upon executing,

in Executable_Server.c,besides the actual code of Executable_Client.c., a lot of garbage value is also present. As a result the terminal window in ubuntu 12.04 shows a lot of errors mentioning stray symbols which appear during the execution part. Can anyone suggest the reason behind this anomaly?

Upvotes: 0

Views: 1021

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123320

The code is wrong in various places. Just to mention the most obvious from a short look:

You mix recvfrom with read and try to read the same data (same with mixing sendto and write later).

   int n2 = recvfrom(sockfd, buf1, sizeof(buf1), 0, (struct     
                     sockaddr *)&seraddr, &clilen); 
    n = read(sockfd,buf1, n2);

And you print out the buffer independent of amount of data you've actually received. This will of course print out any junk data which were in the buffer before the read and which where not overwritten by the read.

    printf("The output is: %s\n", buf1);

And of course is a bad idea to do such things with UDP as long as you don't care about packet reordering, packet loss and packet duplication.

Upvotes: 2

Related Questions