您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在java中利用UDP實現一個聊天工具,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
題目:
假設Tom和Jerry利用Java UDP進行聊天,請為他們編寫程序。具體如下:
(1)、Tom和Jerry聊天的雙方都應該具有發送端和接收端;
(2)、利用DatagramSocket與DatagramPacket;
(3)、實現 java.lang.Runnable類,重寫 run()方法。
學過計網的都知道,利用三元組(ip地址,協議,端口)就可以標識網絡的進程了。
而進行通信我們直接運用socket就可以了,首先需要知道是socket是什么,這部分可以參考這個大佬寫的博客。
https://www.jb51.net/article/121601.htm
實現一個客戶端,那么需要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實現一個聊天工具就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。