Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java套接字中缺少消息_Java - Fatal编程技术网

Java套接字中缺少消息

Java套接字中缺少消息,java,Java,我有一个应该是P2P结构的覆盖,服务器启动,监听来自客户端的注册消息,然后应该向客户端发送确认 问题是我只得到1次确认,即使有10次发出。为了简化,我在StartNode()调用中从客户端发送注册,我希望节点在发送注册消息后开始侦听响应,但只接收到一条确认消息 我的注册工作是否正常。我的服务器收到所有10条消息,并在相应的端口上发送确认,但只收到1条确认。我认为问题可能在于我如何监听确认,尽管这是我在服务器中使用的相同代码,所以我认为这可能是某种竞争条件,因为接收到的确认并不总是相同的

我有一个应该是P2P结构的覆盖,服务器启动,监听来自客户端的注册消息,然后应该向客户端发送确认

问题是我只得到1次确认,即使有10次发出。为了简化,我在StartNode()调用中从客户端发送注册,我希望节点在发送注册消息后开始侦听响应,但只接收到一条确认消息

我的注册工作是否正常。我的服务器收到所有10条消息,并在相应的端口上发送确认,但只收到1条确认。我认为问题可能在于我如何监听确认,尽管这是我在服务器中使用的相同代码,所以我认为这可能是某种竞争条件,因为接收到的确认并不总是相同的

    //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
//单客户端
公共静态void main(字符串args[])引发IOException{

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

我不知道接收者在哪里循环读取消息,据我所知,你的接收者将在fi之后死亡rst消息已读取。为什么要将
cssocket
传递给接收方线程?为什么要同步
运行
?您的代码没有编译,这使得人们很难尝试。请发布一个简单的可编译代码示例,说明问题所在。缺少一系列方法,因此很难确切知道问题所在继续。我想你误解了。我们不需要更多的核心,我们需要更少的代码——这是一个简单的例子,说明了问题。更多的代码=更难调试。你甚至可以通过尝试删除原始代码中不必要的部分来发现问题。没有意义,除非你有多个
ServerSockets
,这本身就是问题所在没有道理。