Java socket - simple program doesn't work -


i've spent lot of time find out problem no success. server launching correctly, when launch client "unexpected error" exception. i've changed ports no effects. should make working?

/* server.java */ import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket;  public class server { private static final int port = 50000; static boolean flaga = true;  private static serversocket serversocket; private static socket clientsocket;  public static void main(string[] args) throws ioexception {     serversocket = null;     try     {         serversocket = new serversocket(port);     }     catch(ioexception e)     {         system.err.println("could not listen on port: "+port);         system.exit(1);     }      system.out.print("wating connection...");      thread t = new thread(new runnable()     {         public void run()         {             try             {                 while(flaga)                 {                     system.out.print(".");                     thread.sleep(1000);                 }             }             catch(interruptedexception ie)             {                 //             }              system.out.println("\nclient connected on port "+port);         }     });     t.start();      clientsocket = null;     try     {         clientsocket = serversocket.accept();         flaga = false;     }     catch(ioexception e)     {         system.err.println("accept failed.");         t.interrupt();         system.exit(1);     }      final printwriter out = new printwriter(clientsocket.getoutputstream(),true);     final bufferedreader in = new bufferedreader(new inputstreamreader(clientsocket.getinputstream()));      t = new thread(new runnable()     {         public void run()         {             try             {                 thread.sleep(5000);                  while(true)                 {                     out.println("ping");                     system.out.println(system.currenttimemillis()+" ping sent");                      string input = in.readline();                      if(input.equals("pong"))                     {                         system.out.println(system.currenttimemillis()+" pong received");                     }                     else                     {                         system.out.println(system.currenttimemillis()+" wrong answer");                          out.close();                         in.close();                         clientsocket.close();                         serversocket.close();                         break;                     }                       thread.sleep(5000);                 }             }             catch(exception e)             {                 system.err.println(system.currenttimemillis()+" unexpected error");             }         }     });     t.start(); } } 

and client class

/* client.java */ import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket;  public class client { private static final int port = 50000; private static final string host = "localhost";  public static void main(string[] args) throws ioexception {     socket socket = null;      try     {         socket = new socket(host, port);     }     catch(exception e)     {         system.err.println("could not connect "+host+":"+port);         system.exit(1);     }      final printwriter out = new printwriter(socket.getoutputstream(),true);     final bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream()));      thread t = new thread(new runnable()     {         public void run()         {             long start = system.currenttimemillis();              while (true)             {                 try                 {                     string input = in.readline();                      if (input != null)                     {                         system.out.println(system.currenttimemillis() + " server: " + input);                     }                      if (input.equals("ping"))                     {                         if(system.currenttimemillis()-start>30000)                         {                             out.println("pon g");                             system.out.println(system.currenttimemillis() + " client: pon g");                             break;                         }                          out.println("pong");                         system.out.println(system.currenttimemillis() + " client: pong");                     }                 }                 catch (ioexception ioe)                 {                     //                 }             }         }     });     t.start();      out.close();     in.close();     socket.close(); } } 

here output on running

wating connection............ client connected on port 50000 1368986914928 ping sent java.lang.nullpointerexception     @ server$2.run(server.java:84)     @ java.lang.thread.run(thread.java:722) 

it shows out object null. instead of input.equals("pong") use input != null && input.equals("pong") in line 84 of server.java. guess have received pong received in later stages when listening nothing have got npe.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -