JAVA学习第六十课 — UDP协议 &基于多线程模拟简单的QQ聊天程序

来源:互联网 发布:听电子书的软件 编辑:程序博客网 时间:2024/04/28 14:17

UDP传输

UDP有发送端和接受端,有两大类,DatagramSocket、DatagramPacket

建立发送端和接收端

建立数据包

调用Socket的接收发送方法

关闭Socket

注意:发送端和接收端是两个独立的运行程序

UDP发送端演示

DatagramPacket(byte[] buf, int length,InetAddress address, int port)
构造数据报包,用来将长度为 length 的包发送到指定主机上的指定端口号。

public static void UDPsendDemo() throws IOException {System.out.println("发送端");/* * 创建UDP传输的发送端: * 1.建立UDP的Socket服务 * 2.将要发送的数据封装到数据包中 * 3.用UDP的Socket服务将数据包发送出去 * 4.关闭Socket服务 *///建立UDP的Socket服务,使用DatagramSocket对象DatagramSocket ds = new DatagramSocket();//不指定端口号,发送端,就是随机的//数据封装成包String str = "udp发送";//使用DatagramPacket将数据封装到该对象的包中byte[] by = str.getBytes();DatagramPacket dp = new DatagramPacket(by,by.length,InetAddress.getByName("127.0.0.1"),6534);//通过UDP的Socket服务将数据包发送出去,send方法ds.send(dp);ds.close();}


UDP接收端演示

public static void UDPreceiveDemo() throws IOException {//接受端System.out.println("接收端");/* * 创建UDP传输的接收端: * 1.建立UDP的Socket服务,因为要接收数据,必须要明确端口号 * 2.创建数据包,用于存储接收到的数据,以便于用数据包的方法解析这些数据 * 3.使用Socket服务的receive方法接收的数据存储到数据包中 * 4.通过数据包中的方法解析包中数据 * 5.关闭 *///建立UDP的Socket服务,使用DatagramSocket对象DatagramSocket ds = new DatagramSocket(6534);//接收端口6534//创建数据包byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf, buf.length);//使用接收方法将数据存储到数据包ds.receive(dp);//注意该方法是阻塞式的//使用数据包对象的方法,解析其中的数据,如:地址、端口、内容String ip = dp.getAddress().getHostAddress();//获取ip地址字符串int port = dp.getPort();//获取端口String datatext = new String(dp.getData(),0,dp.getLength());//将数据封装成字符串对象System.out.println("ip:"+ip+"port : "+port+"data:"+datatext);//关闭ds.close();}

基于多线程模拟简单的QQ聊天程序

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;class Receive implements Runnable{private DatagramSocket ds;public Receive(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();int port = dp.getPort();String datatext = new String(dp.getData(),0,dp.getLength());if(datatext.equals("over")){System.out.println("ip:"+ip+"port : "+port+"退出聊天室");}else{System.out.println("ip:"+ip+"port : "+port+"data:"+datatext);}}} catch (Exception e) {// TODO: handle exception}}}class Send implements Runnable{private DatagramSocket ds;public Send(DatagramSocket ds){this.ds = ds;}public void run(){try {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String line = null;while((line = br.readLine())!=null){byte[] buf = line.getBytes();DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.255"),6534);//IP的有效位是0-254,255是广播,也就是127.0.0,这一IP字段上所有存活的电脑都可以接收到我发的信息ds.send(dp);if("over".equals(line))break;}ds.close();} catch (Exception e) {// TODO: handle exception}}}public class Main {public static void main(String[] args) throws IOException{DatagramSocket dsend =new DatagramSocket();DatagramSocket dr =new DatagramSocket(6534);Send send = new Send(dsend);Receive receive = new Receive(dr);Thread t1 = new Thread(send);Thread t2 = new Thread(receive);t1.start(); t2.start();}}



1 0
原创粉丝点击