溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中使用ServerSocket和Socket的多線程示例

發(fā)布時(shí)間:2020-07-14 06:26:16 來源:網(wǎng)絡(luò) 閱讀:1668 作者:戀上程序員 欄目:編程語言

開始...
1、服務(wù)器端:

public class ServerSocketApp {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            System.out.println("服務(wù)已啟動(dòng)");
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("客戶" + socket.getInetAddress().getHostAddress() + "來訪,");
                new Thread(() -> {
                    try {
                        InputStream inputStream = socket.getInputStream();
                        PrintStream printStream = new PrintStream(socket.getOutputStream());
                        Scanner in = new Scanner(inputStream);
                        in.useDelimiter("\n");
                        while (in.hasNext()) {
                            String message = in.next().trim();
                            if (message.equalsIgnoreCase("bye")) {
                                String bye = new String("客戶端已退出,拜拜".getBytes("UTF-8"));
                                System.out.println(bye);
                                printStream.println(bye);
                            } else {
                                printStream.println("ECHO>" + message);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2、客戶端程序:

public class ClientSocketApp {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 9999);
        Scanner in = new Scanner(System.in);
        Scanner inputStream = new Scanner(socket.getInputStream());
        PrintStream printStream = new PrintStream(socket.getOutputStream());
        in.useDelimiter("\n");
        inputStream.useDelimiter("\n");
        boolean b = true;
        System.out.println("請(qǐng)輸入:");
        while (b) {
            if (in.hasNext()) {
                String message = in.next().trim();
                printStream.println(message);
                if (inputStream.hasNext()) {
                    String serverMessage = new String(inputStream.next().trim().getBytes("UTF-8"));
                    System.out.println(serverMessage);
                }
                if (message.equalsIgnoreCase("bye")) {
                    b = false;
                }
            }
        }
        socket.close();
    }
}

3、運(yùn)行
服務(wù)器
cd com/kyy/bases/socket 運(yùn)行javac ServerSocketApp.java
cd classpath根目錄 運(yùn)行java ServerSocketApp
服務(wù)器端啟動(dòng)
客戶端
cd com/kyy/bases/socket 運(yùn)行javac ClientSocketApp.java
cd classpath根目錄 運(yùn)行java ClientSocketApp

遇到的問題:
如果不通過eclipse、Idea等工具,會(huì)出現(xiàn) “找不到或無法加載主類 com.kyy.bases.socket.ClientSocketApp”的異常
解決辦法,設(shè)置classpath=.;"JAVA_HOME"/lib
或者在cmd窗口臨時(shí)設(shè)置set classpath=.;

...結(jié)束

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI