1. 程式人生 > >Udp實現消息的發送和接收、以及圖片的上傳

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

cat 數據 art ram pan ket length out leo

//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實現消息的發送和接收、以及圖片的上傳