我有一个它应该是P2P结构化覆盖,服务器启动,从客户端侦听注册消息,然后应该向客户端发送确认。
问题是我只收到1个确认,即使10个被发出。为了简化我在StartNode()调用中从客户端发送注册,我想要的是节点在发送注册消息后开始侦听响应,但是只接收到一条确认消息。
如果工作正常我的注册。我的服务器获取所有10条消息,并在相应的端口上发出确认,但只收到1个确认。我认为问题可能在于我是如何监听确认的,虽然它与我在服务器中使用的代码相同,所以我认为它可能是某种竞争条件,因为并不总是接受相同的确认。 / p>
//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
答案 0 :(得分:-1)
我弄清楚了我的问题,我之前收到一条消息的原因是因为我的ServerSocket被声明为静态,这意味着,当我的侦听器启动时,它被覆盖到最后一个实例化的Node。