//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(); } } }
时间: 2024-10-25 19:39:00