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

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

Can u show me the code

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

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?

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.

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

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

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?

and the readObject code...?

isn't that code sufficient for writing to socket?

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

i've just printed that by

System.out.println("data from socket is "+in.readObject().toString());

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());

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..

Did you try anything I said in my previous post?

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 ...

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

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

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.

out.writeUTF(userName + " " + password);

and i read the socket by using this code which is working

buf = new byte[4096];
actualNumberOfBytesRead = client.getInputStream().read(buf);
dataString = new String(buf, 0, actualNumberOfBytesRead);

OK, so you didn't actually try any of my suggestions fully. You have some random mixture of object output with your old non-object input. That's never going to work.

Did you notice that I always had my streams and write/read methods in matched pairs?
Can you see anywhere that I said you could write an object to an ObjectOutputStream then read it from some other stream type?
Let me say this one more time very slowly...
if you writeUTF to an ObjectOutputStream then you have to readUTF from an ObjectInputStream

But when i replace this code

buf = new byte[4096];
  
      actualNumberOfBytesRead = client.getInputStream().read(buf);
  
      dataString = new String(buf, 0, actualNumberOfBytesRead);

with this one

dataString  = in.readUTF();

i get EOF exception why is it so... but fine with above method

It's hopeless trying to debug your code when you only supply tiny fragments of it. Plus you contradict yourself "still i getting nothing except the garbage value ... " or "but fine with above method".
And you still haven't posted the "garbage values" that I asked for twice.

This is really very very simple.
Open an ObjectOutputStream on the socket's output stream and write the userName and password to it by calling writeUTF twice. flush() the ObjectOutputStream for good luck.
At the receiving end open an ObjectInputStream on the socket's input stream and read the userName and password by calling readUTF twice.

If you can't make that work then post ALL the relevant code and an exact copy of all relevant output

commented: 2 Stars for extra effort on this one +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.