java - Client Socket cannot find Server Socket. Throws UnknownHostException despite having the same port number -


i'm following online tutorial used clients&servers in java before start working them.

all trying right start server , connect client. it's couple lines of code client , server , seemed straightforward me, i'm not sure problem is.

i run server program, run client program, client socket cannot find server socket reason though have same port number.

here code.

server:

public class server {        private serversocket serversocket;      private int portnumber;       public server(int portnumber){         this.portnumber=portnumber;     }      public void run() throws ioexception {         serversocket = new serversocket(portnumber);         system.out.println("server @ port "+portnumber);         system.out.println("waiting client.");         socket clientsocket = serversocket.accept();         system.out.println("connected.");     }       public static void main(string[] args) {              int portnumber = 9999;              try {                 server serversocket = new server(portnumber);                  serversocket.run();              }              catch(ioexception e){                  e.printstacktrace();              }          }     } 

and client program:

public class client {     private int portnumber;     private string host;     socket clientsocket;       public client(int portnumber,string host){         this.portnumber=portnumber;         this.host=host;     }      public void connect() throws ioexception, unknownhostexception {         system.out.println("connecting port "+portnumber);         clientsocket = new socket(host,portnumber);         system.out.println("connected");     } public static void main(string arg[]){             client clientsocket = new client(9999,"host");             try {                 clientsocket.connect();             }             catch(unknownhostexception e){                 system.err.println("can't connect host");             }             catch (ioexception e) {                 system.err.println("can't connet. "+e.getmessage());              }          }     } 

unknownhostexception thrown when run client program.

i thought followed tutorial pretty closely, don't know problem code is.

i guess had connect() method because that's exception thrown, can't sure.

any ideas?

in code

client clientsocket = new client(9999,"host"); 

can change , run

client clientsocket = new client(9999,"localhost"); 

your code looking host named "host". either give ip address of host. if want use variable host defined above use

client clientsocket = new client(9999,host); 

but not see place getting initialized


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -