public class SendThread implements Runnable {
private DatagramSocket ds;
public SendThread(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
// 创建UDP发送端的服务
try {
// 把键盘录入数据用高效缓冲流封装
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
}
byte[] bys = line.getBytes();
// 数据包
DatagramPacket dp = new DatagramPacket(bys, bys.length,
InetAddress.getByName("192.168.1.255"), 12345);
// 发送数据
ds.send(dp);
}
// 关闭资源
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入流br为什么不用关闭啊? |
|