Java用UDP實現簡單聊天

碼農天宇發表於2020-12-06

在這裡插入圖片描述
伺服器程式碼

import network.day1.Server;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * @ClassName UdpServer
 * @Description TODO
 * @Author 碼農天宇
 * @Date 2020/12/6 16:28
 * @Version 1.0
 */

public class UdpTalkServer {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        //建立伺服器物件 並指定埠
        DatagramSocket server = new DatagramSocket(9876);
        while (true){
            //建立資料包,接受資料
            byte[] recv = new byte[1024];
            DatagramPacket packetRecv = new DatagramPacket(recv,0, recv.length);
            //接收訊息
            server.receive(packetRecv);
            //讀取資料
            String recvDate = new String(recv,0, packetRecv.getLength());
            System.out.println("客戶端: " + recvDate);

            //獲取客戶端ip
            InetAddress address = packetRecv.getAddress();
            //獲取客戶端埠
            int port = packetRecv.getPort();
            //建立資料包
            System.out.print("伺服器: ");
            byte[] send = sc.next().getBytes();
            DatagramPacket sendDate = new DatagramPacket(send,send.length,address,port);
            //傳送資料給客戶端
            server.send(sendDate);
            //關閉資源
            //server.close();
        }
    }
}

客戶端程式碼

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @ClassName UdpClient
 * @Description TODO
 * @Author 碼農天宇
 * @Date 2020/12/6 16:28
 * @Version 1.0
 */

public class UdpTalkClient {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        //建立客戶端
        DatagramSocket client = new DatagramSocket();
        //定義客戶端地址,埠號
        InetAddress address = InetAddress.getByName("localhost");
        int port = 9876;
        while (true){
           //建立資料
           System.out.print("客戶端: ");
           byte[] send = sc.next().getBytes();
           DatagramPacket packet = new DatagramPacket(send, send.length,address,port);
           //傳送資料
           client.send(packet);

           //建立資料包,用於接受資料
           byte[] recv = new byte[1024];
           DatagramPacket packetRecv = new DatagramPacket(recv,0,recv.length);
           //接受資料
           client.receive(packetRecv);
           //讀取並列印資料
           String recvDate = new String(recv,0,recv.length);
           System.out.println("伺服器: " + recvDate);
            //關閉
            //client.close();
        }
    }
}

server
客戶端: 你好,伺服器
伺服器: 你好,客戶端
客戶端: 一個i澳網i李剛i熬過i奧
伺服器: 嗷嗷嗷

client
客戶端: 你好,伺服器
伺服器: 你好,客戶端                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
客戶端: 一個i澳網i李剛i熬過i奧
伺服器: 嗷嗷嗷                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
客戶端: 

相關文章