* 应用层:HTTP FTP
UDP例子程序:
package javaBase.net;import java.net.*;/* * 发送端 */public class UdpSend { public static void main(String[] args) throws Exception{ //1,创建udp服务,DatagramSocket(); DatagramSocket ds = new DatagramSocket(8888);//自定义数字标识8888 //2,确定数据,并封装成数据包 byte[] buf = "udp said i am coming".getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.4"),10000); //3,用send()方法,发送数据包 ds.send(dp); //4,关闭资源 ds.close(); }}
package javaBase.net;import java.io.*;import java.net.DatagramPacket;import java.net.DatagramSocket;/* * 接收端 */public class UdpReceive { public static void main(String[] args) throws Exception{ //1,创建udp socket,建立端点 DatagramSocket ds = new DatagramSocket(10000);//指定端口数字标识 //2,定义接收包,存放接收到的数据 byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); //3,通过receive()方法,接收数据并写入包中 ds.receive(dp); //4,从包中提取数据 String ip = dp.getAddress().getHostAddress();//获取本机IP? int port = dp.getPort();//获取发送端端口 String data = new String(dp.getData(),0,dp.getLength());//获取数据 System.out.println(ip+" : "+port+"["+data+"]"); //5,关闭资源 ds.close(); }}
键盘输入,循环接收:
发送端:
package javaBase.net;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.*;/* * 发送端 */public class UdpSend { public static void main(String[] args) throws Exception{ send2(); } /* * 键盘输入 */ public static void send2() throws Exception{ DatagramSocket ds = new DatagramSocket(8888); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = in.readLine())!=null){ if("over".equals(line)) break; byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("localhost"),10000); ds.send(dp); } ds.close(); } /* * 详细步骤,一次性 */ public static void send1()throws Exception{ //1,创建udp服务,DatagramSocket(); DatagramSocket ds = new DatagramSocket(8888);//自定义数字标识8888 //2,确定数据,并封装成数据包 byte[] buf = "udp said i am coming".getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.4"),10000); //3,用send()方法,发送数据包 ds.send(dp); //4,关闭资源 ds.close(); }}接收端:
package javaBase.net;import java.io.*;import java.net.DatagramPacket;import java.net.DatagramSocket;/* * 接收端 */public class UdpReceive { public static void main(String[] args) throws Exception{ receive2(); } /* * 循环等待接收 */ public static void receive2() throws Exception{ DatagramSocket ds = new DatagramSocket(10000); 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 static void receive1() throws Exception{ //1,创建udp socket,建立端点 DatagramSocket ds = new DatagramSocket(10000);//指定端口数字标识 //2,定义接收包,存放接收到的数据 byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); //3,通过receive()方法,接收数据并写入包中 ds.receive(dp); //4,从包中提取数据 String ip = dp.getAddress().getHostAddress();//获取发送端IP? int port = dp.getPort();//获取发送端端口 String data = new String(dp.getData(),0,dp.getLength());//获取数据 System.out.println(ip+" : "+port+"["+data+"]"); //5,关闭资源 ds.close(); }}
即时聊天程序
package javaBase.net;import java.net.*;import java.io.*;/* * 发送线程 */class Send implements Runnable{ //成员socket private DatagramSocket ds ; //构造方法接收传入socket public Send(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = in.readLine())!=null){ if("over".equals(line)) break; byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("localhost"),10002); ds.send(dp); } } catch (Exception e) { throw new RuntimeException("发送端错误"); } }}/* * 接收线程 */class Receive implements Runnable{ //成员socket private DatagramSocket ds ; //构造方法初始化socket public Receive(DatagramSocket ds) { this.ds = ds; } @Override 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) { throw new RuntimeException("接收端错误"); } }}public class Chat { public static void main(String[] args) throws Exception{ DatagramSocket sendSocket = new DatagramSocket();//发送端可以随机端口号 DatagramSocket receiveSocket = new DatagramSocket(10002);//接收端指定端口 new Thread(new Send(sendSocket)).start(); new Thread(new Receive(receiveSocket)).start(); }}