Corrupted binary data

This is a discussion on Corrupted binary data within the C++ Programming forums, part of the General Programming Boards category; The file sender, when sending the data locally(127.0.0.1, or my IP address), is sent fine and works. However, when I ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    Corrupted binary data

    The file sender, when sending the data locally(127.0.0.1, or my IP address), is sent fine and works. However, when I try to send it to another computer, the data is always corrupted. Everything is sent and all, but the application in which it's suppose to open says the data is corrupted. If anyone could give me an idea of why this is happening, that would be great.

    Source:
    http://rafb.net/paste/results/YzTC9T78.html

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    You have to convert the data to network byte order before sending it and back to host byte order after recieving it. Look into htons(), htonl(), ntohs(), ntohl(), etc...
    Last edited by SlyMaelstrom; 05-03-2006 at 12:46 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    I'll try that, thanks.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > sprintf(charfilesize, "%d", filesize);
    Look at where charfilesize is pointing - nowhere.

    > int thesend = send(mysocket, buffer, sizeof(buffer), 0);
    You're assuming that send() always sends everything on the first call - this isn't true.
    Check how much you sent.
    Also, what about files which are not a multiple of 1024 in length?

    > recv(mysocket, writebuffer, 1024, 0);
    Likewise, just because you ask for 1024 doesn't mean you get 1024.
    In particular, the last block is almost always going to be less than this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  4. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21