UDP小实例(模仿在线咨询)
程序员文章站
2022-06-16 10:18:50
package cn.lxq.net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; impor ......
package cn.lxq.net; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.inetsocketaddress; import java.net.socketexception; /* * 发送端 * 1. 使用datagramsocket 指定端口 创建接收端 * 2.准备数据一定要转化成字节数组 * 3.封装成datagrampacket 目的地 ip+端口 * 4.发送数据send(); * 5.释放数据 */ public class udptalkserver implements runnable{ private datagramsocket sender; private string loip; private int report; private bufferedreader reader; public udptalkserver(int port,string loip,int report){ try { this.sender=new datagramsocket(port); this.loip=loip; this.report=report; this.reader=new bufferedreader(new inputstreamreader(system.in)); } catch (socketexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public void run() { while(true) { string str; try { //* 2.准备数据一定要转化成字节数组 str=reader.readline(); byte[] datas=str.getbytes(); //* 3.封装成datagrampacket 目的地 ip+端口 datagrampacket packet=new datagrampacket(datas, 0,datas.length,new inetsocketaddress(this.loip, this.report)); //* 4.发送数据send(); sender.send(packet); //发送bye 退出线程 if(str.equals("bye")) { break; } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } sender.close(); } }
1 package cn.lxq.net; 2 3 import java.io.ioexception; 4 import java.net.datagrampacket; 5 import java.net.datagramsocket; 6 import java.net.socketexception; 7 8 /* 9 * 接收端 10 * 1. 使用datagramsocket 指定端口 创建接收端 11 * 2.准备容器 封装成datagrampacket包裹 12 * 3.阻塞式接受包裹receive(); 13 * 4.分析数据 14 * 5.释放数据 15 */ 16 public class udptalkclient implements runnable{ 17 18 private datagramsocket receiver; 19 20 public udptalkclient(int port){ 21 try { 22 this.receiver=new datagramsocket(port); 23 } catch (socketexception e) { 24 // todo auto-generated catch block 25 e.printstacktrace(); 26 } 27 } 28 29 @override 30 public void run() { 31 while(true) 32 { 33 //* 2.准备容器 封装成datagrampacket包裹 34 byte[] container=new byte[1024*60]; 35 datagrampacket packet=new datagrampacket(container, 0,container.length); 36 try { 37 38 //* 3.阻塞式接受包裹receive(); 39 receiver.receive(packet); 40 //* 4.分析数据 41 byte[] datas=packet.getdata(); 42 int len=packet.getlength(); 43 string str=new string(datas,0,len); 44 system.out.println(str); 45 if(str.equals("bye")) 46 { 47 break; 48 } 49 } catch (ioexception e) { 50 // todo auto-generated catch block 51 e.printstacktrace(); 52 } 53 } 54 //* 5.释放数据 55 receiver.close(); 56 } 57 }
1 package cn.lxq.net; 2 /* 3 * 4 */ 5 public class talkteacher { 6 7 public static void main(string[] args) { 8 new thread(new udptalkserver(6666, "localhost", 8888)).start();//发送 9 10 11 new thread(new udptalkclient(5555)).start();//接受 12 } 13 14 }
package cn.lxq.net; public class talkstudent { public static void main(string[] args) { new thread(new udptalkclient(8888)).start();//接收 new thread(new udptalkserver(9999, "localhost", 5555)).start();//发送 } }
上一篇: 面向 Java 开发者的全新 Twitter API
下一篇: 移动端布局