Thread: Not receiving file content on Server side.

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

    Not receiving file content on Server side.

    I believe everything is in working order. The only problem I have with my code is that the server-side isn't getting all the information sent to the file that it is creating from the client-side.

    This is using Winsock, by the way. Anybody here maybe know what's wrong with the file transfer process?

    Client:

    Code:
    int main(){
        WSADATA Winsock;
        SOCKET Socket,Sub;
        Addr addr;
        IncomingAddress incomingAddress;
        intAddressLen=sizeof(IncomingAddress);
    
        WSAStartup(MAKEWORD(2,2),&Winsock);
    
        if(LOBYTE(Winsock.wVersion)!=2|| HIBYTE(Winsock.wVersion)!=2){
            WSACleanup();
            return0;
        }
    
        Socket= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
        ZeroMemory(&addr,sizeof(Addr));
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr("127.0.0.1");
        addr.sin_port = htons(6000);
    
        if(connect(Socket,(sockaddr*)&addr,sizeof(Addr))<0){
            printf("Connection Failed!\n");
            getchar();
            return0;
        }
    
            char*ClientIP= inet_ntoa(incomingAddress.sin_addr);
            intClientPort= ntohs(incomingAddress.sin_port);
            printf("Client connected!\n");
            printf("IP: %s:%d\n",ClientIP,ClientPort);
    
            printf("Sending file... \n");
    
            FILE*File;
            char*Buffer;
            unsignedlongSize;
    
            File= fopen("hey.txt","rb+");
            if(!File){
                printf("Error while reading the file!\n");
                getchar();
                return0;
            }
    
            fseek(File,0, SEEK_END);
            Size= ftell(File);
            fseek(File,0, SEEK_SET);
    
            Buffer= malloc(Size);
    
            fread(Buffer,Size,1,File);
            char cSize[MAX_PATH];
            sprintf(cSize,"%i",Size);
    
            fclose(File);
            send(Sub, cSize, MAX_PATH,0);// File size
    
            intOffset=0;
            while(Size>Offset){
                intAmount= send(Sub,Buffer+Offset,Size-Offset,0);
    
                if(Amount<=0){
                    printf("Error: "+WSAGetLastError());
                    break;
                }else{
                    Offset+=Amount;
                }
            }
            free(Buffer);
            closesocket(Sub);
            closesocket(Socket);
            WSACleanup();
        return0;
    }
    


    Server:

    Code:
    int main(){
        WSADATA Winsock;
        SOCKET Socket,Sub;
        Addr addr;
        IncomingAddress incomingAddress;
        intAddressLen=sizeof(IncomingAddress);
    
        // Start up Winsock
        WSAStartup(MAKEWORD(2,2),&Winsock);
    
        if(LOBYTE(Winsock.wVersion)!=2|| HIBYTE(Winsock.wVersion)!=2){
            WSACleanup();
            return0;
        }
    
        Socket= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
        // Memset alternative
        ZeroMemory(&addr,sizeof(Addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(portNumber);
        bind(Socket,(sockaddr*)&addr,sizeof(Addr));
    
        if(listen(Socket,1)== SOCKET_ERROR){
            printf("Listening error!\n");
        }else{
            printf("Listening...\n");
        }
    
        if(Sub= accept(Socket,(sockaddr*)&incomingAddress,&AddressLen)){
            intSize;
        char*Filesize= malloc(1024);
    
        if(recv(Socket,Filesize,1024,0)){// File Size
            Size= atoi((constchar*)Filesize);
            printf("File size: %d\n",Size);
        }
    
        char*Buffer= malloc(Size);
    
        intOffset=0;
        while(Size>Offset){
    
    
    
            intAmount= recv(Socket,Buffer+Offset,Size-Offset,0);
    
            if(Amount<=0){
                printf("Error: "+WSAGetLastError());
            }else{
                Offset+=Amount;
            }
        }
    
        FILE*File;
        File= fopen("test.txt","wb+");
        fwrite(Buffer,1,Size,File);
        fclose(File);
    
        getchar();
        closesocket(Socket);
        WSACleanup();
    
        }
    }
    return0;
    }
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > send(Sub, cSize, MAX_PATH,0);// File size
    You never assign Sub in the client.

    > char*Filesize= malloc(1024);
    Why aren't you using the constant MAX_PATH instead?

    Sending 1K of data for a string which is a handful of chars seems wasteful.
    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
    2
    Alright. Check out this revision:

    Client:

    Code:
     WSADATA Winsock;        SOCKET Socket;
            Addr addr;
            IncomingAddress incomingAddress;
            int AddressLen = sizeof(IncomingAddress);
    
    
            WSAStartup(MAKEWORD(2, 2), &Winsock);
    
    
            if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
                WSACleanup();
                return 0;
            }
    
    
            Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
            ZeroMemory(&addr, sizeof(Addr));
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = inet_addr("127.0.0.1");
            addr.sin_port = htons(6000);
    
    
            if (connect(Socket, (sockaddr*) &addr, sizeof(Addr)) < 0) {
                printf("Connection Failed!\n");
                getchar();
                return 0;
            }
    
    
                char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
                int ClientPort = ntohs(incomingAddress.sin_port);
                printf("Client connected!\n");
                printf("IP: %s:%d\n", ClientIP, ClientPort);
    
    
                printf("Sending file... \n");
    
    
                FILE *File;
                char *Buffer;
                unsigned long Size;
    
    
                File = fopen("test.txt", "rb+");
                if (!File) {
                    printf("Error while reading the file!\n");
                    getchar();
                    return 0;
                }
    
    
                fseek(File, 0, SEEK_END);
                Size = ftell(File);
                fseek(File, 0, SEEK_SET);
    
    
                Buffer = malloc(Size);
    
    
                fread(Buffer, Size, 1, File);
                char cSize[MAX_PATH];
                sprintf(cSize, "%i", Size);
    
    
                fclose(File);
                send(socket, cSize, MAX_PATH, 0); // File size
    
    
                int Offset = 0;
                while (Size > Offset) {
                    int Amount = send(socket, Buffer + Offset, Size - Offset, 0);
    
    
                    if (Amount <= 0) {
                        printf("Error: " + WSAGetLastError());
                        break;
                    } else {
                        Offset += Amount;
                    }
                }
                free(Buffer);
                closesocket(Socket);
                WSACleanup();
                printf("File sent!\n");
            return 0;
    Server:

    Code:
    WSADATA Winsock;
            SOCKET Socket, Sub;
            Addr addr;
            IncomingAddress incomingAddress;
            int AddressLen = sizeof(IncomingAddress);
    
    
            // Start up Winsock
            WSAStartup(MAKEWORD(2, 2), &Winsock);
    
    
            if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
                WSACleanup();
                return 0;
            }
    
    
            Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
            // Memset alternative
            ZeroMemory(&addr, sizeof(Addr));
            addr.sin_family = AF_INET;
            addr.sin_port = htons(portNumber);
            bind(Socket, (sockaddr*) &addr, sizeof(Addr));
    
    
            if (listen(Socket, 1) == SOCKET_ERROR) {
                printf("Listening error!\n");
            } else {
                printf("Listening...\n");
            }
    
    
            if (Sub = accept(Socket, (sockaddr*) &incomingAddress, &AddressLen)) {
                int Size;
            char *Filesize = malloc(MAX_PATH);
    
    
            if (recv(Socket, Filesize, MAX_PATH, 0)) { // File Size
                Size = atoi((const char*) Filesize);
                printf("File size: %d\n", Size);
            }
    
    
            char *Buffer = malloc(Size);
    
    
            int Offset = 0;
            while (Size > Offset) {
                int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);
    
    
                if (Amount <= 0) {
                    printf("Error: " + WSAGetLastError());
                } else {
                    Offset += Amount;
                }
            }
    
    
            FILE *File;
            File = fopen("test.txt", "wb+");
            fwrite(Buffer, 1, Size, File);
            fclose(File);
    
    
            getchar();
            closesocket(Socket);
            WSACleanup();
    
    
            }
       }
       return 0;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > send(socket, cSize, MAX_PATH, 0); // File size
    Do you get even get the teensiest error message with this?

    socket is the name of a FUNCTION, not a variable name containing the result of calling socket / connect / accept.
    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. Replies: 7
    Last Post: 10-14-2016, 12:02 PM
  2. Replies: 29
    Last Post: 10-22-2009, 11:01 AM
  3. Server Side
    By lruc in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 03-04-2009, 07:14 PM
  4. Receiving content
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 12-18-2005, 03:17 AM
  5. 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

Tags for this Thread