温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-14 06:26:16 来源:网络 阅读:1667 作者:恋上程序员 栏目:编程语言

开始...
1、服务器端:

public class ServerSocketApp {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            System.out.println("服务已启动");
            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("请输入:");
        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、运行
服务器
cd com/kyy/bases/socket 运行javac ServerSocketApp.java
cd classpath根目录 运行java ServerSocketApp
服务器端启动
客户端
cd com/kyy/bases/socket 运行javac ClientSocketApp.java
cd classpath根目录 运行java ClientSocketApp

遇到的问题:
如果不通过eclipse、Idea等工具,会出现 “找不到或无法加载主类 com.kyy.bases.socket.ClientSocketApp”的异常
解决办法,设置classpath=.;"JAVA_HOME"/lib
或者在cmd窗口临时设置set classpath=.;

...结束

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI