views:

330

answers:

2

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;
    }
+1  A: 

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?

Andy Ross
I want to send a file like .mp3 or any format and recive it and save it like .mp3. So the both sides have to send and recv simultaneously!! When I send a text file the server side recieves only the length before SPACE or ENTER(/n). So I need to send a file as it is. The problem is in the server side as it cannot recieve simultaneously.
I'm not following. I think you need to back out and read a few tutorials on socket programming. Stevens' Unix Network Programming is the classic book, but there's lots of web content available.
Andy Ross
+2  A: 
Alok
Allocating is a problem I found but I am really noob at this and learning it like 2 weeks now. htons is a solution I was thinking but cannot implement. Thanks for the answer!!
If allocating is a problem, you can use an array as I said, but in the long run, you should learn pointers and malloc and friends in C. For networking, there are many good tutorials online, or you can read Stevens' books.
Alok