• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way for Reading/Writing BufferedImage over Socket ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m sending bufferedImage in compressed jpg format to remote machine over socket with this code


over the remote machine i m reading the above written image like this

I m reading data using BufferedReader because server is sending some commands as well, apart from the images, so i need to get that also some times....
So do you guys think this is a write way to read images over the socket connection ....If you have any other method which is less error prone and might increase the efficiency of my application then your suggestion are welcome .....
And if you think this way will not work then also your comment or suggestion are most welcome ....
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use a BufferedReader wrapped around the socket's InputStream and read from both. The BufferedReader can (and will!) read "ahead", consuming bytes that actually belong to the image.
If you need to mix text and data, you should use a DataOutputStream / DataInputStream pair.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You can't use a BufferedReader wrapped around the socket's InputStream and read from both. The BufferedReader can (and will!) read "ahead", consuming bytes that actually belong to the image.
If you need to mix text and data, you should use a DataOutputStream / DataInputStream pair.

thanks for replying rob ...now i want to share some problem with you lets see can we come up with nice solution
Yes, you were right bufferedReader was consuming the "ahead" of image that's why i was getting socketcorruptexception..so after that i have change the solution and now i m sending serialize ImageIcon object and text of commands from client to server through ObjectOutputStream and receiving with ObjectInputStream..then soon after i was receiving OutOfMemoryException so increasing heap size din't help me but using ObjectOutputStream.reset() just after sending the ImageIcon object helps me ...and now i m not getting any OutOfMemoryException (Now my VM heap size is 1048m) .......but do you think this is a right way of sending images from client to server ??? Or should i use DataInputStream/DataOutputStream as you mention earlier ?

another question is why fix the OutOfMemoryException i mean i have read the documentation of reset method but still i m not getting it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!