Java實現一個簡單的聊天小程序


服務端代碼:

package com.example.nettypractice.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @description: TCP服務端
 * @author: YangWanYi
 * @create: 2021-11-24 22:16
 **/
public class ServerTcp {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream is = null;
        OutputStream os = null;
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("服務端啟動成功……");
            serverSocket = new ServerSocket(9999);
            accept = serverSocket.accept();
            System.out.println("接收到客戶端的連接請求……");
            String msg;
            os = accept.getOutputStream();
            is = accept.getInputStream();
            byte[] bytes = new byte[1024];
            int len;
            while (true) {
                System.out.println("請輸入:");
                msg = scanner.nextLine();
                os.write(msg.getBytes(StandardCharsets.UTF_8));
                len = is.read(bytes);
                System.out.println("來自客戶端的消息:" + new String(bytes, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                accept.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

客戶端代碼:

package com.example.nettypractice.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @description: TCP客戶端
 * @author: YangWanYi
 * @create: 2021-11-24 22:25
 **/
public class ClientTcp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Socket clientSocket = null;
        InputStream is = null;
        OutputStream os = null;
        try {
            clientSocket = new Socket("localhost", 9999);
            System.out.println("客戶端啟動成功……");
            is = clientSocket.getInputStream();
            os = clientSocket.getOutputStream();
            byte[] bytes = new byte[1024];
            String msg;
            int len;
            while (true) {
                len = is.read(bytes);
                System.out.println("服務端發來消息:" + new String(bytes, 0, len));
                System.out.println("請輸入:");
                msg = scanner.nextLine();
                os.write(msg.getBytes(StandardCharsets.UTF_8));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                is.close();
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM