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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周博文 中级黑马   /  2015-8-17 14:08  /  872 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

发送端:
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.Reader;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.net.InetAddress;

  8. /*
  9. * 发送端程序:用于发送信息
  10. */
  11. public class SendMessage implements Runnable {
  12.         /*
  13.          * 发送端成员变量: socket对象 目的地ip地址 通信端口号
  14.          */
  15.         private DatagramSocket socket;
  16.         private InetAddress ip;
  17.         private int port;

  18.         public SendMessage(DatagramSocket socket, InetAddress ip, int port) {
  19.                 super();
  20.                 this.socket = socket;
  21.                 this.ip = ip;
  22.                 this.port = port;
  23.         }

  24.         @Override
  25.         public void run() {
  26.                 // 缓冲输入流用于读取用户输入
  27.                 Reader in = new InputStreamReader(System.in);
  28.                 BufferedReader reader = new BufferedReader(in);
  29.                 while (true) {
  30.                         try {
  31.                                 // 读取用户输入,并打包数据
  32.                                 String line = reader.readLine();
  33.                                 int length = line.getBytes().length;
  34.                                 DatagramPacket dp = new DatagramPacket(line.getBytes(), length, ip, port);
  35.                                 // 发送数据报包
  36.                                 socket.send(dp);
  37.                                 // 如果输入886,结束循环
  38.                                 if("886".equals(line)){
  39.                                         break;
  40.                                 }
  41.                                
  42.                         } catch (IOException e) {
  43.                                 e.printStackTrace();
  44.                         }
  45.                 }
  46.                 // 关闭资源
  47.                 try {
  48.                         reader.close();
  49.                         socket.close();
  50.                 } catch (IOException e) {
  51.                         e.printStackTrace();
  52.                 }
  53.         }

  54. }
复制代码

接收端:
  1. import java.io.IOException;
  2. import java.net.DatagramPacket;
  3. import java.net.DatagramSocket;

  4. /*
  5. * 接收端程序:用于接收信息
  6. */
  7. public class ReceiveMessage implements Runnable {
  8.         /*
  9.          * 接收端成员变量: 接收端socket对象
  10.          */
  11.         private DatagramSocket socket;

  12.         public ReceiveMessage(DatagramSocket socket) {
  13.                 super();
  14.                 this.socket = socket;
  15.         }

  16.         @Override
  17.         public void run() {
  18.                 while (true) {
  19.                         // 接收数据并打印到控制台
  20.                         DatagramPacket dp = new DatagramPacket(new byte[1024], 0, 1024);
  21.                         try {
  22.                                 socket.receive(dp);
  23.                         } catch (IOException e) {
  24.                                 e.printStackTrace();
  25.                         }
  26.                         String data = new String(dp.getData(), 0, dp.getLength());
  27.                         System.out.println(dp.getAddress().getHostName() + "-" + dp.getPort() + " : " + data);
  28.                        
  29.                 }
  30.         }

  31. }
复制代码

客户端1:
  1. import java.io.IOException;
  2. import java.net.DatagramSocket;
  3. import java.net.InetAddress;

  4. public class Client1 {
  5.         public static void main(String[] args) throws IOException {
  6.                 SendMessage sm = new SendMessage(new DatagramSocket(), InetAddress.getByName("kaven-PC"), 10010);
  7.                 ReceiveMessage rm = new ReceiveMessage(new DatagramSocket(10011));
  8.                
  9.                 Thread t1 = new Thread(sm);
  10.                 Thread t2 = new Thread(rm);
  11.                
  12.                 t1.start();
  13.                 t2.start();
  14.         }
  15. }
复制代码

客户端2:

  1. import java.io.IOException;
  2. import java.net.DatagramSocket;
  3. import java.net.InetAddress;

  4. public class Client1 {
  5.         public static void main(String[] args) throws IOException {
  6.                 SendMessage sm = new SendMessage(new DatagramSocket(), InetAddress.getByName("kaven-PC"), 10010);
  7.                 ReceiveMessage rm = new ReceiveMessage(new DatagramSocket(10011));
  8.                
  9.                 Thread t1 = new Thread(sm);
  10.                 Thread t2 = new Thread(rm);
  11.                
  12.                 t1.start();
  13.                 t2.start();
  14.         }
  15. }
复制代码



5 个回复

倒序浏览
运行结果:
回复 使用道具 举报
自己跟自己说话呢?
回复 使用道具 举报
这个年代 自娱自乐也需要技术
回复 使用道具 举报
不错不错要是我会建立个图形化界面有有趣很多呵呵
回复 使用道具 举报
℃葫芦 发表于 2015-8-17 16:57
自己跟自己说话呢?

。。。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马