i've been trying send private message client(from sendprivate method) still don't work. can have suggestion here? (my sendtoallmethod works). here code far. in advance guys :)
import java.net.*; import java.util.arraylist; import java.io.*; public class testserver extends thread { private socket clientsocket; protected static string inputline; public static arraylist < socket > connectionarray = new arraylist < socket > (); public static arraylist < string > currentusers = new arraylist < string > (); string sender; string receiver; final static int port = 256; public static void main(string[] args) throws ioexception { serversocket serversocket = null; try { serversocket = new serversocket(port); system.out.println("connection socket created"); try { while (true) { system.out.println("waiting connection"); socket sock = serversocket.accept(); connectionarray.add(sock); system.out.println("client connected from: " + sock.getlocaladdress().gethostname()); new testserver(sock); } } catch (ioexception e) { system.err.println("accept failed."); system.exit(1); } } catch (ioexception e) { system.err.println("could not listen on port: " + port); system.exit(1); } { try { serversocket.close(); } catch (ioexception e) { system.err.println("could not close port: 10008."); system.exit(1); } } } private testserver(socket insock) { clientsocket = insock; start(); } public void run() { system.out.println("new communication thread started"); //system.out.println ("client connected from: " + sock.getlocaladdress().gethostname()); try { printwriter out = new printwriter(clientsocket.getoutputstream(), true); bufferedreader in = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); while (true) { (int = 1; <= testserver.connectionarray.size(); i++) { socket temp_sock = (socket) testserver.connectionarray.get(i - 1); if (clientsocket == temp_sock) try { sender = testserver.currentusers.get(i - 1); } catch (exception e) {} } inputline = in .readline(); system.out.println("server: " + inputline); getcommand(inputline); if (inputline.equals("exit")) break; } out.close(); in .close(); clientsocket.close(); } catch (ioexception e) { system.err.println("problem communication server"); system.exit(1); } } public void addusername(string inputname) throws ioexception { currentusers.add(inputname); (int = 1; <= testserver.connectionarray.size(); i++) { socket temp_sock = (socket) testserver.connectionarray.get(i - 1); printwriter out = new printwriter(temp_sock.getoutputstream()); out.println("current users: " + currentusers); out.flush(); } } public void sendtoall(string input) throws ioexception { (int = 1; <= testserver.connectionarray.size(); i++) { socket temp_sock = (socket) testserver.connectionarray.get(i - 1); if (clientsocket != temp_sock) { printwriter temp_out = new printwriter(temp_sock.getoutputstream(), true); temp_out.println(sender + ": " + input); temp_out.flush(); system.out.println("sending to: " + temp_sock.getlocaladdress().gethostname()); } } } public void sendprivate(string inputname, string inputmsg) throws ioexception { socket temp_sock = null; (int = 1; <= testserver.currentusers.size(); a++) { string receiver = testserver.currentusers.get(a - 1); if (inputname == (receiver)); { system.out.println("found username!!!"); temp_sock = (socket) testserver.connectionarray.get(a - 1); } } printwriter temp_out = new printwriter(temp_sock.getoutputstream()); temp_out.println("[pm] " + sender + " :" + inputmsg); temp_out.flush(); } public void getcommand(string inputcmd) throws ioexception { string str = inputcmd; string[] splitstr = str.trim().split("\\s+"); if (splitstr[0].equalsignorecase("reg")) { system.out.println("registering"); stringbuffer result = new stringbuffer(); (int y = 1; y < splitstr.length; y++) { result.append(splitstr[y]); result.append(" "); } string joinedstr = result.tostring(); system.out.println("registering name ---> " + joinedstr); addusername(joinedstr); } else if (splitstr[0].equalsignorecase("msg")) // send { system.out.println("sending all"); stringbuffer result = new stringbuffer(); (int y = 1; y < splitstr.length; y++) { result.append(splitstr[y]); result.append(" "); } string joinedstr = result.tostring(); system.out.println("sending---> " + joinedstr); sendtoall(joinedstr); } else if (splitstr[0].equalsignorecase("pmsg")) // send private { system.out.println("sending private message"); stringbuffer result = new stringbuffer(); (int y = 2; y < splitstr.length; y++) { result.append(splitstr[y]); result.append(" "); } string joinedstr = result.tostring(); system.out.println("sending---> " + joinedstr); string pvtmsg = splitstr[1]; sendprivate(pvtmsg, joinedstr); } else if (splitstr[0].equalsignorecase("exit")) { socket temp_sock = clientsocket; printwriter temp_out = new printwriter(temp_sock.getoutputstream(), true); temp_out.println("bye bye"); temp_out.flush(); } else { socket temp_sock = clientsocket; printwriter out = new printwriter(temp_sock.getoutputstream()); out.println("invalid input"); out.flush(); } } }
inputname == (receiver)
should :
inputname.equals(reciever)
Comments
Post a Comment