我对Java和Eclipse IDE还是很陌生,在Windows的命令提示符下运行Java代码时遇到问题。如果我通过Eclipse IDE运行代码,则输出为
Message number 0
Message number 1
Message number 2
Timeout. Client is closing.
但是,如果我通过命令提示符java test.Main
运行代码,则命令提示符下的消息之间会出现很多空行。
//Hundreds of empty lines
Message number 0
//Hundreds of empty lines
Message number 1
//Hundreds of empty lines
Message number 2
//Hundreds of empty lines
Timeout. Client is closing.
我尝试添加简单的检查,以在字符串为NULL或为空但不起作用时不输出任何输出。下面是我的代码。我花了很多时间,但是不知道是什么原因造成的。
Main.java
package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// create 5 processes
public class Main {
public static void main(String[] args) {
int port = 50001;
UdpUnicastServer server = new UdpUnicastServer(port);
UdpUnicastClient client = new UdpUnicastClient(port);
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(client);
executorService.submit(server);
}
}
UdpUnicastClient.java
package test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* Created by dan.geabunea on 6/3/2016.
*/
public class UdpUnicastClient implements Runnable {
private final int port;
public UdpUnicastClient(int port) {
this.port = port;
}
@Override
public void run() {
/**
* Bind the client socket to the port on which you expect to
* read incoming messages
*/
try (DatagramSocket clientSocket = new DatagramSocket(port)) {
/**
* Create a byte array buffer to store incoming data. If the message length
* exceeds the length of your buffer, then the message will be truncated. To avoid this,
* you can simply instantiate the buffer with the maximum UDP packet size, which
* is 65506
*/
byte[] buffer = new byte[65507];
// Set a timeout of 3000 ms for the client.
clientSocket.setSoTimeout(3000);
while (true) {
DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
/**
* The receive method will wait for 3000 ms for data.
* After that, the client will throw a timeout exception.
*/
clientSocket.receive(datagramPacket);
String receivedMessage = new String(datagramPacket.getData());
if(receivedMessage != null && !receivedMessage.isEmpty()) {
System.out.println(receivedMessage);
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Timeout. Client is closing.");
}
}
}
UdpUnicastServer.java
package test;
import java.io.IOException;
import java.net.*;
/**
* Created by dan.geabunea on 6/3/2016.
*/
public class UdpUnicastServer implements Runnable {
/**
* The port where the client is listening.
*/
private final int clientPort;
public UdpUnicastServer(int clientPort) {
this.clientPort = clientPort;
}
@Override
public void run() {
/**
* Create a new server socket and bind it to a free port. I have chosen
* one in the 49152 - 65535 range, which are allocated for internal applications
*/
try (DatagramSocket serverSocket = new DatagramSocket(50000)) {
// The server will generate 3 messages and send them to the client
for (int i = 0; i < 3; i++) {
String message = "Message number " + i;
DatagramPacket datagramPacket = new DatagramPacket(
message.getBytes(),
message.length(),
InetAddress.getLocalHost(),
clientPort
);
serverSocket.send(datagramPacket);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}