我正在使用多播编写Java聊天应用程序。客户端之间可以进行通信,但是它们也可以向服务器发送预定义的消息,服务器始终对相应的预定义消息具有相同的答案。
客户端和服务器都可以接收和发送消息。它们都将收件人预订到相同的主机和端口,因此它们的套接字是相同的,但是当客户端向服务器发送预定义的消息时,服务器陷入无限循环,接收到相同的消息。
代码服务器
final Lock lock = new ReentrantLock();
final Condition Rx = lock.newCondition();
final Condition Tx = lock.newCondition();
private volatile boolean msgRead;
private volatile int TypeMsg;
try{
NetworkInterface nif = NetworkInterface.getByName("en1");
MSocket = new MulticastSocket(port_chat);
group = InetAddress.getByName(adresse_chat);
MSocket.joinGroup(new InetSocketAddress(adresse_chat, port_chat), nif);
}catch(IOException se){
System.out.println(this.toString() + " IOException -> " + se.getMessage());
}
/*
Thread Rx
*/
new Thread(){
@Override
public void run(){
while(!isInterrupted()){
lock.lock();
try{
while(msgRead == true)
Rx.await();
byte[] buf = new byte[256];
DatagramPacket packetRx = new DatagramPacket(buf, buf.length);
try{
MSocket.receive(packetRx);
}catch(IOException ioe){
System.out.println(this.toString() + " IOException -> " + ioe.getMessage());
}
String received = new String(packetRx.getData(), 0, packetRx.getLength());
if("end".equals(received))
break;
if(received.contains("WEATHER_FORECAST") == true)
TypeMsg = 1;
else
if(received.contains("ASK_AGE_CAPTAIN") == true)
TypeMsg = 2;
msgRead = true;
Tx.signal();
}catch(InterruptedException ie){
System.out.println("Thread Rx -> " + ie.getMessage());
}
finally{
lock.unlock();
}
}
}
}.start();
/*
Thread Tx
*/
new Thread(){
@Override
public void run(){
while(!isInterrupted()){
lock.lock();
try{
while(msgRead == false)
Tx.await();
byte[] buf = new byte[256];
/* switch(TypeMsg){...} */
buf = text.getBytes();
DatagramPacket packetTx = new DatagramPacket(buf, buf.length, group, port_chat);
try{
MSocket.send(packetTx);
}catch(IOException ioe){
System.out.println(this.toString() + " IOException -> " + ioe.getMessage());
}
msgRead = false;
Rx.signal();
}catch(InterruptedException ie){
System.out.println("Thread Tx -> " + ie.getMessage());
}finally{
lock.unlock();
}
}
}
}.start();
代码客户端
try{
NetworkInterface nif = NetworkInterface.getByName("en1");
MSocket = new MulticastSocket(port_chat);
group = InetAddress.getByName(adresse_chat);
MSocket.joinGroup(new InetSocketAddress(adresse_chat, port_chat), nif);
}catch(IOException se){
System.out.println(this.toString() + " IOException -> " + se.getMessage());
}
/*
Thread Rx
*/
new Thread(){
@Override
public void run(){
while(!isInterrupted()){
byte[] buf = new byte[256];
DatagramPacket packetRx = new DatagramPacket(buf, buf.length);
try{
MSocket.receive(packetRx);
}catch(IOException ioe){
System.out.println(this.toString() + " IOException -> " + ioe.getMessage());
}
String received = new String(packetRx.getData(), 0, packetRx.getLength());
if("end".equals(received))
break;
jTextArea_Rx.append(received + "\n");
}
}
}.start();
/*
Tx
*/
private void jButton_SendActionPerformed(java.awt.event.ActionEvent evt) {
byte[] buf = new byte[256];
String text = username + " >> " + jTextArea_Tx.getText();
buf = text.getBytes();
DatagramPacket packetTx = new DatagramPacket(buf, buf.length, group, port_chat);
try{
MSocket.send(packetTx);
}catch(IOException ioe){
System.out.println(this.toString() + " IOException -> " + ioe.getMessage());
}
}
我知道了,实际上我在客户端和服务器上使用了相同的IP地址。更改服务器的IP地址即可解决问题。我也将异常移到了while循环之外。