Udp實現訊息的發送和接收、以及圖片的上傳

來源:互聯網
上載者:User

標籤:als   ams   content   put   res   system.in   tput   ati   pre   

//Udp實現訊息的發送和接收

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketException;import java.util.Scanner;public class UdpUtils implements Runnable {    //定義Socket資料包服務    private DatagramSocket socket;    public UdpUtils(int port) {        try {            //建立socket資料包服務            socket = new DatagramSocket();        } catch (SocketException e) {            e.printStackTrace();        }    }    //發送    public void send(String content, String ip, int port) {        //擷取接收端 的IP 和 連接埠號碼        InetSocketAddress address = new InetSocketAddress(ip, port);         //建立資料包  並將 訊息內容 、地址 傳入        DatagramPacket dp = new DatagramPacket(content.getBytes(),                content.getBytes().length,address);        try {            //發送資料包            socket.send(dp);            try {                Thread.sleep(1);    //防止Udp傳輸時,包錯誤。            } catch (InterruptedException e) {                e.printStackTrace();            }        } catch (IOException e) {            e.printStackTrace();        }    }    //接收    @Override    public void run() {        byte[] buf = new byte[1024];        //建立資料包 將 發送過來的 訊息內容 取出        DatagramPacket dp = new DatagramPacket(buf, buf.length);        while (true) {            try {                //接收資料包                socket.receive(dp);            //    System.out.println(new String(dp.getData(), 0, dp.getLength()));            } catch (IOException e) {                e.printStackTrace();            }        }    }}//Test 測試class TestUdpUtils{    public static void main(String[] args) {        UdpUtils utils = new UdpUtils(10010);        Thread thread = new Thread(utils);        thread.start();        Scanner input = new Scanner(System.in);        while(true){            String msg = input.next();            if(msg.equals("exit")){                input.close();                System.exit(0);            }            utils.send("Send:" + msg, "127.0.0.1", 10010);        }    }}

 

//Udp實現圖片的上傳

 

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketException;public class UdpMapUtils implements Runnable {    private DatagramSocket socket;    private File file;    private String ip;    private int port;    public UdpMapUtils(File file, String ip, int port) {        this.file = file;        this.ip = ip;        this.port = port;        try {            socket = new DatagramSocket(port);        } catch (SocketException e) {            e.printStackTrace();        }    }    public void send(File file) {        InetSocketAddress address = new InetSocketAddress(ip, port);    //    System.out.println(address);        BufferedInputStream bis = null;        try {            bis = new BufferedInputStream(new FileInputStream(file));            byte[] buf = new byte[1024];            while (bis.read(buf) != -1) {                DatagramPacket dp = new DatagramPacket(buf, buf.length, address);                socket.send(dp);                Thread.sleep(2);            }            byte[] b = "over".getBytes();            DatagramPacket dp = new DatagramPacket(b, b.length,address);            socket.send(dp);        } catch (Exception e) {            e.printStackTrace();        }         finally{            try {                bis.close();            } catch (IOException e) {                e.printStackTrace();            }            socket.close();        }    }    @Override    public void run() {        BufferedOutputStream bos = null;        try {            bos = new BufferedOutputStream(new FileOutputStream(file));            byte[] buf = new byte[1024];            while (true) {                DatagramPacket dp = new DatagramPacket(buf, buf.length);                socket.receive(dp);                if(new String(buf,0,dp.getLength()).equals("over"))                    break;                bos.write(buf);                bos.flush();            }        } catch (Exception e) {            e.printStackTrace();        }         finally {            try {                bos.close();            } catch (IOException e) {                e.printStackTrace();            }            socket.close();        }    }}

 

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.