File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Transferring image via socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Transferring image via socket" Watch "Transferring image via socket" New topic
Author

Transferring image via socket

Dinesh Kumar Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 25

Hi to all...!
I am trying to write a code for transfer a image file through socket the sever reads the image and stores in a byte array. and the problem is while writing a byte array to client the connection was reset in client side..



The Operations are:
1.Client Send the image File name to server
2.Server read the image and send the image size to the client
3.then client receive the image size and allocate the receiving Byte array in client side
4.then server start write the image byte array to the client then connection was reset by client

Here My Code
Sever File


Client Side



I am using ObjectOutputStream class for my socket..
Please help me to solve the problem the connection was reset by client everytime

-Đinê$h
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Why, what problem are you having? What's not working? TellTheDetails please.

Welcome to CodeRanch!
Dinesh Kumar Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 25

Stephan van Hulst wrote:Why, what problem are you having? What's not working? TellTheDetails please.

Welcome to CodeRanch!


While Transmitting the size it was working well the client was able to receive the size and allocate the byte array..

when i was start transmit the image byte array the client was receive the half of the byre array and server shows the error Connection Was Reset...
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Dinesh here wrote:
Stephan van Hulst wrote:Why, what problem are you having? What's not working? TellTheDetails please.

Welcome to CodeRanch!


While Transmitting the size it was working well the client was able to receive the size and allocate the byte array..

when i was start transmit the image byte array the client was receive the half of the byre array and server shows the error Connection Was Reset...

You are making your way difficult, use my way of doing this

1. load an image using ImageIO.read(inputstream)
2. cast bufferedImage to Image
3. put Image in ImageIcon c'tor
4. use serialization to to send ImageIcon object via Socket

Client side
1. readObject will get you the imageIcon
2. play with it

----------------------------------------------------------------------------------------------------------------------
solution to your way of doing it , ....use objectOutputStream.reset() after flushing your data (give this a try and see if this works )

The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Dinesh Kumar Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 25

No ObjetOutputStream.reset() is not working

it shows an exception
Errorjava.net.SocketException: Connection reset by peer: socket write error
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

That Exception you got because you are trying to reset the stream before all the data has been written to the stream, so what you have to do is after you do do and then reset the stream , it will work ....
Dinesh Kumar Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 25

naved momin wrote:That Exception you got because you are trying to reset the stream before all the data has been written to the stream, so what you have to do is after you do do and then reset the stream , it will work ....



i change my server code like your advice but is not working...!


The image array half of the content will received correctly. But reaming 75% of the image data is only zeros...! please Help ME
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

you are doing it all wrong , i told you first write using 2nd flush the stream like 3rd join for joining use only this code there is no need to get instance of current Thread , and finally 4th reset the stream
Dinesh Kumar Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 25

It Shows an error.when i was write code like this




Error:
sim_sock.java:46: non-static method join() cannot be referenced from a static context
Thread.join();
^


I think it needs instance..!(But it will not work)
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

can you post your code ..i wanted to see what actually you are doing now ....
Dinesh Kumar Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 25

naved momin wrote:can you post your code ..i wanted to see what actually you are doing now ....


Here My Code
Server


Client

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Your way of doing is completely wrong( sorry to say ) , what i briefy understand is that you are reading a file and sending it to server ?
if thats your requirement then do this instead of what you are currently doing

understand the steps before coding , it will help
server

follow this pseudo code

after accepting the connection
read Object and cast it to Byte
use ImageIcon to construct a new image using new ImageIcon(byte[] byte) c'tor
display the new image in frame or do whatever ....


client

create new FileInputStream fis
get the bytes of data from the above stream
and write it using OOS.write(byte[] data)
remember to do flush OOS.flush();


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transferring image via socket