简体   繁体   中英

how do I communicate between two servers using sockets java

I'm trying to connect two servers here

following is my code which will be run on both servers

public class Node {

/**
 * @param args the 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 the system ready");
    System.out.println(nodeSock.getLocalSocketAddress()+ ";"+nodeSock.getInetAddress()+";"+nodeSock.getLocalPort());
    for(int i = 0 ; i < SysInfo.maxNodes ; i++ ){
        if(i == nodeID) 
            continue;
       // this.connectToNode(SysInfo.nodePorts[i], i);
        try {
            System.out.println("waiting for connection to node #"+i+" to be 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 to other node#"+i);

        } catch (IOException ex) {
            /* ignore */
        }
    }
}

I'm trying to check if both nodes are connected or notand only then proceed to next stage(ie, I'll only start my actual communications if both servers are up and running.)

but I didn't get proper result here. the output which I'm getting are as following.....

output at node 0...

current node #0 :

Making the system ready

/127.0.0.1:20000;/127.0.0.1;20000

waiting for connection to node #1 to be established


and output at node 1.....

current node #1 : 

Making the system ready

/127.0.0.1:20001;/127.0.0.1;20001

waiting for connection to node #0 to be established

node #1 connected to other node#0


on one node it is showing that it is connected and on the other it has not shown anything. please help me where I'm going wrong here.

You try to make a connection to node 1 before node 1 has started. The connection fails and throws an exception which you ignore. If you changed "/* ignore */" to "ex.printStackTrace();", you would find out that this is happening.

Never ignore an exception if you don't know what to do with it, because things like this happen every time.

You should not need this loop:

while(!(s.isConnected()));

The socket makes a connection before "new Socket" finishes, so it is meaningless to check for connection here. Worse, if it connects and then immediately disconnects, you will get stuck in an infinite loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM