一个线程的问题
代码如下:
import java.io.IOException;
import java.net.*;
public class TestMonitorPort {
public static void main(String[] args) {
printReceiveInfomationFromPort(6000);
}
static void printReceiveInfomationFromPort(int port) {
new Thread(new MonitorPortRunnable(port)).start();
}
}
class MonitorPortRunnable implements Runnable {
byte buf[] = new byte[1024];
DatagramSocket ds = null;
DatagramPacket dp = null;
int localReceivePort ;
public MonitorPortRunnable(int localReceivePort) {
this.localReceivePort = localReceivePort;
}
public void run() {
dp = new DatagramPacket(buf, 0, 1024);
try {
ds = new DatagramSocket(localReceivePort);
} catch (SocketException e1) {
//prompt("本地接收端口已被使用");
System.exit(0);
}
while (true) {
try {
ds.receive(dp);
System.out.println("信息来自:" + this.localReceivePort);
} catch (IOException e) {
ds.close();
e.printStackTrace();
}
String receiveMessage = new String(dp.getData(), 0, dp
.getLength());
System.out.println(receiveMessage);//暂时打印到控制台,一般输出到文件
}
}
本来运行一切正常,但是导出俩次jar包后,再次运行,每次都是一闪就过去了
而不是正常情况的一直监听6000端口..
是什么问题呢?