我想让三台服务器使用java UDP进行通信。如果在第一个服务器实例上调用了存在于所有三个服务器的特定方法,则其他两个服务器也应该使用UDP将这些方法的结果传递给第一个服务器。我应该使用多播吗?我尝试使用Unicast,但它没有成功。
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的端口以发送数据。收到后,他们会将回复发送回Concordia
答案 0 :(得分:2)
试试这个,我不确定它是不是你想要的。
UDPDataExchange
public class UDPDataExchange {
String Hostname;
static InetAddress IPAddress;
UDPDataExchange() {
Hostname = new String("127.0.0.1");
try {
IPAddress = InetAddress.getLocalHost();
} catch (final UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void requestData(final int portNumber, final String data) {
try {
final DatagramSocket clientSocket = new DatagramSocket();
final byte[] receiveData = new byte[1024];
final DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
byte[] sendData = new byte[1024];
sendData = data.getBytes();
System.out.print("Ready to send data ");
final DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, portNumber);
clientSocket.send(sendPacket);
clientSocket.setSoTimeout(10000);
try {
clientSocket.receive(receivePacket);
final String modifiedSentence = new String(receivePacket.getData());
final InetAddress returnIPAddress = receivePacket.getAddress();
final int port = receivePacket.getPort();
System.out.println("From server at: " + returnIPAddress + ":"
+ port);
System.out.println("Message: " + modifiedSentence);
} catch (final SocketTimeoutException ste) {
System.out.println("Timeout Occurred: Packet assumed lost");
}
// clientSocket.close();
} catch (final SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void responseData(final int portNumber) {
try {
final DatagramSocket serverSocket = new DatagramSocket(portNumber, IPAddress);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while (true) {
receiveData = new byte[1024];
final DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
System.out.println("Waiting for datagram packet");
serverSocket.receive(receivePacket);
final String sentence = new String(receivePacket.getData());
final InetAddress IPAddress = receivePacket.getAddress();
final int port = receivePacket.getPort();
System.out.println("From: " + IPAddress + ":" + port);
System.out.println("Message: " + sentence);
final String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
final DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
} catch (final SocketException ex) {
System.out.println("UDP Port 9876 is occupied.");
System.exit(1);
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
UDPTest
class UDPTest extends UDPDataExchange {
public static void main(final String args[]) throws Exception {
final int UDPClientConcordiaPortNumber = 9876;
final int UDPClientMcgillPortNumber = 9890;
final int UDPClientDawsonPortNumber = 9891;
final UDPTest McgillServer = new UDPTest();
final UDPTest DawsonServer = new UDPTest();
final Thread t1 = new Thread() {
@Override
public void run() {
McgillServer.responseData(UDPClientMcgillPortNumber);
}
};
t1.setDaemon(true);
t1.start();
final Thread t2 = new Thread() {
@Override
public void run() {
DawsonServer.responseData(UDPClientDawsonPortNumber);
}
};
t2.setDaemon(true);
t2.start();
final UDPTest concordiaTest = new UDPTest();
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + UDPClientConcordiaPortNumber);
final String concordiaData = "Hello from concordia";
System.out.println("Sending data " + concordiaData.length()
+ " bytes to server.");
concordiaTest.requestData(UDPClientMcgillPortNumber, concordiaData);
concordiaTest.requestData(UDPClientDawsonPortNumber, concordiaData);
}
}
我会解释brielfy我改变了什么:
UDPDataExchange: 我将IPAdress更改为InetAddress.getLocalHost()
UDPTest: 每次对responseData的调用都在一个单独的Thread中运行,以避免阻塞主线程。 该线程是守护进程以允许退出应用程序。 requestData()调用已更改为调用每个服务器。
希望有用。