在调试的时候,出现了SocketException: java.net.BindException: Address already in use;
解决办法:
byte[] msg = new byte[1];
Integer port = 8080;
DatagramSocket dSocket = null;
DatagramPacket dPacket = new DatagramPacket(msg, msg.length);
try {
// dSocket = new DatagramSocket(port);
if(dSocket==null){
dSocket = new DatagramSocket();
dSocket.setReuseAddress(true);
dSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),port));
}
while (true) {
try {
dSocket.receive(dPacket);
...........................................
} catch (IOException e) {
e.printStackTrace();
}
if (null != dPacket.getData().toString())
break;
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}