Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to send images through a network

networking

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1 KansaiRobot

KansaiRobot

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 09 January 2014 - 04:21 PM

Hello everybody and thanks for your help :)

 

I have read this tutorial on Network Programming
 
Beej's Guide to Network Programming
 
 
It is quite useful. With this I have been able to send text over a virtual LAN connection and also over a LAN cable from one microcontroller running (a variant of ) uCLinux to a PC running a virtual Debian Linux. 
 
So far so good.
 
My question is how do I send images, ergo graphic data? Can I use the same technique or have to pack the data in some way? :confused:
 
Is there a resource or book or tutorial which deals with this?
 
Any help or pointer will be greatly appreciated
 
Thanks
 
Kansai


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 09 January 2014 - 09:23 PM

Well, the key here being "sending data". An image is a file, or a fixed length data stream. Where both, data packaging and data interpretation are done at the ends of communication. So, there's nothing special about that.



#3 KansaiRobot

KansaiRobot

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 10 January 2014 - 02:02 AM

Thank you for your reply.

 

About data packaging , are there recommended strategies that I should be using? in particular for image files? I am thinking of sending byte by byte or in chunks...



#4 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 10 January 2014 - 02:20 AM

KansaiRobot, since you're using a stream socket (I assume you're not using UDP.) Don't think in terms of packets, but in terms of a data stream. The protocol will automatically split up data into packets for you.

 

Therefore, there are a few methods you can use (some pseudo code:)

#Simple byte by byte sending (very inefficient)
 
socket <- open_socket
image_stream <- open_file(image)
while(!end_of_file(image_stream)) {
    socket.send <- image_stream.read
}
socket close
 
# Better buffered approach
 
socket <- open_socket
image_stream <- open_file(image)
buffer[x] <- create buffer of size x
while(!end_of_file(image_stream)) {
    buffer <- read from image_stream up to X bytes, store how many as Y
    socket.send <- buffer [0...Y]
}

 

Now in Linux there's a function called sendfile which allows you to open a file and just pass all the data to another file descriptor.

 

Assuming your binary data doesn't have any endianness issues, you'll be fine.


Creating SEGFAULTs since 1995.


#5 KansaiRobot

KansaiRobot

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 13 January 2014 - 04:31 PM

Thank you for your reply. 

I just have a question. In the examples you put, I can see clearly that the second one is cutting the data in chunks but how is the first one being byte by byte? If anything I see it sending it all at once??



#6 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts

Posted 13 January 2014 - 05:21 PM

My understanding of Evan's first example is that image_stream is being read and sent concurrently, not all at once. So, for each byte that is read by image_stream.read, that same byte is sent by socket.send.


                                                                                                                                                                            FACEB00K Likes this.


#7 KansaiRobot

KansaiRobot

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 13 January 2014 - 06:14 PM

Now I see it! Thanks!

 

Anyway, I ll code some today to try these ideas and will get back  :)



#8 KansaiRobot

KansaiRobot

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 15 January 2014 - 08:49 PM

Thank you everybody for your help. I actually managed to send an image file (jpg). For some reason I lost some bytes but the image remained the same. I will post the details later in this thread as a help for anybody who would have the same problem. In the meantime I have another question that I will post in other thread






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download