I am (still) writing a server+client application.

I start the server. After a connection is accept()-ed, I start a thread to handle that connection.

On the handler thread, I add the client nickname(String) and Socket to a Hashtable.

private Hashtable<String, Socket> clients = new Hashtable<String, Socket>();

clients.put(nickname, socket);

After a client connects, he can send messages to other clients. This is done by this function:

public void forwardMessage(String destination, String message){ 

    	System.out.print("forward(destination, message): ");
    	if (serverMain.nameToSocket(destination)!=null){
    											try {
    		Socket socketDestination = serverMain.getHashClients().get(destination);

    		System.out.println("name/socket destination= "+destination+ " " +serverMain.getHashClients().get(destination));
    		ObjectOutputStream oos = new ObjectOutputStream(socketDestination.getOutputStream());
            //return true;
    											} catch (IOException e) {e.printStackTrace();}

//return false;


However, on the client side, receiving fails

java.io.StreamCorruptedException: invalid type code: AC
	at java.io.ObjectInputStream.readObject0(Unknown Source)
	at java.io.ObjectInputStream.readObject(Unknown Source)
	at defaultPackage.ClientHandler.run(ClientHandler.java:44)
	at java.lang.Thread.run(Unknown Source)

at this line:

message = (String) ois.readObject();

What am I doing wrong?

Recommended Answers

All 8 Replies

Creating a new ObjectOutputStream around an OutputStream will cause a new "header" object to be created and written to the Stream, which you do not want to do more than once (unless you are also creating a new ObjectInputStream on the other end with every "read", both of these is which an extreme waste of effort and bandwidth).

Create an Object to hold both a Socket and an ObjectOutputStream and add these objects to your hashtable rather than the socket.

Create an Object to hold both a Socket and an ObjectOutputStream and add these objects to your hashtable rather than the socket.

I don't understand. How do I do this?

By creating another class, of course. A simple class with two instance fields. and if you don't feel like making the setter/getter methods and/or a special constructor simply make the fields public.

Ah! I was thinking about storing two values with one hashtable entry. Thanks, I'll try this.

I stopped making multiple ObjectOut/InnputStreams. Now it fails in a new place, on a readObject call, while I'm not even sending something through the socket.

It just freezes executing this:

ObjectInputStream ois = null;
			   try {
ois = new ObjectInputStream(socket.getInputStream());  }catch (IOException e1){e1.printStackTrace();}

I can't, for the life of me, figure out what's wrong now. If anyone can take a look at the code, it would be greatly appreciated.

Run: Src -> defaultPackage -> mainClass (server).
src -> defaultPackage -> clientSocketExample (client).

Thank you.

Read the API docs. When you do new ObjectInputStream(InputStream) it will attempt to read the "header" object created by on the other end when doing new ObjectOutputStream(OutputStream), and that read call blocks until it gets something. You need to create the ObjectOutputStream before creating the ObjectInputStream or design the thing in such a way (i.e. in a thread or something) that it doesn't matter that the creation of ObjectInputStream blocks until it gets the "header".

Works like a charm. Now I can go eat and get some sleep. :D

Thanks, masijade!

im doing remote desktop project. i need to connect two sockets (each from single client) through the hashtable in the separate application server, so that client1 can see the desktop of the client2. can u suggest me solution for this? how can i achieve this? so far i can connect two client directly in the LAN in client- server base through socket. now i have to implement application server to make the remote desktop working in the internet. your suggestions will be valuable. thank you.

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.