954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Images over socket

HOw can i send and receive images over the socket in java?

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

Create ObjectOutputStream / ObjectInputStream on the socket's streams and use writeObject / readObject to transfer an Image object

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Can u show me the code

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

No, but I'll help you learn how to do it yourself.

First read the introduction to the API docs for ObjectOutputStream and ObjectInputStream. You don't need to worry about any of the advanced bits, just the most basic simple example. You will use your Socket's output and input streams instead of file streams, but otherwize it's all there

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I've been using my socket for the text messages fro receiving and sending is it possible to send and receive images through the same socket?

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

You can't mix different stream types over the same socket, but with Object streams you can send/receive pretty much anything you like, including Strings, so that will work for your text

ps: I don't know what format your images are in, but convert them to ImageIcon for sending over the object streams.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

when i used Object input and output streams the strings are corrupted in socket how to get exact string as user gives.

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

No. If you code correctly they are absolutely NOT corrupted. Object I/O Streams would be pretty useless if they corrupted anything as fundamental as a String. Post the relevant bits of your code

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
client = new Socket(serverName, serverPort);
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());;
ObjectInputStream in = new ObjectInputStream(client.getInputStream(););


when i click a login button a username should be sent to the socket . It is working fine with Data input putput streams but when i used Object streams i cannot find the exact string in socket.

for object stream, to write data to socket i wrote

String[] array={userName,password};
out.writeObject(array);


What mistake am i making?

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

and the readObject code...?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

isn't that code sufficient for writing to socket?

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

Yes. But you need to receive it at the other end. What's your code for that?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

i've just printed that by

System.out.println("data from socket is "+in.readObject().toString());
47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

That's the problem. You have written an array and you retrieve its "toString" representation - that's not the same - it gives the class name and its hash. If you look at the sample in the doc for ObjectInoutStream you will see that you have to cast the object you just read, eg

(String[]) in.readObject()

But there's no need to mess about putting the strings into an array, just write/read them directly:

out.writeObject(userName);
out.writeObject(password};
...
System.out.println("data from socket is "+ (String) in.readObject() + (String) in.readObject());

That approach works for all kinds of objects, which is why I introduced it first, but if you read the doc for object streams you would have found that there's a special pair of methods just for Strings that saves the cast...

out.writeUTF(userName);
out.writeUTF(password};
...
System.out.println("data from socket is "+ in.readUTF() + in.readUTF());
JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Object streams are not working, my all codes are working fine with data streams but when i use Object stream, i am getting some messy data instead the actual data. can u figure out the problem please help..

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

Did you try anything I said in my previous post?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

yes i had replaced all data streams class with object stream an tried all your suggestion but still i getting nothing except the garbage value ...

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

Post the write/readUTF version as you tested it, and post the "garbage values" that gave you

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

sorry i didn't get you what u want me to post?

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

You said you tries all my suggestions. In that case you must have written compiled and tested code that uses writeUTF and readUTF. Post that code.
You said "getting nothing except the garbage value ... ". It may be garbage to you, but it will probably tell me something. Post that "garbage" value.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You