網路程式設計-UDP
阿新 • • 發佈:2020-11-24
UDP傳送和接收訊息
-
傳送端
package com.phil.socket; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class TestUDP1 { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(); // String dataString = "你好 我是UDP1"; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true) { String string = reader.readLine(); DatagramPacket sendData = new DatagramPacket(string.getBytes(), 0, string.getBytes().length, InetAddress.getByName("localhost"), 8888); socket.send(sendData); if (string.equals("88")) break; } socket.close(); } }
-
接收端
package com.phil.socket; import java.net.DatagramPacket; import java.net.DatagramSocket; public class TestUDP2 { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(8888); byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); while (true) { socket.receive(packet); String s = new String(packet.getData()); System.out.println(s); if (s.equals("88")) break; } socket.close(); } }
UDP實現多執行緒聊天
-
傳送工具類
package com.phil.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; public class SendTool implements Runnable { private DatagramSocket socket = null; private BufferedReader reader = null; private int fromPort; private String toIP; private int toPort; public SendTool(int fromPort, String toIP, int toPort) { this.fromPort = fromPort; this.toIP = toIP; this.toPort = toPort; try { this.socket = new DatagramSocket(this.fromPort); }catch (Exception e) { e.printStackTrace(); } this.reader = new BufferedReader(new InputStreamReader(System.in)); } @Override public void run() { while (true) { String dataString = null; try { dataString = this.reader.readLine(); } catch (IOException e) { e.printStackTrace(); } byte[] bytes = dataString.getBytes(); DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIP, this.toPort)); try { this.socket.send(packet); } catch (IOException e) { e.printStackTrace(); } if (dataString.equals("88")) break; } this.socket.close(); } }
-
接收工具類
package com.phil.socket; import java.net.DatagramPacket; import java.net.DatagramSocket; public class RecieveTool implements Runnable { private DatagramSocket socket = null; private int port; private String name; public RecieveTool(int port, String name) { this.port = port; this.name = name; try { this.socket = new DatagramSocket(port); }catch (Exception e) { e.printStackTrace(); } } @Override public void run() { while (true) { try { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); if (this.socket==null) { System.out.println("傻逼哦"); break; } this.socket.receive(packet); String dataString = new String(packet.getData()); System.out.println(this.name + ": " + dataString); if (dataString.equals("88")) break; }catch (Exception e) { e.printStackTrace(); } } this.socket.close(); } }
-
角色1(兒子)
package com.phil.socket; public class Son { public static void main(String[] args) { new Thread(new SendTool(6666, "127.0.0.1", 7788)).start(); new Thread(new RecieveTool(6677, "老子")).start(); } }
-
角色2(老子)
package com.phil.socket; public class Father { public static void main(String[] args) { new Thread(new SendTool(7777,"localhost",6677)).start(); new Thread(new RecieveTool(7788,"兒子")).start(); } }