- import java.io.*;
- import java.net.*;
- class Send implements Runnable
- {
- private DatagramSocket ds;
- public Send(DatagramSocket ds){
- this.ds = ds;
- }
- public void run(){
- try
- {
-
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while ((line = bufr.readLine())!= null)
- {
- if ("886".equals(line))
- <font color="red">break;</font>
- byte[] buf = line.getBytes();
- DatagramPacket dp =
- new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.103"),10005);
- ds.send(dp);
- }
- <font color="red">ds.close();</font>
- }
- catch (Exception e)
- {
- System.out.println("出现异常,发送失败");
- }
- }
- }
- class Rece implements Runnable
- {
- private DatagramSocket ds;
- public Rece(DatagramSocket ds){
- this.ds = ds;
- }
- public void run(){
- try
- {
- while (true)
- {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- String ip = dp.getAddress().getHostAddress();
- String data = new String(dp.getData(),0,dp.getLength());
- System.out.println(ip+":::"+data);
- }
- }
- catch (Exception e)
- {
- System.out.println("出现异常,接收失败");
- }
- }
- }
- class ChatDemo
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket sendSocket = new DatagramSocket();
- DatagramSocket receSocket = new DatagramSocket(10005);
- new Thread(new Send(sendSocket)).start();
- new Thread(new Rece(receSocket)).start();
- }
- }
复制代码 今天又试试,发现按下886中断不了程序,break,跳出while循环之后,ds关闭,接下来dos命令行卡在那里了,怎么改写让程序关闭, |