Java Server-Client Socket trouble (updated) -
i managed make example of want program be. problem can't make socket connection work (the way want work). , don't know problem here.
public class testchat extends frame { public static panel1 p1; public static panel2 p2; public static testchat tc; public testchat() { super(); setpreferredsize(new dimension(800, 600)); setlayout(new borderlayout()); addwindowlistener(new windowadapter() { public void windowclosing(windowevent we) { system.exit(0); } }); p1 = new panel1(); p2 = new panel2(); add(p1); } public static void main(string[] args) { // todo code application logic here tc = new testchat(); tc.pack(); tc.setvisible(true); ///* try { testchat.p2.run(); } catch (ioexception ioe) { system.out.println("io here"); } //*/ } public void change(int to) { if (to == 1) { tc.remove(p2); tc.add(p1); } if (to == 2) { tc.remove(p1); tc.add(p2); } tc.pack(); } } public class panel1 extends panel implements actionlistener{ public button button = new button("launch chat"); public panel1() { super(); setlayout(new borderlayout()); label label = new label("launcher panel here"); add(label); add(button, borderlayout.south); button.addactionlistener(this); } @override public void actionperformed(actionevent e) { if (e.getsource() == button) { testchat.tc.change(2); /* try { testchat.p2.run(); } catch (ioexception ioe) { system.out.println("io here"); } //*/ } } } public class panel2 extends panel implements actionlistener { private static final int login_max = 300; public static textarea ta = new textarea(); public static textfield tf = new textfield(); public static textarea logins = new textarea(); public static printwriter out = null; public static string[] loginlist = new string[login_max]; public static int logincount = 0; public panel temp = new panel(); public button startbutton = new button("start!"); ///* public string fromserver; public bufferedreader in = null; public bufferedreader stdin; public socket kksocket = null; //*/ public panel2() { setlayout(new borderlayout()); temp.setlayout(new borderlayout()); ta.seteditable(false); tf.addactionlistener(this); startbutton.addactionlistener(this); logins.seteditable(false); temp.add(ta, borderlayout.center); temp.add(tf, borderlayout.south); add(temp); add(logins, borderlayout.east); add(startbutton, borderlayout.south); } //private static void makelogins() { public static void makelogins() { string userarea = loginlist[0] + "\n"; (int = 1; < logincount; i++) { userarea = userarea + loginlist[i] + "\n"; } logins.settext(userarea); } public void run() throws ioexception { kksocket = null; bufferedreader in = null; try { kksocket = new socket("localhost", 4444); out = new printwriter(kksocket.getoutputstream(), true); in = new bufferedreader(new inputstreamreader(kksocket.getinputstream())); } catch (unknownhostexception e) { //system.err.println("can't host server."); system.out.println("can't host server."); system.exit(1); } catch (ioexception e) { system.err.println("couldn't i/o connection server."); system.exit(1); } bufferedreader stdin = new bufferedreader(new inputstreamreader(system.in)); string fromserver; while ((fromserver = in.readline()) != null) { validate(); if (fromserver.startswith("cmd_newuser_")) { loginlist[logincount++] = fromserver.substring(12); if (logincount > 1) { arrays.sort(loginlist, 1, logincount - 1); } makelogins(); } else if (fromserver.startswith("cmd_deleteuser_")) { string tmp = fromserver.substring(15); (int = 0; < logincount; i++) { if (loginlist[i].equals(tmp)) { loginlist[i] = "" + ((char) 255); break; } } arrays.sort(loginlist, 1, logincount); logincount--; makelogins(); } else { ta.append(fromserver + "\n"); } if (fromserver.equals("bye.")) { break; } } out.close(); in.close(); stdin.close(); } private void sendstr(printwriter out) { if (tf.gettext() != "") { out.println(tf.gettext()); tf.settext(""); } } @override public void actionperformed(actionevent e) { if (e.getsource() == tf) { sendstr(out); } else if (e.getsource() == startbutton) { system.out.println("i make actions in original proj"); } } }
when use way, program connects , works fine. want start connection panel1 class when button pressed (commented call). problem here , how can solve it?
p.s. here server code (just in case)
public class kkmultiserver extends frame { public static int usercount = 0; public static label users; public static kkmultiserverthread[] userlist=new kkmultiserverthread[100]; public static int writer=0; public static int curnum=1; public kkmultiserver() { super("server"); setlayout(new gridlayout(2, 1)); users = new label("users online: " + usercount); add(users); setlocation(200, 200); setresizable(false); setminimumsize(new dimension(300, 200)); addwindowlistener(new windowadapter() { public void windowclosing(windowevent we) { system.exit(0); } }); } public static void main(string[] args) throws ioexception { serversocket serversocket = null; boolean listening = true; kkmultiserver server = new kkmultiserver(); server.pack(); server.setvisible(true); try { serversocket = new serversocket(4444); } catch (ioexception e) { system.err.println("could not listen on port: 4444."); system.exit(-1); } while (listening) { userlist[writer]=new kkmultiserverthread(serversocket.accept()); system.out.println("client added!"); usercount++; users.settext("users online: " + usercount); userlist[writer++].start(); } serversocket.close(); } }
i'm not sure understand problem fully. if want on button press idea use listener , perform actions listener. example of mouselistener run code in method mousepressed() when button pressed.
this small private class implemenets mouselistener interface:
private class connectbuttonlistener implements mouselistener{ @override public void mousepressed(mouseevent arg0) { //connect serversocket conecttoserver(); } }
you need add listener button:
button connectbutton = new button(); connectbutton.addmouselistener(new connectbuttonlistener());
this should done create button. after done click on button run mousepressed method. connect code should go.
also when initialize frames , panels should done in swing thread , not main thread.
swingutilities.invokelater(new runnable(){ @override public void run() { //initialize gui here } });
Comments
Post a Comment