我想使三臺服務器使用java UDP進行通信。如果在第一個服務器實例上調用了所有三個服務器的特定方法,則其他兩個服務器也應該使用UDP將這些方法的結果傳遞給第一個服務器。我應該使用多播嗎?我試過使用單播,但它沒有工作。UDP單播使用java的三臺服務器之間的通信
UDPDataExchangeclass
import java.io.BufferedReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public class UDPDataExchange {
String Hostname;
static InetAddress IPAddress;
UDPDataExchange() {
Hostname = new String("127.0.0.1");
try {
IPAddress = InetAddress.getByName(Hostname);
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void requestData(int portNumber, String data) {
try {
DatagramSocket clientSocket = new DatagramSocket();
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
byte[] sendData = new byte[1024];
sendData = data.getBytes();
System.out.print("Ready to send data ");
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, portNumber);
clientSocket.send(sendPacket);
clientSocket.setSoTimeout(10000);
try {
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
InetAddress returnIPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
System.out.println("From server at: " + returnIPAddress + ":"
+ port);
System.out.println("Message: " + modifiedSentence);
}
catch (SocketTimeoutException ste) {
System.out.println("Timeout Occurred: Packet assumed lost");
}
//clientSocket.close();
}
catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void responseData(int portNumber) {
try {
DatagramSocket serverSocket = new DatagramSocket(portNumber);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while (true) {
receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
System.out.println("Waiting for datagram packet");
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
System.out.println("From: " + IPAddress + ":" + port);
System.out.println("Message: " + sentence);
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
catch (SocketException ex) {
System.out.println("UDP Port 9876 is occupied.");
System.exit(1);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
UDPTest
import java.io.*;
import java.net.*;
class UDPTest extends UDPDataExchange {
public static void main(String args[]) throws Exception {
int UDPClientConcordiaPortNumber = 9876;
int UDPClientMcgillPortNumber=9890;
int UDPClientDawsonPortNumber=9891;
UDPTest McgillServer=new UDPTest();
UDPTest DawsonServer=new UDPTest();
McgillServer.responseData(UDPClientConcordiaPortNumber);
DawsonServer.responseData(UDPClientDawsonPortNumber);
UDPTest concordiaTest = new UDPTest();
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + UDPClientConcordiaPortNumber);
String concordiaData = "Hello from concordia";
System.out.println("Sending data " + concordiaData.length()
+ " bytes to server.");
concordiaTest.requestData(UDPClientConcordiaPortNumber, concordiaData);
}
}
在UDPDataExchange我定義了兩種方法來發送和接收數據。在UDPTest類中,我試圖創建兩個服務器實例,即mcgill和dawson服務器,它們將偵聽concordia的端口以發送數據。一旦他們收到它,他們發送迴應給康科迪亞
請發佈代碼,看看發生了什麼。 – rafalopez79 2014-10-30 21:30:20
單播應該工作。你能發佈錯誤還是問題是什麼?他們不能達到對方或什麼? – ldmtwo 2014-10-30 23:30:47
@ldmtwo:他們無法到達其他第一臺服務器啓動,並在那一點執行被暫停 – Aayush 2014-10-30 23:34:14