java - Need help on a simple server/multiple client chat application -
this scenario .. have 2 clients connected server.. want them able chat eachother. after couple of messages error.
exception in thread "thread-0" java.lang.classcastexception: java.io.objectstreamclass cannot cast message.mesaj @ server.serverthread.readandwrite(serverthread.java:43) @ server.serverthread.run(serverthread.java:61) java.io.streamcorruptedexception: invalid type code: 00 @ java.io.objectinputstream.readobject0(unknown source) @ java.io.objectinputstream.readobject(unknown source) @ server.serverthread.readandwrite(serverthread.java:43) @ server.serverthread.run(serverthread.java:61)
this client:
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(); }
this server:
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(); } } } }
..and server thread:
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(); } }
}
edit:
message class:
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; } }
you having concurrency issues.
both serverthreads access same static outputstream, meaning there change corruption object streams aren't designed this.
in serverthread
, remove static variables , method "readandwrite".
public objectoutputstream oo; public objectinputstream oi; public mesaj m; public socket socket; ... public void readandwrite() throws classnotfoundexception, ioexception{
if want access output streams multiple threads, should synchronize on them.
Comments
Post a Comment