代码如下:在Eclipse上运行的,没有报错也没有异常,功能实现不了。不知道怎么回事了,大神在哪,快来帮忙看看,感激不尽!
- import java.net.*;
- import java.io.*;
- 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))
- break;
- byte[] buf = line.getBytes();
- DatagramPacket dp =
- new DatagramPacket(buf,buf.length,InetAddress.getByName("117.40.37.5"),9003);
- ds.send(dp);
- }
- }
- catch(Exception e)
- {
- throw new RuntimeException("发送失败");
- }
- }
- }
- 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)
- {
- throw new RuntimeException("接收失败");
- }
- }
- }
- public class ChatDemo{
- public static void main (String[] args)throws Exception
- {
- DatagramSocket sendsocket = new DatagramSocket();
- DatagramSocket recesocket = new DatagramSocket(9003);
- new Thread(new Send(sendsocket)).start();
- new Thread(new Rece(recesocket)).start();
- }
- }
复制代码 |
|