需要有关简单服务器/多客户端聊天应用程序的帮助

时间:2016-02-08 14:35:52

标签: java server client classcastexception

这是场景..我有2个客户端连接到服务器..我希望他们能够互相聊天。在收到几条消息后,我收到了这个错误。

    Exception in thread "Thread-0" java.lang.ClassCastException:
    java.io.ObjectStreamClass cannot be cast to message.Mesaj
    at server.ServerThread.readAndWrite(ServerThread.java:43)
    at server.ServerThread.run(ServerThread.java:61)
    java.io.StreamCorruptedException: invalid type code: 00
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at server.ServerThread.readAndWrite(ServerThread.java:43)
    at server.ServerThread.run(ServerThread.java:61)

这是客户:

package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import message.Mesaj;

public class Client {

public static int port=4321;
public static Socket socket;
public static ObjectOutputStream oo;
public static ObjectInputStream oi;
public static Scanner sc;

public Client() throws IOException{
    socket = new Socket ("localhost",4321);
    oi = new ObjectInputStream(socket.getInputStream());
    oo = new ObjectOutputStream(socket.getOutputStream());

}


public static void listen() throws ClassNotFoundException, IOException{
    while(true){
        Mesaj m = (Mesaj) oi.readObject();
        if(m!=null){
            System.out.println("mesajul este: " + m.getMesaj());
        }
    }
}

public static void write() throws IOException{
    sc= new Scanner(System.in); 
    while(true){
        String trimite= sc.nextLine();
        Mesaj m = new Mesaj();
        m.setMesaj(trimite);

        oo.writeObject(m);
        oo.flush();

    }

}


public static Thread t = new Thread(){
    public void run(){

        try {
            listen();
        } catch (ClassNotFoundException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
};

public static Thread t2 = new Thread(){
    public void run(){
        try {
            write();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};

public static void main(String[] args) throws IOException{
        new Client();
        t.start();
        t2.start();

  }

这是服务器:

package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server {
public int port;
public static Socket socket;
public static ServerSocket serverSocket;


public Server() throws IOException{
this.port=4321;
serverSocket = new ServerSocket(port);

}

public static void main (String [] args){
 try {
    new Server();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
System.out.println("Server functionabil..asteptam conexiune client");
while(true){
    try {   
        socket= serverSocket.accept();
        ServerThread st= new ServerThread(socket);
        st.start();
        System.out.println("Conexiune realizata -client conectat");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}
  }

  }
  }

..这是服务器线程:

   package server;

   import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   import java.net.Socket;
   import java.util.ArrayList;

   import message.Mesaj;

   public class ServerThread extends Thread {

   boolean running;
   public static ObjectOutputStream oo;
   public static ObjectInputStream oi;
   public static Mesaj m;
   public static Socket socket;




public ServerThread(Socket socket) throws ClassNotFoundException{
    try {       
        running=true;
        this.socket=socket;
        oo = new ObjectOutputStream(socket.getOutputStream());


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



public static void readAndWrite() throws ClassNotFoundException, IOException{

    oi = new ObjectInputStream(socket.getInputStream());

        while(true){    
        m= (Mesaj) oi.readObject();         
        if(m!=null){
        oo.writeObject(m);
        oo.flush();

        System.out.println(m.getMesaj());

    }
        }
}





public void run(){
    System.out.println("Server Thread contectat");
    try {
       readAndWrite();          
    } catch (IOException | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

编辑:
这是消息类:

package message;

import java.io.Serializable;

public class Mesaj implements Serializable {

private String mesaj;

public String getMesaj() {
    return mesaj;
}

public void setMesaj(String mesaj) {
    this.mesaj = mesaj;
}




}

1 个答案:

答案 0 :(得分:0)

您遇到并发问题。

两个serverthread都访问相同的静态输出流,这意味着损坏会发生变化,因为对象流不是为此而设计的。

ServerThread中,从所有变量和方法“readAndWrite”中删除static。

public ObjectOutputStream oo;
public ObjectInputStream oi;
public Mesaj m;
public Socket socket;
...
public void readAndWrite() throws ClassNotFoundException, IOException{

如果要从多个线程访问输出流,则应同步它们。