• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transferring image via socket

 
Ranch Hand
Posts: 98
Angular Framework Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why, what problem are you having? What's not working? TellTheDetails please.

Welcome to CodeRanch!
 
Dinesh Kumar Ramakrishnan
Ranch Hand
Posts: 98
Angular Framework Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 )
 
Dinesh Kumar Ramakrishnan
Ranch Hand
Posts: 98
Angular Framework Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No ObjetOutputStream.reset() is not working

it shows an exception
Errorjava.net.SocketException: Connection reset by peer: socket write error
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Ramakrishnan
Ranch Hand
Posts: 98
Angular Framework Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Ramakrishnan
Ranch Hand
Posts: 98
Angular Framework Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post your code ..i wanted to see what actually you are doing now ....
 
Dinesh Kumar Ramakrishnan
Ranch Hand
Posts: 98
Angular Framework Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic