简体   繁体   中英

Missing Messages Java Socket

I have a what it supposed to be a P2P structured overlay, The server starts, listens for a registration message from the client and then is supposed to send a confirmation to the client.

The problem is I'm only getting 1 confirmation even though 10 are sent out. To simplify I'm sending a registration from the client in the StartNode() call and what I want is for the node to start listening for a response once the registration message is sent, but only one of confirmation messages is being received.

My registration if working fine. My server gets all 10 messages and sends out confirmations on the corresponding ports, but only 1 confirmation is recieved. I think the problem might be with how I'm listening for confirmations, though it's the same code I used in the server so I'm thinking it might be some kind of race condition, because it's not always the same confirmation that's recevied.

    //SINGLE CLIENT
    public static void main(String args[]) throws IOException { 
        for (int i = 0; i <= 10; i++) {
            Socket newS = new Socket(args[0],Integer.parseInt(args[1]));            
            Thread mNode = new MessagingNode(newS);
            mNode.start();

        }       
    } 

    public void run() {

        try {
            StartNode();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        boolean confirm = false;
        while (!confirm) {
            System.out.println("listening on port :" + portnumber );
            try {
                nsocket = cssocket.accept();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 
            Thread receiver = new MessageReceiver(cssocket,nsocket);
            receiver.start();
        }

}

//RECEIVER CLASS
public void run() {
    System.out.println("Recieving");
    synchronized (socket){
    int dataLength;
     try {
         din = new DataInputStream(socket.getInputStream());
         dataLength = din.readInt();             
         byte[] data = new byte[dataLength];
         din.readFully(data, 0, dataLength);
         Message inbound = new Message(data);                
         if (((int)inbound.messageT) == 2) {
             MessageONSR register = new MessageONSR(data);
             System.out.println(register.toString());
             MessagingNode addNode = new MessagingNode(register);
             if (registry.NodeExists(addNode)) {
                 System.out.println("Registration request un-successful. Messaging Node already exists in overlay. The number of messaging nodes currently constituting the overlay is ("+registry.regC+")");
             } else if (!CheckMismatch(register)) {
                 System.out.println("Registration request un-successful. The number of messaging nodes currently constituting the overlay is ("+registry.regC+")");
             } else {
                 registry.RegisterNodes(addNode);
                 System.out.println("Registration request from " + addNode.portnumber + " successful. The number of messaging nodes currently constituting the overlay is ("+registry.regC+")");
             }
         }  if (((int)inbound.messageT) == 3) {
             MessageRRS register = new MessageRRS(data);
             System.out.println(register.status);
             this.interrupt();

         }
         } catch (SocketException se) {
         System.out.println(se.getMessage());
         } catch (IOException ioe) {
         System.out.println(ioe.getMessage());
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     this.interrupt();
}
}

listening on port :510
listened on port :510
14Sent Registration
11Sent Registration
listening on port :512
listening on port :515
listened on port :515
listened on port :512
12Sent Registration
listening on port :513
listened on port :513
listening on port :510
***listened on port :510
Recieving
30***
10Sent Registration
listening on port :511
listened on port :511
19Sent Registration
listening on port :520
listened on port :520
13Sent Registration
listening on port :514
listened on port :514
18Sent Registration
listening on port :519
listened on port :519
17Sent Registration
listening on port :518
listened on port :518
15Sent Registration
listening on port :516
listened on port :516
16Sent Registration
listening on port :517
listened on port :517

我发现了问题,我只收到一条消息的原因是因为我的ServerSocket被声明为静态,这意味着,当我的侦听器启动时,它被覆盖到实例化的最后一个Node。

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