package com.wuming.chat; public class TalkStudent { public static void main(String[] args) { //开启两个线程 new Thread(new TalkSend(7777,"localhost",9999)).start(); new Thread(new TalkReceive(8888,"老师")).start(); } }
==============
package com.wuming.chat; public class TalkTeacher { public static void main(String[] args) { new Thread(new TalkSend(5555,"localhost",8888)).start();//5555端口是随便写的;8888对应学生端口 new Thread(new TalkReceive(9999,"学生")).start();//9999端口对应学生端口 } }
=============
package com.wuming.chat; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class TalkReceive implements Runnable{ /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ DatagramSocket socket=null; private int port; private String msgFrom; public TalkReceive(int port,String msgFrom) { this.port = port; this.msgFrom = msgFrom; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { try { //准备接受包裹 byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container, 0, container.length); socket.receive(packet);//阻塞式接受包裹 //断开连接 bye byte[] data = packet.getData(); String receiveData = new String(data, 0, packet.getLength()); System.out.println(msgFrom + ":" + receiveData); if (receiveData.equals("bye")) { break; } } catch (IOException e) { e.printStackTrace(); } } } } =======================
package com.wuming.chat; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; public class TalkSend implements Runnable{ /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ DatagramSocket socket=null; BufferedReader reader=null; private int fromPort; private String toIp; private int toPort; public TalkSend(int fromPort, String toIp, int toPort) { this.fromPort = fromPort; this.toIp = toIp; this.toPort = toPort; try { socket = new DatagramSocket(fromPort); //准备数据:控制台读取System.in BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { //准备数据:控制台读取System.in BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true){ try{ String data = reader.readLine(); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas, 0, data.length(), new InetSocketAddress(this.toIp,this.toPort)); socket.send(packet); if (data.equals("bye")){ break; } }catch(Exception e){ e.printStackTrace(); } } } }
===============
控制台输入:
打开下面这个结果为