Java基於UDP通訊聊天室程式碼
阿新 • • 發佈:2019-01-04
UDP就是將目的地和資料封裝在一個包中,然後傳送給相應的目的地,包的大小最大為64K,安全性低,效率高
在建立之前我們餘姚知道一些類和方法的用法
比如:InetAdress類
//得到名為zhang的主機物件 InetAddress iAddress=InetAddress.getByName("zhang"); //得到該物件的ip地址 String hostIp=iAddress.getHostAddress(); //獲得本地主機物件 InetAddress iAddress2=InetAddress.getLocalHost(); //得到該物件的ip地址 String hostip1=iAddress2.getHostAddress(); //得到該主機的名字 String hostName=iAddress2.getHostName(); //得到名為zhang的計算機的所有ipv4和ipv6連結 InetAddress[] iAddress3=InetAddress.getAllByName("zhang"); for (int i = 0; i < iAddress3.length; i++) { System.out.println(iAddress3[i].getHostAddress()); }
DatagramSocket ds=new DatagramSocket();
DatagramPacket dp=new DatagramPacket();
UDP發包
package net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.util.Scanner; public class UDPClient implements Runnable { //目的埠 private int port; //目的ip private String ip; //初始化ip和埠 public UDPClient(String ip, int port) { super(); this.ip = ip; this.port = port; } @Override public void run() { UDPClient uClient=new UDPClient(ip, port); uClient.UDPClient(); } public void UDPClient() { //定義socket物件,實現通訊 DatagramSocket dSocket = null; //高效讀 BufferedReader bReader = null; try { //得到目的ip的主機物件 InetAddress iAddress1=InetAddress.getByName(ip); System.out.println("已接入"+iAddress1.getHostName()); while (true) { //從控制檯讀取 bReader = new BufferedReader(new InputStreamReader(System.in)); //讀取一行資料 String string = bReader.readLine(); //定義socket物件,實現通訊 dSocket = new DatagramSocket(); //把讀到的字串轉碼為utf-8字元陣列 byte[] by = string.getBytes("utf-8");//兩個客戶端務必編碼一致,不然會亂碼 InetAddress iAddress = InetAddress.getByName(ip); //建立資料包 DatagramPacket dPacket = new DatagramPacket(by, by.length, iAddress, port); //傳送資料包 dSocket.send(dPacket); System.out.println("你對" + iAddress.getHostName() + "說:" + string); } } catch (Exception e) { e.printStackTrace(); } finally { if (dSocket != null) { dSocket.close(); } } } }
UDP收包
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPServer implements Runnable { //接受埠 private int port; //構造器,初始化埠 public UDPServer(int port) { super(); this.port = port; } @Override public void run() { UDPServer uServer=new UDPServer(port); uServer.udpServer(); } public void udpServer() { //設定socket通訊 DatagramSocket dSocket = null; try { dSocket = new DatagramSocket(port); //接受的位元組大小 byte[] by = new byte[1024]; //無限監聽 while (true) { //定義一個數據包物件 DatagramPacket dPacket = new DatagramPacket(by, by.length); //接受資料包 dSocket.receive(dPacket); //獲得資料包中的資料 byte by1[] = dPacket.getData(); //獲得資料包中資料的長度 int len = dPacket.getLength(); //以字串的形式輸出 System.out.println(dPacket.getAddress() + "對你說:" + new String(by1, 0, len)); } } catch (Exception e) { e.printStackTrace(); } finally { dSocket.close(); } } }
這時,我們需要再定義一個聊天室物件
CharRom
一個客戶端
package net;
public class ChatRom implements Runnable {
@Override
public void run() {
//一個客戶端
//發到10000埠,10001埠接受
Thread thread=new Thread(new UDPClient("192.168.1.44", 10000));
Thread thread1=new Thread(new UDPServer(10001));
thread.start();
thread1.start();
}
}
另一個客戶端
package net;
public class ChatRom implements Runnable {
@Override
public void run() {
//另一個客戶端
//發到10001埠,10000埠接受
Thread thread=new Thread(new UDPClient("192.168.1.11", 10001));
Thread thread1=new Thread(new UDPServer(10000));
thread.start();
thread1.start();
}
}
然後分別在兩個客戶端執行
package net;
public class Chatdemo {
public static void main(String[] args) {
ChatRom chatRom=new ChatRom();
Thread thread=new Thread(chatRom);
thread.start();
}
}
好了,聊天室製作完成
總結:
根據UDP特點,傳送端和接收端需要執行緒同時執行,且使用的埠不一樣,注意發包的時候和接包的時候編碼一致,不然會亂碼