java中UDP傳輸協議

來源:互聯網
上載者:User

標籤:java   udp   

package pack;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;/*public class Demo {    public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        //method1();    }    public static void method1() throws Exception {        InetAddress i = InetAddress.getLocalHost();        System.out.println(i.toString());        InetAddress ia = InetAddress.getByName("www.baidu.com");        System.out.println(ia.getHostAddress());    }}發送接收資料class UdpSend {    public static void main(String[] args) throws Exception {        //1.建立udp服務,通過DatagramSocket對象        DatagramSocket ds = new DatagramSocket();        //2.確定資料,封裝成包        byte[] buf = "udp send".getBytes();        DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.2"),10000);        //3.通過socket服務,將已有的資料包發送出去        ds.send(dp);        ds.close();    }}class UdpReceive {    public static void main(String[] args) throws Exception {        //1.建立udp socket,建立端點,指定連接埠        DatagramSocket ds = new DatagramSocket(10000);        while(true) {            //2.定義資料包,用於儲存資料            byte[] buf = new byte[1024];            DatagramPacket dp = new DatagramPacket(buf,buf.length);            //3.通過服務的receive接收資料            ds.receive(dp);            //4.通過資料包對象擷取ip地址與資料            String ip = dp.getAddress().getHostAddress();            String data = new String(dp.getData(),0,dp.getLength());            int port = dp.getPort();            System.out.println(ip+":"+data+":"+port);            ds.close();        }    }} 鍵盤錄入發送資料class UdpSend2 {    public static void main(String[] args) throws Exception {        DatagramSocket ds = new DatagramSocket();        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));        String line = bufr.readLine();        while(line!=null) {            if("over".equals(line)) {                break;            }            byte[] buf = line.getBytes();            DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.10"),10001);            ds.send(dp);        }    }}class UdpRece2 {    public static void main(String[] args) throws Exception {        DatagramSocket ds = new DatagramSocket(100001);        while(true) {            byte[] buf =new byte[1024];            DatagramPacket dp = new DatagramPacket(buf,buf.length);            ds.receive(dp);            String ip = dp.getAddress().getHostAddress();            String data = new String(dp.getData(),0,dp.getLength());            System.out.println(ip+":"+data);        }    }}*//*聊天程式*/public class Demo {    public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        DatagramSocket sendSocket = new DatagramSocket();        DatagramSocket receSocket = new DatagramSocket(10001);        new Thread(new Send(sendSocket)).start();        new Thread(new Rece(receSocket)).start();    }}class Send implements Runnable {    private DatagramSocket ds;    Send(DatagramSocket ds) {        this.ds = ds;    }    public void run() {        try {            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));            String line = bufr.readLine();            while(line!=null) {                if("over".equals(line))                    break;            byte[] buf = line.getBytes();            DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.10"),10001);            ds.send(dp);            }        }        catch(Exception e) {            System.out.println("error");        }    }}class Rece implements Runnable {    private DatagramSocket ds;    Rece(DatagramSocket ds) {        this.ds = ds;    }    public void run() {        try {            while(true) {             byte[] buf =new byte[1024];            DatagramPacket dp = new DatagramPacket(buf,buf.length);            ds.receive(dp);            String ip = dp.getAddress().getHostAddress();            String data = new String(dp.getData(),0,dp.getLength());            System.out.println(ip+":"+data);            }        }        catch(Exception e) {            System.out.println("error");        }    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java中UDP傳輸協議

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.