Question

This code sends and recv s txt file perfectly but cannot do it to otehr formats like .exe or .img. Please help me with these as I need to use htonl or htons?? Take a look!!

Here is the server side recv function ::

 if (socket_type != SOCK_DGRAM)
        {

                fi = fopen (final,"wb");
                retval = recv(msgsock, recv_buf, strlen(recv_buf), 0);
                /*recv_buf[retval] = '\0';
                fprintf (fi,"%s",recv_buf);*/

                int i;
                i=atoi(recv_buf);
                char *q;
                q=(char *)malloc(i*sizeof(char));
                retval = recv(msgsock, q, strlen(q), 0);
                //printf ("%s",q);
                fwrite(q,i,1,fi);
                fclose(fi);

        }
        else
        {
            retval = recvfrom(msgsock,recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&from, &fromlen);
            printf("Server: Received datagram from %s\n", inet_ntoa(from.sin_addr));
            printf ("SOCK_DGRAM");
        }

        if (retval == SOCKET_ERROR)
        {
            fprintf(stderr,"Server: recv() failed: error %d\n", WSAGetLastError());
            closesocket(msgsock);
            //continue;
        }
        else
            printf("Server: recv() is OK.\n");

        if (retval == 0)
        {
            printf("Server: Client closed connection.\n");
            closesocket(msgsock);
                //continue;
        }
        printf("Server: Received %d bytes, data from client\n", retval);

The client side sending function :::

void send_command()
{
    int bytesent;
    FILE *file_out;
    //file_out = fopen(file_path,"rb");
    char str_all[100000];//flag [30]="end";

    ///////////////////////getsize//////////////
    char fsize[5];
    int filesize;
    file_out = fopen(file_path, "rb");
    fseek(file_out, 0, SEEK_END);
    filesize = ftell(file_out);
    rewind (file_out);
    itoa (filesize,fsize,10);
    /////////////////////////////////////////////
    send (ConnectSocket, fsize, strlen (fsize), 0);

    char *r = (char *)malloc (filesize * sizeof(char));

    fread(r,filesize,1,file_out);
    bytesent = send( ConnectSocket, r, strlen(r), 0 );
    printf("\nClient: Bytes sent: %ld\n", bytesent);
    fclose (file_out);

    /*while (fscanf(file_out,"%s",&str_all) != EOF)
    {
        bytesent = send( ConnectSocket, str_all, strlen(str_all), 0 );
        printf("\nClient: Bytes sent: %ld\n", bytesent);
        //Sleep(500);
    }*/

    /*printf("%s",flag);
    send( ConnectSocket, flag, strlen(flag), 0 );*/
    WSACleanup();
    //return 0;
    }
Was it helpful?

Solution

OK, there are multiple issues with your program.

  • You are transferring binary data. The receiver is only going to see a sequence of bytes. There is no way for the receiver to know the end of the data, since all possible values of char are legal data values. If you were sending text data, you could say that a 0 signifies the end of the data, but now you can't. So, you have to decide on a "protocol" between the server and the client—the simplest is that the server sends the length of the data in the first 4 bytes (read up on ntonl() and ntohl() for how to do this portably). Then, the receiver will know exactly how many bytes to read.
  • You declare the receiver buffer as char *recv_buf, and similarly for recv_buf1. You don't allocate any storage for any of the two pointers, so they aren't pointing to anywhere useful. Then, your recv call is: recv(msgsock, recv_buf, sizeof(recv_buf), 0); This also has problems. The first is the one mentioned above: you don't have storage for recv_buf. The second is that after you do allocate storage for recv_buf, you are taking the size of a char pointer instead of the length of the buffer recv points to. One easy way to solve both the issues would be to declare recv_buf as: char recv_buf[SIZE]; and then use sizeof recv_buf in the recv() call.

I haven't looked at the rest of your code. You probably need a good C and network programming introduction.

OTHER TIPS

I think you're confusing the null-termination of a C string with the end of a packet sent on a socket. There is no "termination" of a packet, it's just a string of bytes. Zeros are completely legal, and you pass (and receive) the length explicitly. You certainly don't need to use the out-of-band facilities to receive multiple packets. Can you be more specific about what you're asking?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top