zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }
  • 相关阅读:
    VOA 2009/11/02 DEVELOPMENT REPORT In Kenya, a Better Life Through Mobile Money
    2009.11.26教育报道在美留学生数量创历史新高
    Java中如何实现Tree的数据结构算法
    The Python Tutorial
    VOA HEALTH REPORT Debate Over New Guidelines for Breast Cancer Screening
    VOA ECONOMICS REPORT Nearly Half of US Jobs Now Held by Women
    VOA ECONOMICS REPORT Junior Achievement Marks 90 Years of Business Education
    VOA 2009/11/07 IN THE NEWS A Second Term for Karzai; US Jobless Rate at 10.2%
    Ant入门
    Python 与系统管理
  • 原文地址:https://www.cnblogs.com/ywy8/p/15603011.html
Copyright © 2011-2022 走看看