一个线程的问题

y63964632 y63964632 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:
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端口..

是什么问题呢?
...全文
评论 6
收藏 已收藏
分享
返回顶部
y63964632
Java SE 62,621 成员 307,251 内容