multithreading - One Client just listening after a few messages: Client Server Chat with ArrayList sockets outputstreamwriter -
i have got problem broadcast method. did chat server , clients. each client runs 1 thread , has own socket output stream @ beginning works fine after few messages 1 client can send while other receiving messages..?! don't know why idea? @ beginning both can send , receive.. can't close outputstream because nullpointerexception maybe mistake? thankful every help!
server:
public static void broadcastjson(jsonobject jsonobject, arraylist<socket> socketlist)throws ioexception{ outputstreamwriter out = null; for(int i=0; i<socketlist.size(); i++){ out=new outputstreamwriter(socketlist.get(i).getoutputstream(), "utf-8"); out.write(jsonobject.tostring() + "\n"); out.flush(); } }
client:
public jsonobject receivejson()throws ioexception{ bufferedreader in = new bufferedreader(new inputstreamreader(client.socket.getinputstream(), "utf-8")); string obje = in.readline(); jsonobject jsonobject = new jsonobject(obje); return jsonobject; }
you're losing data read-ahead bufferedreader.
don't create new streams/readers/writers per message. use same ones life of socket.
Comments
Post a Comment