java - unable to send messages on chat program -
i writing chat program in java. can't seem able send messages. clientconnectionhandler
handles instance of each client
import java.io.*; import java.net.*; import java.text.*; import java.util.*; public class clientconnectionhandler extends thread { private socket socket; string username; private uuid id; //= uuid.randomuuid(); bufferedreader reader; printwriter writer; private final hashmap<uuid, clientconnectionhandler> clients = new hashmap<>(); volatile boolean messageloop = true; servergui servergui; public clientconnectionhandler(socket socket){ this.socket = socket; try{ this.socket.setsotimeout(1000); } catch (socketexception e) { system.out.println(e); } try { reader = new bufferedreader(new inputstreamreader(socket.getinputstream())); writer = new printwriter(new outputstreamwriter(socket.getoutputstream())); } catch (exception e){ e.printstacktrace(); } } public void run(){ if (socket != null && reader != null) { addtoclients(); try { string messageinput; while (messageloop){ try{ messageinput = reader.readline(); sendmessage(messageinput); } catch(sockettimeoutexception ste){ //thread.yield(); } } } catch (ioexception e){ e.printstacktrace(); } { removefromclients(); try { reader.close(); writer.close(); } catch (ioexception e) { e.printstacktrace(); } } } else { system.out.println("socket connection , bufferedreader closed"); } } private void sendmessage(string msg){ (clientconnectionhandler cchandler : clients.values()){ try { //if (!clientconn.id.equals(this.id)) { cchandler.writer.write(msg+"\n"); cchandler.writer.flush(); //} } catch (exception e){ system.err.println("unable write client"); clients.remove(cchandler.id); } } } }
here's client
class
public class client { private socket clientsocket; private string username; private static int port = 7777; bufferedreader inreader; printwriter outwriter; private simpledateformat sdf; clientgui clientgui; servergui servergui; private login login; public client(string username, int port){ this.username = username; this.port = port; sdf = new simpledateformat("dd.mm.yyyy 'at' hh:mm:ss a"); } /** * connects client server */ public void connectclient(){ try{ clientsocket = new socket("127.0.0.1", port); thread thread = new thread(new clientconnectionhandler(clientsocket)); thread.start(); string connected = sdf.format(new date())+"\n"+username+" connected \n"; system.out.println(connected); } catch(exception e){ system.out.println(sdf.format(new date())+"\n"+username+" did not connect server "+e+"\n"); } } /** * disconnects client server */ void disconnectclient(){ try{ clientsocket.close(); system.out.println(username+" disconnected \n"); } catch(exception e){ system.out.println(username+" failed disconnect \n"); } } //send message server public void sendmessage() throws ioexception{ try{ inreader = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); outwriter = new printwriter(new outputstreamwriter(clientsocket.getoutputstream())); } catch(ioexception ioe){ } //infinite loop check messages try{ string response; string inputmsg = inreader.readline(); outwriter.println(inputmsg); while((response = inreader.readline()) != null){ system.out.println(response); outwriter.println(inputmsg); } inreader.close(); outwriter.close(); //clientgui.appendmessage(inputmsg); } catch(ioexception ioe){ } } void appendtocg(string str){ clientgui.appendmessage(str); } public static void main(string[] args) { client client = new client("127.0.0.1",port); } }
edit
startserverconnection()
method in server
class:
public void startserverconnection() { try { serversocket = new serversocket(portnumber); while(connection){ system.out.println("waiting client..."); socket clientsocket = serversocket.accept(); system.out.println("server connection established on port: "+clientsocket.getlocalport()); clientconnectionhandler clientconnection = new clientconnectionhandler(clientsocket); thread thread = new thread(clientconnection); thread.start(); } } catch (exception e) { // system.out.println(sdf.format(new // date())+": server didn't connect"); //append eventslog e.printstacktrace(); return; } }
i'm pretty sure i've created readers , writers necessary, on both clientconnectionhandler
(for each user send message) , client
read messages. missing out on something?
probably not full resolution, but:
the clientconnectionhandler extends thread. should extend runnable since create thread in startserverconnection().
it looks attempting reuse clientconnectionhandler between
server
,client
. approach may not want. more want differentrunnable
client. haven't thought through of implications, might able makeclient
class implementrunnable
, , changeclient
public void connectclient(){ try{ clientsocket = new socket("127.0.0.1", port); // set timeout here, or block io! alternatively, // use nio stuff thread thread = new thread(this); thread.start(); string connected = sdf.format(new date())+"\n"+username+" connected \n"; system.out.println(connected); } catch(exception e){ system.out.println(sdf.format(new date())+"\n"+username+" did not connect server "+e+"\n"); } } public void run() { try { clientsocket.setsotimeout(1000); //loop read messages sent *this* client while (true) { try { string inputmsg = reader.readline(); // print stdout; might want update gui system.out.println(inputmsg); } catch (sockettimeoutexception noop) { } } //end of while loop } catch (exception e) { // note error may not problem since // disconnect client close socket, // cause error @ future point. system.err.println(e); } } }
in
client.sendmessage()
method:
a. message want send? must gathering input somewhere
b. not attach , close streams in method.so presumably in
client
class need like:public void sendmessage() throws ioexception{ try{ // message send somewhere string msgtosend = "foo"; // dumb text; replace actual writer.write(msgtosend); writer.write("\n"); // needed writer.flush(); // flush buffer } }
Comments
Post a Comment