Sending image

This is a discussion on Sending image within the Networking/Device Communication forums, part of the General Programming Boards category; Ok thank you Salem but its still not too clear to me how these functions work. For example when exactly ...

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    825
    Ok thank you Salem but its still not too clear to me how these functions work.

    For example when exactly a recv() function stops recieving?

    I imagine its something like the send() function knows how many bytes it has to send (because we have to precise it) and it gets transmitted to the recv function and when it recieved the number of bytes it stops recieving.

    Because if i follow your reasoning that it would recieve every other send() function, it should never stop recieving.

    So there must be something that tells him to stop.

    Otherwise in every code there would be extra delimiter characters added to it and i never see it in any code.

    Btw i managed to fix the code by adding Sleep() on the sender side but i as i read everywhere its not the real solution.

    I just cant believe they made it so complicated to send and recieve data.
    And every code i see is like mine or even worse because theyre not even sending the size of the file before.
    So how come its working for everybody and you telling me to send delimiter characters in addition?
    Last edited by Ducky; 08-14-2009 at 07:36 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    If the connection is lost half-way though, recv() will return 0 at some point, and there will be no more bytes.

    Without the length up-front, how would you know whether you had received the whole image or not?
    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.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    825
    If the connection is lost either way you are not getting what you wanted so its not worth considering.

    Im talking about when everything is going well.

    When does the recv() function stop recieving?
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    825
    Ok i understood that the TCP/IP stack sends data as he pleases.

    Now after what kind of character could i search that doesnt exist in a binary file?

    Does the \0 character only exist at the end of files and streams?
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ducky View Post
    Does the \0 character only exist at the end of files and streams?
    No; AFAIK the main point of using a "binary" vs. "text" stream is that C strings are terminated by '\0' whereas binary data can contain a 0 as part of the actual data. So there is no terminating character.

    With TCP/IP networking, there is a Total Length field that is part of the IP header. You can use that to decide how much is supposed to be received and loop until completed.

    See here:
    http://www.rfc-editor.org/rfc/rfc791.txt
    and just keep searching for "Total Length".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    825
    Thank you MK27!

    I checked out the link you gave me:

    Total Length
    The internet header field Total Length is the length of the
    datagram in octets including internet header and data.

    So its not the data size and btw i dont even know how to access it.

    Looks very complicated. How come that there are hundreds of networking codes and tutorials
    and i havent seen one explaining this?
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    825
    Btw i discovered the problem with my code.

    Even the size of the first recv()' buffer,
    (the one that recieves only the size of the file, which is just a number so it cant be bigger than 4.000.000.000 which is only 10 characters long so: char buf [10] would be enough)
    must be the size of the data that the second recv() will recieve.

    This completely not making sense but its like that or otherwise the program crashes.
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ducky View Post
    So its not the data size and btw i dont even know how to access it.

    Looks very complicated. How come that there are hundreds of networking codes and tutorials
    and i havent seen one explaining this?
    Well, the tcp/ip header is a constant size so if you subtract that you will have the data size. Accessing it is difficult (you can use pcap), IMO not really worth it.

    The reason is: tcp/ip networking is in practice done using some further "protocol"; if you were communicating with an HTTP internet server, you would use the HTTP protocol, which means everything has a http header including a "Content-Length:" field. This is read as plain text in the first part of an http transmission.

    So to send an image using HTTP, you must first send a header, then the image data. If you are writing your own client/server thing, you don't have to use HTTP, but you make up your own "protocol" to perform the same purpose. The way I usually do it is this: Send the transmission length as a plain text number before the rest of the data. Then at the other end you just pull it off like this:
    Code:
    int len;
    sscanf(buffer,"%d",len);
    So from the transmission end, you just send a short string with the length of the data before you send the data.

    You can also send an int (four bytes) but a test string is easier.
    Last edited by MK27; 08-15-2009 at 12:24 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    825
    Thank you MK27!

    sscanf is a great function, i didnt know about it.

    Its great because it retrieves the number whatever is the size.
    Compiler MSVC++ 2013 with Code::Blocks.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 01:46 PM
  3. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 01:10 PM
  4. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 01:46 PM
  5. Replies: 4
    Last Post: 03-02-2003, 09:12 AM

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