java UDP实现一个聊天工具的示例代码
程序员文章站
2022-06-01 17:42:32
题目:假设tom和jerry利用java udp进行聊天,请为他们编写程序。具体如下:(1)、tom和jerry聊天的双方都应该具有发送端和接收端;(2)、利用datagramsocket与datag...
题目:
假设tom和jerry利用java udp进行聊天,请为他们编写程序。具体如下:
(1)、tom和jerry聊天的双方都应该具有发送端和接收端;
(2)、利用datagramsocket与datagrampacket;
(3)、实现 java.lang.runnable类,重写 run()方法。
学过计网的都知道,利用三元组(ip地址,协议,端口)就可以标识网络的进程了。
而进行通信我们直接运用socket就可以了,首先需要知道是socket是什么,这部分可以参考这个大佬写的博客。
实现一个客户端,那么需要tom和jerry都可以实现接收和发送功能,分别用两个线程启动。
接收线程receive_thread.java
package chat_udp; import java.io.ioexception; import java.util.date; import java.net.datagrampacket; import java.net.datagramsocket; import java.text.simpledateformat; public class receive_thread extends thread { private static final int max_receive_buffer = 1024; private datagramsocket server; private datagrampacket packet; byte[] buffer = new byte[max_receive_buffer]; public receive_thread(datagramsocket server) { this.server = server; packet = new datagrampacket(buffer, buffer.length); } @override public void run() { try { while(true) { //接收数据包 server.receive(packet); string s = new string(packet.getdata(),packet.getoffset(),packet.getlength(),"utf-8"); date day=new date(); simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); if(packet.getport() == 10001) system.out.println("tom"+packet.getaddress()+" 说:"+s+"\t"+df.format(day)); else{ system.out.println("jerry"+packet.getaddress()+" 说 :"+s+"\t"+df.format(day)); } packet.setlength(buffer.length); } } catch(ioexception e) { system.out.println("ioexception"); } } }
发送线程send_thread.java
package chat_udp; import java.io.ioexception; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.inetaddress; import java.net.inetsocketaddress; import java.util.scanner; public class send_thread extends thread{ //发送的socket端 private datagramsocket sender = null; //待发送的目标地址 private inetsocketaddress address = null; //从键盘输入 scanner scan = new scanner(system.in); public send_thread(datagramsocket sender,inetsocketaddress address) { this.sender = sender; this.address = address; } @override public void run() { // todo auto-generated method stub try { while(true) { //输入待发送的内容 string input = scan.nextline(); if(input.equals("exit")) break; byte[] data = null; data = input.getbytes("utf-8"); //创建udp数据报 datagrampacket pack = new datagrampacket(data, data.length,address); sender.send(pack); } system.out.println("exit!"); }catch(ioexception e) { system.out.println("ioexception"); } } }
聊天服务器
chat_server.java
package chat_udp; //杨丽冰 201831064402 import java.net.datagramsocket; import java.net.inetsocketaddress; public class chat_server { private static final int dest_port = 8888; private static final int send_port = 10001; private static final int rece_port = 9000; private static final string ip = "127.0.0.1"; public static void main(string[] args) { try{ send_thread send_thread = null; receive_thread rece_thread = null; inetsocketaddress address = null; //创建待接受数据包的目的机的端口号和ip地址 address = new inetsocketaddress(ip, dest_port); //创建发送的socket端 datagramsocket sendsocket = new datagramsocket(send_port); //创建接受的socket端 datagramsocket recesocket = new datagramsocket(rece_port); //发送线程建立 send_thread = new send_thread(sendsocket, address); //接受线程的建立 rece_thread = new receive_thread(recesocket); send_thread.start(); rece_thread.start(); }catch(exception e) { system.out.println("exception!"); } } }
聊天客户端 chat_client.java
package chat_udp; //杨丽冰 201831064402 import java.net.datagramsocket; import java.net.inetsocketaddress; public class chat_client { //声明端口号 private static final int dest_port = 9000; private static final int send_port = 10000; private static final int rece_port = 8888; private static final string ip = "127.0.0.1"; public static void main(string[] args) { try{ send_thread send_thread = null; receive_thread rece_thread = null; inetsocketaddress address = null; //创建待接受数据包的目的机的端口号和ip地址 address = new inetsocketaddress(ip, dest_port); //创建发送的socket端 datagramsocket sendsocket = new datagramsocket(send_port); //创建接受的socket端 datagramsocket recesocket = new datagramsocket(rece_port); //发送线程建立 send_thread = new send_thread(sendsocket, address); //接受线程的建立 rece_thread = new receive_thread(recesocket); send_thread.start(); rece_thread.start(); }catch(exception e) { system.out.println("exception!"); } } }
需要注意的时候运行的时候需要同时运行聊天服务器和客户端,才能够正常运行。
最终效果如下:
到此这篇关于java udp实现一个聊天工具的文章就介绍到这了,更多相关java udp聊天工具内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 4、XML约束--Schema约束
下一篇: Sublime Text