Thread: File Transfer Using UDP/IP

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    File Transfer Using UDP/IP

    I'll start by saying this is an assignment for a class - so I'm not looking for a complete solution - just some guidance on a few questions. I am a C novice at best.

    My assignment is to write a program using UDP/IP to send a file from one machine to another within our network. I've got the mechanics of sending a char message from one machine to the next so I'm working on figuring out how to read, parse, add the header and CRC information prior to sending.

    What I have so far....

    File: send.c
    Code:
    #include <sys/types.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    
    #define MSG "This is a test message!"
    #define BUFMAX 100
    
    //Command Arguments:
    // [0] - send
    // [1] - destination machine
    // [2] - destination port number
    // [3] - file to be sent
    
    int main(int argc, char* argv[]){
        int sk;
        char buf[BUFMAX];
    
        FILE *fp;
        fp = fopen(argv[3],"r");
    
        fread(buf, BUFMAX, 1, fp);
    
        // My question is in this area...how do I get binary values?
    
        int k;
        for(k=0;k<BUFMAX;k++){
          printf("The buffer contains %d\n",(char *)buf[k]);
        }
    
        struct sockaddr_in remote;
        struct hostent *hp;
    
        sk = socket(AF_INET, SOCK_DGRAM, 0);
    
        remote.sin_family = AF_INET;
    
    
        hp = gethostbyname(argv[1]);
    
        if(hp==NULL){
            printf("Can't find hostname. %s\n", argv[1]);
            exit(1);
        }
    
        bcopy(hp->h_addr, &remote.sin_addr.s_addr, hp->h_length);
    
        remote.sin_port = ntohs(atoi(argv[2]));
    
        dlsendto(sk, MSG, strlen(MSG)+1, 0, &remote,
        sizeof(remote),0);
        read(sk,buf,BUFMAX);
        printf("%s\n", buf);
        close(sk);
    }
    What I'm trying to figure out is this....I need to read in the file, parse it into a 100 bit message, add a header and CRC-16 codes to create a frame, then send it through the socket to be validated, assembled and saved on the other side.

    I can read the file, but when I output the values I'm getting characters instead of binary. Is there another way to display the data so I can validate what's being passed? I've spent a great deal of time looking through articles on Binary files, text to binary, and can't seem to find an example that explains what I'm doing wrong.

    Any guidance would be great.

  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
    If you stick to sending just text files, it's very easy to see whether you received what you sent.

    There's no difference when it comes to sending binary data, so long as you're using fread/fwrite to read/write the file.

    > I need to read in the file, parse it into a 100 bit message
    Do you mean 100 bits?
    It's like 12.5 bytes.
    It's not necessarily a problem, but it does need a bit of extra care when dealing with the buffers.
    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
    Mar 2011
    Posts
    10

    Clarification

    Ok - so perhaps I'm not explaining this properly.

    The text file contains a message. For the sake of making this easy - lets assume myFile.txt contains the message "Hello World".

    I need take that file and pass it from one server to another using UDP/IP. So the characters become:

    H = 0100 1000
    e = 0110 0101
    etc.

    I need to take 100 bits at a time, if the message contains more than 100 bits, then I need break it into 100 bit sections. I need to do a CRC calculation on it and write an 8-bit header (format provided by the professor. So my final message will be:

    Header (8bits) + Message (100bits) + CRC(8 bits)

    This frame is sent to the receiving machine, the CRC is calculated to ensure no errors and re-assembled into the txt file. There's more - i.e. ACK response, resending, etc - but I'm not that far yet.

    Hoping this helps make sense of what I'm saying.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Are you sure it's 100 bits and not 100 bytes? The latter would make more sense because 1) you're putting more data into a packet, and 2) you don't have to worry about dealing with partial bytes.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Bytes / Bits

    So - I'm re-reading the assignment. It says in one of the last paragraphs that "the maximum frame size is 128 bytes long". So perhaps your correct.

    I am slightly confused however. How does this change anything? I was assuming that the letters would convert somehow to bytes.

    I'm afraid I don't understand how to go from Text to Bytes any more than I know to go from Text to Bits.

    Thanks for the thoughts.

    So then, the message I need to send is 1 byte header + 100 byte chunks of the message + CRC code (I think). How can I take a text file and parse it into 100 byte chunks?
    Last edited by AdamOhio76; 03-19-2011 at 02:17 PM. Reason: Clarification

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    char buff[128];
    while ( (n=fread(&buff[28],1,100,fp) > 0 ) {
      // do something with n bytes stored in buff
      // add 28 bytes of whatever to the start in buff[0] to buff[27]
    }
    There is no difference between characters and bytes - not at this level of code anyway.
    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
    Mar 2011
    Posts
    10

    Dang it

    Ok - thanks Salem & Clairvoyant1332.

    I spent so much time focused on searching for bit conversion it never occurred to me that each character was a byte.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-08-2010, 02:03 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM