A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.io.*;
  2. import java.net.*;

  3. class  DuiHua
  4. {
  5.         public static void main(String[] args) throws Exception
  6.         {
  7.                 DatagramSocket sendSocket = new DatagramSocket();
  8.                 DatagramSocket receSocket = new DatagramSocket(10005);
  9.                

  10.                 new Thread(new Send(sendSocket)).start();
  11.                 new Thread(new Rece(receSocket)).start();
  12.                

  13.         }
  14. }
  15. class Send implements Runnable
  16. {
  17.         private DatagramSocket ds;
  18.         public Send(DatagramSocket ds)
  19.         {
  20.                 this.ds=ds;
  21.         }
  22.         public void run()
  23.         {
  24.                 try
  25.                 {
  26.                         BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
  27.                         String line = null;
  28.                         while ((line = bufr.readLine())!=null)
  29.                         {
  30.                                 if("886".equals(line))
  31.                                         break;
  32.                                 byte[] buf = line.getBytes();

  33.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),100005);
  34.                                 ds.send(dp);
  35.                                 //ds.close();

  36.                         }
  37.                 }
  38.                 catch (Exception e)
  39.                 {
  40.                         throw new RuntimeException("发送端失败");
  41.                 }
  42.         }
  43. }
  44. class Rece implements Runnable
  45. {
  46.         private DatagramSocket ds;
  47.         public Rece(DatagramSocket ds)
  48.         {
  49.                 this.ds=ds;
  50.         }
  51.        
  52.         public void run()
  53.         {
  54.                 try
  55.                 {
  56.                         while (true)
  57.                         {
  58.                                 byte[] arr = new byte[1024];

  59.                                 DatagramPacket dp = new DatagramPacket(arr,arr.length);

  60.                                 ds.receive(dp);

  61.                                 String ip = dp.getAddress().getHostAddress();

  62.                                 String data = new String(dp.getData(),0,dp.getLength());

  63.                                 System.out.println(ip+"::"+data);
  64.                         }
  65.                 }
  66.                 catch (Exception e)
  67.                 {
  68.                         throw new RuntimeException("接收端失败");
  69.                 }
  70.         }

  71. }
复制代码

1 个回复

正序浏览
java.lang.IllegalArgumentException: Port out of range:100005

端口超出最大了。65535
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马