how do I communicate between two servers using sockets java -


i'm trying connect 2 servers here

following code run on both servers

public class node {  /**  * @param args command line arguments  */  private int nodeid; private int port; private serversocket nodesock; private int maxnodes = sysinfo.maxnodes; private socket othersock; private printwriter ostream; private bufferedreader in; private int connectednodeid; private hashmap<integer, socket> socks; private hashmap<socket, printwriter> ostreams; private boolean isprimary;   public node(int nodeid, boolean isprimary){     this.nodeid = nodeid;     this.port   = sysinfo.nodeports[nodeid];     this.isprimary = isprimary;     socks = new hashmap<integer, socket>();     ostreams = new hashmap<socket, printwriter>();     system.out.println("current node #"+this.nodeid+" : ");     try{         //nodesock = new serversocket(sysinfo.nodeports[nodeid]);         nodesock = new serversocket(this.port,0,inetaddress.getbyname("127.0.0.1")); }catch(ioexception e){         e.printstacktrace(); }      makesystemready(); } private void makesystemready()  {     system.out.println("making system ready");     system.out.println(nodesock.getlocalsocketaddress()+ ";"+nodesock.getinetaddress()+";"+nodesock.getlocalport());     for(int = 0 ; < sysinfo.maxnodes ; i++ ){         if(i == nodeid)              continue;        // this.connecttonode(sysinfo.nodeports[i], i);         try {             system.out.println("waiting connection node #"+i+" established");             socket s = new socket(inetaddress.getbyname("127.0.0.1"), sysinfo.nodeports[i]);             //socks.put(port, s);             while(!(s.isconnected()));             system.out.println("node #"+nodeid+" connected other node#"+i);          } catch (ioexception ex) {             /* ignore */         }     } } 

i'm trying check if both nodes connected or notand proceed next stage(i.e., i'll start actual communications if both servers , running.)

but didn't proper result here. output i'm getting following.....

output @ node 0...

current node #0 : 

making system ready

/127.0.0.1:20000;/127.0.0.1;20000

waiting connection node #1 established


and output @ node 1.....

current node #1 :  

making system ready

/127.0.0.1:20001;/127.0.0.1;20001

waiting connection node #0 established

node #1 connected other node#0


on 1 node showing connected , on other has not shown anything. please me i'm going wrong here.

you try make connection node 1 before node 1 has started. connection fails , throws exception ignore. if changed "/* ignore */" "ex.printstacktrace();", find out happening.

never ignore exception if don't know it, because things happen every time.

you should not need loop:

while(!(s.isconnected()));

the socket makes connection before "new socket" finishes, meaningless check connection here. worse, if connects , disconnects, stuck in infinite loop.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -