Java網絡編程實例——簡單模擬在線聊天
1、前提知識
需要知道簡單的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(); } }
試驗:正常開啟客戶端和服務端後:
客戶端發送消息:
服務端接收到信息即成功:
有瞭這個思路,我們隻需要把發送和接收封裝成發送類和接收類,使用多線程即可實現聊天瞭
2、實現思路:
創建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網絡編程實例——簡單模擬在線聊天的詳細內容,更多關於Java 模擬在線聊天的資料請關註WalkonNet其它相關文章!