Day62.UDP网络编程 -Java常用类、集合、IO#

    科技2022-08-15  85

    UDP网络编程

    /* UDP协议的网络编程 */ public class UDPTest { //发送端 @Test public void sender() throws IOException { DatagramSocket socket = null; try { socket = new DatagramSocket(); String str = "我是UDP方式发送的导弹"; byte[] data = str.getBytes(); InetAddress inet = InetAddress.getByName("127.0.0.1"); DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090); socket.send(packet); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null){ try { socket.close(); } catch (Exception e) { e.printStackTrace(); } } } } //接受端 @Test public void receiver() throws IOException { DatagramSocket socket = null; try { socket = new DatagramSocket(9090); byte[] buffer =new byte[100]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null){ try { socket.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
    Processed: 0.009, SQL: 8