我创建了2个java程序,客户端ping服务器,服务器回复客户端。我遇到的问题是,当我尝试在同一台机器上运行客户端和服务器时,程序运行正常。但是当我试图单独运行客户端和服务器(即虚拟机中另一台机器上的客户端和服务器)时,我没有得到服务器的响应。有什么帮助吗?
客户端发送ping请求的功能
public static void ping(String serverIP, int numberOfPacketToSend,int setTimeOut,int sendDa){
int lossPacket = 0;
try {
String serverHostname = new String (serverIP);
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(serverHostname);
System.out.println ("connecting to " + IPAddress+"...");
byte[] sendData = new byte[sendDa];
byte[] receiveData = new byte[sendDa];
for(int i = 0;i<numberOfPacketToSend;i++){
String sentence = "Pinging "+serverIP;
//sendData = sentence.getBytes();
// System.out.println ("Sending data to " + sendData.length +
// " bytes to server.");
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
//System.out.println("data being send to server "+sendData.length);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
// System.out.println ("Waiting for return packet");
clientSocket.setSoTimeout(setTimeOut);
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("Reply from "+serverIP+" bytes="+sendData.length);
}
catch (SocketTimeoutException ste)
{
System.out.println ("Timeout Occurred: Packet assumed lost");
lossPacket ++;
}}
clientSocket.close();
}
catch (UnknownHostException ex) {
System.err.println(ex);
}
catch (IOException ex) {
System.err.println(ex);
}
System.out.println("Total loss packet = "+lossPacket);
System.out.println("Number of packet send ="+numberOfPacketToSend);
try{
System.out.println("Percentage packet loss ="+((numberOfPacketToSend/lossPacket)*100)+"%");
}catch(ArithmeticException e){System.out.println("Percentage packet loss = 0%");}
}
}
服务器等待任何连接的功能
public static void waitForConnection(int portNumber, String ServerAddress) throws IOException{
try
{
InetAddress addr = InetAddress.getByName("5.5.5.5");
DatagramSocket serverSocket = new DatagramSocket(null);
InetSocketAddress address = new InetSocketAddress(ServerAddress, portNumber);
serverSocket.bind(address);
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 ("Server up and running on port "+portNumber);
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);
}
}