Java socket connection problems -
i'm trying learn network programming java , having trouble getting simple chat program connect between server , host. here i'm experiencing:
when try connect client program server i'm getting null pointer exception , won't connect.
while debugging found line
connection.equals(server.accept()); in waitforcommuinication() method in server class not executed.
this occurs when line
client = new socket(inetaddress.getbyname(chatserver), 50499); is executed in method connecttoserver() in client class.
i running code using localhost ip add
server class:
public class server extends jframe { private jtextfield enterfield; private jtextarea displayarea; private objectoutputstream output; private objectinputstream input; private serversocket server; private socket connection; private int counter = 1; public server() { super ("server"); enterfield = new jtextfield(); enterfield.seteditable(true); enterfield.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent event) { senddata(event.getactioncommand()); enterfield.settext(""); } }); add(enterfield, borderlayout.north); displayarea = new jtextarea(); add (new jscrollpane(displayarea)); setsize(300,150); setvisible(true); } public void runserver() { try { server = new serversocket(50499, 100); //displaymessage("\n listening on port: " + server.getlocalport() + "\n"); while (true) { try { waitforcommunication(); getstreams(); processconnection(); } catch (eofexception eofexception) { displaymessage("\n server terminated connection "); } { closeconnection(); ++counter; } } } catch (ioexception ioexception) { ioexception.printstacktrace(); } } private void closeconnection() { displaymessage("\nterminating connection\n"); settextfieldeditable(false); try { output.close(); input.close(); connection.close(); } catch (ioexception ioexception) { ioexception.printstacktrace(); } } private void displaymessage(final string string) { swingutilities.invokelater(new runnable(){ @override public void run() { displayarea.append(string); } }); } private void processconnection() throws ioexception { string message = "connection sucessful"; senddata(message); settextfieldeditable(true); { try { message = (string) input.readobject(); displaymessage("\n" + message); } catch (classnotfoundexception classnotfoundexception) { displaymessage("\nnunknown object type recieved"); } } while (!message.equals("client>>> terminate")); } private void settextfieldeditable(final boolean editable) { swingutilities.invokelater(new runnable(){ @override public void run() { enterfield.seteditable(editable); } }); } private void getstreams() throws ioexception { output = new objectoutputstream(connection.getoutputstream()); output.flush(); input = new objectinputstream(connection.getinputstream()); displaymessage("\ngot i/o stream \n"); } private void waitforcommunication() throws ioexception { displaymessage("waiting cennection \n"); connection.equals(server.accept()); displaymessage("connection" + counter + " received from: " + connection.getinetaddress().gethostname()); } private void senddata(string message) { try { output.writeobject("server>>> " + message); output.flush(); displaymessage("\nserver>>> " + message); } catch (ioexception ioexception){ displayarea.append("\nerror writing object"); } } } client class:
public class client extends jframe { private jtextfield enterfield; private jtextarea displayarea; private objectoutputstream output; private objectinputstream input; private string message = ""; private string chatserver; private socket client; public client(string host){ super ("host"); chatserver = host; enterfield = new jtextfield(); enterfield.seteditable(false); enterfield.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent event) { senddata(event.getactioncommand()); enterfield.settext(""); } }); add(enterfield, borderlayout.north); displayarea = new jtextarea(); add(new jscrollpane(displayarea)); setsize(300,150); setvisible(true); } public void runclient(){ try{ connecttoserver(); getstreams(); processconnection(); } catch (eofexception eofexception){ displaymessage("\nclient terminated connection"); } catch (ioexception ioexception){ ioexception.printstacktrace(); } { closeconnection(); } } private void closeconnection() { displaymessage("\nclosing connection"); settextfieldeditable(false); try{ output.close(); input.close(); client.close(); } catch (ioexception ioexception){ ioexception.printstacktrace(); } } private void displaymessage(final string messagetodisplay) { swingutilities.invokelater( new runnable() { @override public void run() { displayarea.append(messagetodisplay); } }); } private void processconnection() throws ioexception { settextfieldeditable(true); { try{ message = (string) input.readobject(); } catch (classnotfoundexception classnotfoundexception){ displaymessage("\nunknown object type recieved"); } } while (!message.equals("server>>> terminate")); } private void settextfieldeditable(final boolean b) { swingutilities.invokelater(new runnable(){ @override public void run() { enterfield.seteditable(b); } }); } private void getstreams() throws ioexception { output = new objectoutputstream(client.getoutputstream()); output.flush(); input = new objectinputstream(client.getinputstream()); displaymessage("\ngot i/o streams!\n"); } private void connecttoserver() throws ioexception { displaymessage("attempting connection\n"); client = new socket(inetaddress.getbyname(chatserver), 50499); displaymessage("connected to: " + client.getinetaddress().gethostname()); } protected void senddata(string actioncommand) { try{ output.writeobject("client>>> " + actioncommand); output.flush(); displaymessage("\nclient>>> " + actioncommand); } catch (ioexception ioexception){ displayarea.append("\nerror sending message"); } } }
in server class, waitforcommunication method have npe couse of connection null. connection.equals(server.accept());
i'm sure need connection = server.accept();
Comments
Post a Comment