java使用udp實(shí)現(xiàn)簡(jiǎn)單多人聊天功能
本文實(shí)例為大家分享了udp實(shí)現(xiàn)簡(jiǎn)單的多人聊天功能,供大家參考,具體內(nèi)容如下
多個(gè)客戶(hù)端向服務(wù)器發(fā)送信息,服務(wù)端再將信息返回到各個(gè)客戶(hù)端。
這是接收udp的實(shí)現(xiàn)類(lèi):
public class Rec implements Runnable{ ? ? private DatagramSocket ds; ? ? public Rec(DatagramSocket ds){ ? ? ? ? this.ds = ds; ? ? } ? ? @Override ? ? public void run() { ? ? ? ? while(true){ ? ? ? ? ? ? //接受數(shù)據(jù)和打印數(shù)據(jù) ? ? ? ? ? ? byte[] buf= new byte[1024]; ? ? ? ? ? ? DatagramPacket pac = null; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? pac = new DatagramPacket(buf,buf.length); ? ? ? ? ? ? ? ? ds.receive(pac);//接收數(shù)據(jù) ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? ? ? //獲得消息 ? ? ? ? ? ? String info = new String(pac.getData(),0,pac.getLength()); ? ? ? ? ? ? //獲得ip地址 ? ? ? ? ? ? String ip = pac.getAddress().getHostAddress(); ? ? ? ? ? ? String port = pac.getPort()+""; ? ? ? ? ? ? String name = info.substring(0, info.lastIndexOf(":")); ? ? ? ? ? ? if(!name.equals(Thread.currentThread().getName())){ ? ? ? ? ? ? ? ? System.out.println(info); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
這是發(fā)送udp的實(shí)現(xiàn)類(lèi):
public class Send implements Runnable { ? ? ? ? private DatagramSocket ds;//負(fù)責(zé)發(fā)送和接受數(shù)據(jù) ? ? ? ? private int receport;//準(zhǔn)備發(fā)送的端口號(hào) ? ? ? ? private String ipAddress; //準(zhǔn)備發(fā)送的ip地址 ? ? ? ? public Send(DatagramSocket ds,int receport,String ipAddress){ ? ? ? ? ? ? this.ds = ds; ? ? ? ? ? ? this.ipAddress = ipAddress; ? ? ? ? ? ? this.receport = receport; ? ? ? ? } ? ? ? ? @Override ? ? ? ? public void run() { ? ? ? ? ? ? // 多線(xiàn)程發(fā)送消息 ? ? ? ? ? ? Scanner sc = new Scanner(System.in); ? ? ? ? ? ? InetAddress ip = null; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ?// String ipad = ipAddress.substring(0, ipAddress.lastIndexOf(":"));//ip地址 ? ? ? ? ? ? ? ? ip = InetAddress.getByName(ipAddress); ? ? ? ? ? ? } catch (UnknownHostException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? ? ? while(true){ ? ? ? ? ? ? ? ? String info = sc.nextLine(); ? ? ? ? ? ? ? ? info=Thread.currentThread().getName()+":"+info; ? ? ? ? ? ? ? ? byte[] bs = info.getBytes(); ? ? ? ? ? ? ? ? //把數(shù)據(jù)封裝為數(shù)據(jù)包 ? ? ? ? ? ? ? ? //數(shù)據(jù)包有四個(gè)部分組成 ? ? ? ? ? ? ? ? DatagramPacket pack = new DatagramPacket(bs,bs.length,ip,receport); ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ds.send(pack); ? ? ? ? ? ? ? ? ? ? String content=info.substring(info.lastIndexOf(":")+1); ? ? ? ? ? ? ? ? ? ? if(content.equals("886")){ ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("已退出聊天室"); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? }
服務(wù)端:
我是模擬的本地多用戶(hù),端口號(hào)不能重復(fù)所以設(shè)置了6001-6005,線(xiàn)上只需同一接收udp端口號(hào)即可。
public class ServerCilent { ? ? public static void main(String[] args) throws IOException { ? ? ? ? //接受數(shù)據(jù)包 ? ? ? ? DatagramSocket rec = new DatagramSocket(6262);//接收端口號(hào) ? ? ? ? //建立發(fā)送類(lèi) ? ? ? ? DatagramSocket send = new DatagramSocket(); //發(fā)送數(shù)據(jù) ? ? ? ? List<String> addressList=new ArrayList<>();//存放所有發(fā)送數(shù)據(jù)用戶(hù)的ip ? ? ? ? while (true) { ? ? ? ? ? ? //定義一個(gè)數(shù)據(jù)包接受數(shù)據(jù) ? ? ? ? ? ? byte[] bs = new byte[1024]; ? ? ? ? ? ? DatagramPacket dp = new DatagramPacket(bs, bs.length); ? ? ? ? ? ? //接受數(shù)據(jù) ? ? ? ? ? ? rec.receive(dp); ? ? ? ? ? ? InetAddress address = dp.getAddress(); ? ? ? ? ? ? String s = address.toString(); ? ? ? ? ? ? String ip = s.substring(s.lastIndexOf("/") + 1);//ip ? ? ? ? ? ? //判斷ip是否已存在List中 ? ? ? ? ? ? int index=1; ? ? ? ? ? ? for (String s1 : addressList) { ? ? ? ? ? ? ? ? if(s1.equals(ip)){ ? ? ? ? ? ? ? ? ? ? index=-1; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? if(index>0){ ? ? ? ? ? ? ? ? addressList.add(ip); ? ? ? ? ? ? } ? ? ? ? ? ? //取出數(shù)據(jù) ? ? ? ? ? ? //dp.getData()獲取發(fā)送數(shù)據(jù)的字節(jié)數(shù)組,dp.getLength()獲取發(fā)送內(nèi)容的長(zhǎng)度 ? ? ? ? ? ? String info = new String(dp.getData(), 0, dp.getLength()); ? ? ? ? ? ? System.out.println("客戶(hù)端發(fā)送來(lái)的信息:" + info); ? ? ? ? ? ? //--------------------服務(wù)器轉(zhuǎn)發(fā)--------------------------// ? ? ? ? ? ? String content=info.substring(info.lastIndexOf(":")+1); ? ? ? ? ? ? if (content.equals("886")) { ? ? ? ? ? ? ? ? String name=info.substring(0,info.lastIndexOf(":")); ? ? ? ? ? ? ? ? info=name+":退出了聊天室"; ? ? ? ? ? ? } ? ? ? ? ? ? //建立數(shù)據(jù)包 并轉(zhuǎn)發(fā)信息 ? ? ? ? ? ? byte[] bs1 = info.getBytes(); ? ? ? ? ? ? for (String s1 : addressList) { ? ? ? ? ? ? ? ? for (int i=6001;i<=6005;i++){ ? ? ? ? ? ? ? ? ? ? DatagramPacket dp1 = new DatagramPacket(bs1, bs1.length, InetAddress.getByName(s1), i); ? ? ? ? ? ? ? ? ? ? send.send(dp1); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } }
客戶(hù)端測(cè)試:
public class Test1 { ? ? public static void main(String[] args) throws SocketException { ? ? ? ? Send s=new Send(new DatagramSocket(),6262,"127.0.0.1"); ? ? ? ? Rec rec=new Rec(new DatagramSocket(6001)); ? ? ? ? new Thread(s,"宸").start(); ? ? ? ? new Thread(rec,"宸").start(); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java使用UDP實(shí)現(xiàn)多人聊天功能
- java實(shí)現(xiàn)基于UDP協(xié)議的聊天小程序操作
- Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)消息發(fā)送及聊天
- Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)多線(xiàn)程在線(xiàn)聊天
- java UDP實(shí)現(xiàn)一個(gè)聊天工具的示例代碼
- java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析
- Java基于UDP協(xié)議實(shí)現(xiàn)簡(jiǎn)單的聊天室程序
- java中UDP簡(jiǎn)單聊天程序?qū)嵗a
- 使用Java和WebSocket實(shí)現(xiàn)網(wǎng)頁(yè)聊天室實(shí)例代碼
- java聊天室的實(shí)現(xiàn)代碼
相關(guān)文章
2022版IDEA創(chuàng)建一個(gè)maven項(xiàng)目的超詳細(xì)圖文教程
IDEA是用于java語(yǔ)言開(kāi)發(fā)的集成環(huán)境,并且經(jīng)常用于maven、spring、MyBatis等項(xiàng)目的開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于2022版IDEA創(chuàng)建一個(gè)maven項(xiàng)目的超詳細(xì)圖文教程,需要的朋友可以參考下2023-02-02淺談StringEntity 和 UrlEncodedFormEntity之間的區(qū)別
這篇文章主要介紹了StringEntity 和 UrlEncodedFormEntity之間的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容
這篇文章主要介紹了如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03實(shí)例講解Java批量插入、更新數(shù)據(jù)
這片文章介紹了一個(gè)Java批量添加數(shù)據(jù),多個(gè)字段同時(shí)添加多條數(shù)據(jù)具體實(shí)例,面向的是Oracle數(shù)據(jù)庫(kù),需要的朋友可以參考下2015-07-07