Getting garbage values when transfering file from server to client in c?

dinali dabarera

this is my c code of the server and the client. /* TCP server */

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

    #include <string.h>
    #include <errno.h>

        #include <stdlib.h>

            FILE *fp;

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


            int listenfd,connfd,n;


            struct sockaddr_in servaddr,cliaddr;
            socklen_t clilen;

            char * banner = "1077"; // size of the file
            char buffer[1000];


            /* one socket is dedicated to listening */

            listenfd=socket(AF_INET,SOCK_STREAM,0);

            /* initialize a sockaddr_in struct with our own address information for
            binding the socket */

            servaddr.sin_family = AF_INET;
            servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
            servaddr.sin_port=htons(32000);

            /* binding */
            bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
            listen(listenfd,0); //listen( --,--);
            clilen=sizeof(cliaddr);

            /* accept the client with a different socket. */
            connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); // the uninitialized cliaddr variable is filled in with the
            n = recvfrom(connfd,buffer,1000,0,(struct sockaddr *)&cliaddr,&clilen);//information of the client by recvfrom function
                buffer[n] = 0;
            sendto(connfd,banner,strlen(banner),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
            printf("Received:%s\n",buffer);


             char file_name[] ="severtext.txt";




                 fp = fopen(file_name,"r"); // read mode

                           if( fp == NULL )
                           {
                              perror("Error while opening the file.\n");
                              exit(EXIT_FAILURE);
                           }

                printf("The contents of %s file are :\n", file_name);
            char sendData[1000];
        /*open servertext.txt and coppy it to a char array */
                fread(sendData, 1000, 1, fp);

                printf("The contents of sending\n");

                         sendto(connfd,sendData,sizeof(sendData),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

        /*information of the client by recvfrom function */
                         n = recvfrom(connfd,buffer,1000,0,(struct sockaddr *)&cliaddr,&clilen);
                         buffer[n] = 0;
                         printf("Received:%s\n",buffer);









                 printf("End of connection \n");


            return 0;
        }

...client code /* Sample TCP client */

 #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>



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



            int sockfd,n;
            FILE *fp;
            fp = fopen("file.txt", "w+");
        struct sockaddr_in servaddr;
        char banner[] = "request serverfile.txt";

           char revbuf1[1000];

        char buffer[4];

        if (argc != 2){
            printf("usage: ./%s <IP address>\n",argv[0]);
            return -1;
        }
        /* socket to connect */
        sockfd=socket(AF_INET,SOCK_STREAM,0);
        /* IP address information of the server to connect to */
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr=inet_addr(argv[1]);
        servaddr.sin_port=htons(32000);
        connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

        sendto(sockfd,banner,strlen(banner),0, (struct sockaddr *)&servaddr,sizeof(servaddr));

        n=recvfrom(sockfd,buffer,10,0,NULL,NULL);
        buffer[n]=0;

        printf("Received: %s\n",buffer);


            printf("[Client] Receiveing file from Server and saving it as final.txt...");
            char * banner1="ok";


            printf("\ndata1 set\n"); 

            n=recvfrom(sockfd,revbuf1,1000,0,NULL,NULL);
        revbuf1[n]=0;
            sendto(sockfd,banner1,strlen(banner1),0, (struct sockaddr *)&servaddr,sizeof(servaddr));
        printf("Received: %s\n",revbuf1);
            //open file.txt and over write it with the recieved data in the client side.
            fwrite(revbuf1, 1000, 1, fp);
            fclose(fp);


             close (sockfd);
             printf("[Client] Connection lost.\n");
        return 0;


    }

When i run these two programs i get garbage values in the file.txt with the servertext.txt content, Can someone help to fix this problem?

Weather Vane

Because you are writing the whole of the rxd string to the file with

fwrite(revbuf1, 1000, 1, fp);

and any unused space in the array will be garbage. You should do this:

fwrite(revbuf1, 1, n, fp);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

when loading a huge file into hadoop cluster , what happends if the client failed while transfering data to datanodes?

From Dev

Getting string from another class and transfering it to url

From Dev

Getting NullPointerException when getting the values from Properties file in Selenium

From Dev

VBA Transfering Values from an Excel table to a String

From Dev

Why am not getting the right char values from scanf(c Linux vi) when scanning a file?

From Dev

Garbage values are getting assigned

From Dev

Video and Audio files change file type when transfering from Ubuntu to Windows 7 partition

From Dev

Video and Audio files change file type when transfering from Ubuntu to Windows 7 partition

From Dev

Transfering files in a client-server socket connection properly

From Dev

Why am I getting garbage values when I print the value?

From Dev

Java - copy/paste file from client to server getting error - UTF-8 encoding problem opening file

From Dev

Getting number values from text file C++

From Dev

C++ - Getting values from text file into array for comparison

From Dev

Getting the number of values in a char buffer (from file read) in C

From Dev

Send a string from client to server, then save the file [using C]

From Dev

connect client to server and get a text file from it in C#

From Dev

Getting values from JSON file

From Dev

Bash: getting values from file

From Dev

Getting values from JSON file

From Dev

Can't fix NullPointerException when transfering values through views

From Dev

Send a file from Client to a Server

From Dev

File transfer from client to server

From Dev

Transfering values from an object in a ArrayList to an array sudoku board

From Dev

Getting Response from TCP server on Swift client

From Dev

Transfering about 300gb in files from one server to another

From Dev

Problems transfering codeigniter 3.0 from localhost to live server hostgator

From Dev

"connection unsuccessful" transfering a file via Bluetooth from Android phone

From Dev

Enterprise Architect - Transfering project from file to PostgreSQL repository

From Dev

Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

Related Related

  1. 1

    when loading a huge file into hadoop cluster , what happends if the client failed while transfering data to datanodes?

  2. 2

    Getting string from another class and transfering it to url

  3. 3

    Getting NullPointerException when getting the values from Properties file in Selenium

  4. 4

    VBA Transfering Values from an Excel table to a String

  5. 5

    Why am not getting the right char values from scanf(c Linux vi) when scanning a file?

  6. 6

    Garbage values are getting assigned

  7. 7

    Video and Audio files change file type when transfering from Ubuntu to Windows 7 partition

  8. 8

    Video and Audio files change file type when transfering from Ubuntu to Windows 7 partition

  9. 9

    Transfering files in a client-server socket connection properly

  10. 10

    Why am I getting garbage values when I print the value?

  11. 11

    Java - copy/paste file from client to server getting error - UTF-8 encoding problem opening file

  12. 12

    Getting number values from text file C++

  13. 13

    C++ - Getting values from text file into array for comparison

  14. 14

    Getting the number of values in a char buffer (from file read) in C

  15. 15

    Send a string from client to server, then save the file [using C]

  16. 16

    connect client to server and get a text file from it in C#

  17. 17

    Getting values from JSON file

  18. 18

    Bash: getting values from file

  19. 19

    Getting values from JSON file

  20. 20

    Can't fix NullPointerException when transfering values through views

  21. 21

    Send a file from Client to a Server

  22. 22

    File transfer from client to server

  23. 23

    Transfering values from an object in a ArrayList to an array sudoku board

  24. 24

    Getting Response from TCP server on Swift client

  25. 25

    Transfering about 300gb in files from one server to another

  26. 26

    Problems transfering codeigniter 3.0 from localhost to live server hostgator

  27. 27

    "connection unsuccessful" transfering a file via Bluetooth from Android phone

  28. 28

    Enterprise Architect - Transfering project from file to PostgreSQL repository

  29. 29

    Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

HotTag

Archive