java - Server is not connecting -
i have void
method called startserverconnection()
connects server port, in case 7777. method called inside action listener button in class, clientgui
.
i'm pretty sure code correct, reason output "waiting connection..."
public void startserverconnection(){ try{ serversocket = new serversocket(portnumber); while(true){ system.out.println("waiting connection..."); socket clientsocket = serversocket.accept(); system.out.println("connection established on port: "+clientsocket.getlocalport()); clientconnection clientconnection = new clientconnection(clientsocket); thread thread = new thread(clientconnection); thread.start(); } } catch(exception e){ e.printstacktrace(); return; } }
edit client class, connectclient method:
public void connectclient(string user){ try{ host = clientsocket.getinetaddress(); clientsocket = new socket(host,port); new clienthandler(clientsocket).run(); string accepted = "connection host "+host+" accepted on port: "+clientsocket.getport(); } catch(exception e){ //sendmessage("connection error: "+e); //servergui.appendeventslog("client "+new clientgui(username, port)+" failed connect"); } }
any ideas on what's wrong?
update:
public void connectclient(string user){ try{ clientsocket = new socket(host,port); // use printwriter send data out server // use bufferedreader receive data server } catch(exception e){ //sendmessage("connection error: "+e); //servergui.appendeventslog("client "+new clientgui(username, port)+" failed connect"); } }
host ip address or hostname, if server/client running on same machine, can either use "127.0.0.1" or "localhost"; port value of int
, in case 7777
original:
accept()
blocking function. code afterwards not go thorough until connection established.
you have build client side , request connection, once server , client connected, see "connection established on port.."
public socket accept() throws ioexception
listens connection made socket , accepts it. method blocks until connection made.
Comments
Post a Comment