Hey, Im trying to write a chat program which should be able to run multiple clients using threads. But I seem to get this "java.io.StreamCorruptedException: invalid type code: 00" error the whole time.

My server code:

import java.io.*;
import java.net.*;
public class Server{
	ServerSocket serverSocket;
	Socket connection = null;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;
	Server(){}
	void run() throws IOException{
            //1. creating a server socket
            serverSocket = new ServerSocket(2004, 10);


            //2. Wait for connection
            while (true){
                try{
                    System.out.println("Waiting for connection");
                    connection = serverSocket.accept();
                    System.out.println("Connection received from " + connection.getInetAddress().getHostName());
                }
                catch(IOException ioException){
                    ioException.printStackTrace();
		}
                myThread thread = new myThread();
                thread.start();
            }
        }
	
	void sendMessage(String msg){
            try{
                out.writeObject(msg);
                out.flush();
                System.out.println("server>" + msg);
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
	}

	public static void main(String args[]) throws IOException{
            Server myserver = new Server();
                myserver.run();
	}

    class myThread extends Thread{
        @Override
        public void run(){
                try {
                    //3. get Input and Output streams
                    out = new ObjectOutputStream(connection.getOutputStream());
                    out.flush();
                    in = new ObjectInputStream(connection.getInputStream());
                    sendMessage("Connection successful");

                    //4. The two parts communicate via the input and output streams
                    do {
                        try {
                            message = (String) in.readObject();
                            System.out.println("client>" + message);
                            sendMessage(("client>" + message));
                            if (message.equals("bye")){
                                sendMessage("bye");
                            }
                        }catch (ClassNotFoundException classnot){
                            System.err.println("Data received in unknown format");
                        }
                    } while (!message.equals("bye"));
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
                try {
                    in.close();
                    out.close();
                    serverSocket.close();
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
        }
    }
}

My client code:

import java.io.*;
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Client{
    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    Client(){}
    void run(){
	try{
            //1. creating a socket to connect to the server
            requestSocket = new Socket("localhost", 2004);
            System.out.println("Connected to localhost in port 2004");

            //2. get Input and Output streams
            out = new ObjectOutputStream(requestSocket.getOutputStream());
            out.flush();
            in = new ObjectInputStream(requestSocket.getInputStream());

            //3: Communicating with the server
            do{
                try{
                    message = (String)in.readObject();
                    System.out.println("server>" + message);
                }
                catch(ClassNotFoundException classNot){
                    System.err.println("data received in unknown format");
                }
                while (true){
                    InputStreamReader isr = new InputStreamReader(System.in);
                    BufferedReader br = new BufferedReader(isr);
                    message = br.readLine();
                    sendMessage(message);
                }
            }while(!message.equals("bye"));
        }
        catch(UnknownHostException unknownHost){
            System.err.println("unknown host!");
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
        finally{
            //4: Closing connection
            try{
                    in.close();
                    out.close();
                    requestSocket.close();
            }
            catch(IOException ioException){
                    ioException.printStackTrace();
            }
        }
    }
	void sendMessage(String msg){
            try{
                out.writeObject(msg);
                out.flush();
                System.out.println("client>" + msg);
            }
            catch(IOException ioException){
                    ioException.printStackTrace();
            }
	}
	public static void main(String args[]){
            Client client = new Client();
            client.run();
	}
}

The error:

java.io.StreamCorruptedException: invalid type code: 00
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at Server$myThread.run(Server.java:59)
java.io.StreamCorruptedException: invalid type code: 00
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at Server$myThread.run(Server.java:59)
java.net.SocketException: socket closed
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390)
        at java.net.ServerSocket.implAccept(ServerSocket.java:453)
        at java.net.ServerSocket.accept(ServerSocket.java:421)
        at Server.run(Server.java:19)
        at Server.main(Server.java:43)
java.net.SocketException: Socket is closed

The error then repeats the last error endlessly.

This happens when I
1. Run the server
2. Run the 1st client
3. Run the second client
4. Send a few messages (usually 2 or 3)

Thanks guys

Recommended Answers

All 3 Replies

The only thing that looks odd to me is Client line 21 out.flush(); where you flush before the first write. That's probably harmless, but definitely redundant, so maybe try taking it out?

Another thought
When you have finished communicating with a client you close the ServerSocket (line 76) when you should be closing the connection Socket, so the accept method throws a socket closed exception and all subsequent attempts to use the connection fail.
If I'm right you should be able to make one successful client connection before it all goes Pete Tong.

did you ever solve this problem coz i have the same problem

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.