標籤: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傳輸協議