java - How should I run a Server/Client Code? -


i started writing first server/client code , simple chat program , dont know how should run code !

there .class file named server side , , .class file named clientside , supposed in different projects ? how should run both have connection ? ,following part of codes

public void runserver()     {         try {             server = new serversocket();             while(true)             {                 try                 {                     connection = server.accept();                     try{                         output = new objectoutputstream(connection.getoutputstream()) ;                         output.flush();                          input = new objectinputstream(connection.getinputstream()) ;                         senddata(message) ;                                                 {                             try{                                 message = (string) input.readobject() ;                                 system.out.println(message);                             }catch(exception e)                             {                                 e.printstacktrace() ;                             }                         }while(!message.equals("end"));                     }catch(eofexception e)                     {                         e.printstacktrace() ;                     }                    }catch(ioexception e)                 {                     e.printstacktrace() ;                 }                            {                     try{                     output.close();                     input.close();                     connection.close() ;                     }catch(exception e)                     {                         e.printstacktrace();                     }                     }             }         }catch(exception e )         {             e.printstacktrace();          }     } 

and here's clientside :

public void runclient()     {             try{             connect() ;             }catch(exception e)             {                 e.printstacktrace();             }             try{                 output = new objectoutputstream(client.getoutputstream()) ;                 output.flush() ;                 input = new objectinputstream(client.getinputstream()) ;             }catch(ioexception e)             {                 e.printstacktrace() ;             }                         {                 try{                     message = (string) input.readobject() ;                     system.out.println(message);                 }catch(exception e)                 {e.printstacktrace();}             }while(!message.equals("end")) ;      }     public void connect() throws unknownhostexception, ioexception     {         client = new socket(inetaddress.getbyname(chatserver),12345) ;     } 

first run server

java server

then run client:

java client

but not see server bound port 12345. client try connect server on port 12345 because of statement

client = new socket(inetaddress.getbyname(chatserver),12345) ; 

if ports not match, connection not established.

to bind server port 12345 try this:

server = new serversocket(12345); 

instead of default constructor.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -