您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Java中利用網絡編程模擬在線聊天,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
需要知道簡單的IO流操作,以及簡單的UDP發送數據包的原理。
需要用到的類:DatagramSocket、DatagramPacket
UDP數據包基于DatagramSocket發送和接收,DatagramPacket用于封裝數據包
看下案例:
客戶端發送消息:
正常情況下從控制臺讀信息,封裝到DatagramPacket之中,再由DatagramSocket的send方法發出
讀取到bye的時候退出聊天
public class UdpOnlineClient { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(10000); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { String data = bufferedReader.readLine(); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 9999)); socket.send(packet); if (data.equals("bye")){ break; } } socket.close(); } }
服務端接收消息:
DatagramSocket的receive方法接收客戶端發送的消息,打印到控制臺,如果信息是bye則退出聊天
public class UdpOnlineServer { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(9999); while (true){ byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); String msg = new String(packet.getData(), 0, packet.getLength()); System.out.println(msg); if(msg.equals("bye")){ break; } } socket.close(); } }
試驗:正常開啟客戶端和服務端后:
客戶端發送消息:
服務端接收到信息即成功:
有了這個思路,我們只需要把發送和接收封裝成發送類和接收類,使用多線程即可實現聊天了
創建UDP發送實現類TaskSend、UDP接收實現類TaskReceive,以及模擬多線程通信的通信類ChatThread1、ChatThread2
UDP發送實現類TaskSend:
public class TaskSend implements Runnable{ private String sendIP; //自己的服務端口 private int ownPort; private int sendPort; //讀取控制臺輸入流 private BufferedReader bufferedReader; private DatagramSocket socket; public TaskSend(String sendIP, int ownPort, int sendPort) { this.sendIP = sendIP; this.sendPort = sendPort; try { socket = new DatagramSocket(ownPort); bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } catch (SocketException e) { e.printStackTrace(); } finally { } } //發送消息 public void run() { while (true) { try { String data = bufferedReader.readLine(); //打印自己的消息 System.out.println("我:"+data); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress(sendIP,sendPort)); socket.send(packet); if (data.equals("bye")) { break; } } catch (IOException e) { e.printStackTrace(); } finally { } } socket.close(); } }
UDP接收實現類TaskReceive:
public class TaskReceive implements Runnable{ private int ownPort; //發送信息的人 private String msgOwner; private DatagramSocket socket; public TaskReceive(int ownPort, String msgOwner) { this.msgOwner = msgOwner; try { socket = new DatagramSocket(ownPort); } catch (SocketException e) { e.printStackTrace(); } finally { } } //接收消息 public void run() { while (true) { try { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); String msg = new String(packet.getData(), 0, packet.getLength()); System.out.println(msgOwner+":"+msg); if (msg.equals("bye")) { break; } } catch (IOException e) { e.printStackTrace(); } finally { } } socket.close(); } }
之后使用兩個通信類開啟多線程模擬一下:
模擬老師
public class ChatThread1 { public static void main(String[] args) { new Thread(new TaskSend("localhost",9999,10000)).start(); new Thread(new TaskReceive(9998,"老師")).start(); } }
模擬學生
public class ChatThread2 { public static void main(String[] args) { new Thread(new TaskReceive(9999,"學生")).start(); new Thread(new TaskSend("localhost",9998,10001)).start(); } }
實例:
學生
老師
上述內容就是怎么在Java中利用網絡編程模擬在線聊天,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。