Thread: Server-side not receiving file sent by Client-side?

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    4

    Angry Server-side not receiving file sent by Client-side?

    Here's the link to my code:

    Sockets: Server accepts file from Client * GitHub

    Client stuff happens on line 150
    Server stuff happens on line 281

    My client is sending the file fine... however, my server doesn't make the file and receive it for some reason. Any clue why?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    > Any clue why?
    Well posting the message logs of both client and server would have been a big help.


    First off, having a 300 line main is horrible.
    Split the whole thing into functions which
    - parse arguments
    - handle the whole client
    - handle the whole server

    Client and server need to be further broken down into
    - create the connection
    - handle the file transfer
    - close the connection

    2. A whole bunch of variables which are declared float should be integral types.
    Convert to float WHEN you need to do the maths, not before.

    Code:
                    unsigned char buff[256] = {0};
                    float nread = fread(buff, 1, sendSize, fp);
    3. Lying about your buffer sizes is a sure way to mess things up.
    Besides, the entire loop is a waste of code. The other file read loop will do the job just perfectly. You've already done fseek(fp, sendSize, SEEK_SET);

    Code:
                FILE *fp;
    
                float total = 0;
                float bytesReceived = 0;
                char buff[256];
                memset(buff, '0', sizeof(buff));
                float percentage = (total / bytesReceived) * 100;
                while ((bytesReceived = read(msgSock, buff, 256)) > 0) {
                    fwrite(buff, sizeof(char), sizeof(buff), fp);
                    printf("\r%s: Percentage received: %.2f", NAME_C, percentage);
    }
    1. You didn't open the file.
    2. the memset is a waste of time, you're going to overwrite the buffer anyway.
    3. the fwrite should use bytesReceived and not sizeof(buff).


    > if (returnValue = WSAStartup(0x202, &wsaData) != 0)
    You need to have explicit parentheses here. You've written this.
    if (returnValue = (WSAStartup(0x202, &wsaData) != 0))

    > printf("%s: Accepted connection from %s", inet_ntoa(outsideSock.sin_addr));
    This is missing an argument for the second %s
    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
    Oct 2016
    Posts
    4
    How does this look instead?

    Code:
     /* Receive data from client */
                char* fileName = "test.txt";
                FILE *fp = fopen(fileName, "w");;
    
    
                float total = 0;
                float bytesReceived;
                char buff[256];
                float percentage = (bytesReceived / total) * 100;
                while ((bytesReceived = read(listenSock, buff, 256)) > 0) {
                    fwrite(buff, sizeof(char), bytesReceived, fp);
                    printf("\r%s: Percentage received: %.2f", NAME_C, percentage);
                }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    Well it's better, apart from your percentage complete being constant garbage.

    How it looks doesn't matter so much - the question is, does it work?

    It's up to you to compile and test the changes.
    If it doesn't work, you report on the latest set of messages and observations.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    4
    So, I finally figured it out. It's on my sever-side. I can make files now, but my server-side is screwy. The output shows weird characters that aren't supposed to be there:

    Client:

    Code:
    if(sendSize <=0){
            for(;;){
                unsignedchar buff[256]={0};
                int nread = fread(buff,1,256, fp);
    
                total = total + nread;
                percentage =(total / fFileSize)*100;
                printf("\r%s: Percentage sent: %.2f", NAME_C, percentage);
                /* Send data in 256 byte chunks */
                if(nread >0){
                    send(clientSock, buff,sizeof(buff),0);
                }
    
                if(nread <256){
                    if(feof(fp)){
                        printf("\nSend Success!\n");
                        break;
                    }
                }
            }
            //printf("%.2f", total);
        }
    
    


    Server:

    Code:
    /* Receive data from client */
            char* fileName ="test.txt";
            FILE*fp = fopen(fileName,"w+");;
    
            float total =0;
            float bytesReceived;
            unsignedchar buff[256]={0};
            float percentage =(bytesReceived / total)*100;
    
            while((bytesReceived = recv(listenSock, buff,sizeof(buff),0))<0){
                //bytesReceived = recv(listenSock, buff, 256, 0);
                if(bytesReceived >0){
                    printf("DONE");
                }
                //total = total + bytesReceived;
                fwrite(buff,sizeof(char), bytesReceived, fp);
                //printf("\r%s: Percentage received: %.2f", NAME_C, percentage);
            }
    

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    > send(clientSock, buff,sizeof(buff),0);
    Again - you're sending the size of the buffer, not the number of chars actually read (nread).
    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.

  7. #7
    Registered User
    Join Date
    Oct 2016
    Posts
    4
    I changed it back to nread and it still came up with the same problem. :/

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    If you want to read and write exactly, you need to use "rb" and "wb" modes in windows.

    Otherwise all the \r\n translation messes everything up.
    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. Side by side error in VS2013
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2015, 09:26 AM
  2. Replies: 26
    Last Post: 03-08-2011, 05:23 PM
  3. placing two bmp images side by side
    By nina_code in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2009, 11:10 AM
  4. Client application having problem receiving from server side?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-04-2005, 02:58 PM
  5. Printing text side by side
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 11:46 AM

Tags for this Thread