zoukankan      html  css  js  c++  java
  • JAVA 无图形界面 双线程聊天程序

    写在前面

    之前的单线程版本没法自由输入和输出;用了多线程之后体验好了

    代码

    客户端

    import java.io.*;
    import java.net.*;
    
    class CL {
        Socket cl;
        public CL(String ip) throws IOException {
            this.cl=new Socket(ip,9999);
            System.out.println("OK");
            BufferedReader in= new BufferedReader(new InputStreamReader(cl.getInputStream()));
            BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
            PrintStream ot=new PrintStream(cl.getOutputStream(),true);
            ot.println("connected");
    
            Thread readThread=new Thread(()-> {while (true) {
                System.out.print("Server:");
                String sv = null;
                try {
                    sv = in.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(sv);}});
    
            Thread wtThread=new Thread(()-> {while (true) {
                System.out.println("Message:");
                String msg = null;
                try {
                    msg = rd.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ot.println(msg);
            }});
            readThread.start();
            wtThread.start();
        }
        public static void main(String[] args) throws IOException {
            System.out.println("enter your dest-ip:");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String ip= br.readLine();
            CL CL=new CL(ip);
        }
    }
    

    服务端

    import java.io.*;
    import java.net.*;
    class SERV {
        Socket cl;
        ServerSocket ser;
    
         SERV() throws IOException {
            this.ser = new ServerSocket(9999);
            InetAddress inetAddr = InetAddress.getLocalHost();
            System.out.println("Connecting,LocalIP:"+ inetAddr.getHostAddress());
            this.cl = ser.accept();
            //System.out.println("Connected");
            PrintStream ot=new PrintStream(cl.getOutputStream());
            BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
            BufferedReader in= new BufferedReader(new InputStreamReader(cl.getInputStream()));
            ot.println("plz");
    
            Thread readThread=new Thread(()-> {while (true) {
                System.out.print("Client:");
                String sv = null;
                try {
                    sv = in.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(sv);}});
    
            Thread wtThread=new Thread(()-> {while (true) {
                System.out.println("Message:");
                String msg = null;
                try {
                    msg = rd.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ot.println(msg);
            }});
            readThread.start();
            wtThread.start();
    
        }
        public static void main(String[] args) throws IOException {
            SERV SERV=new SERV();
    
        }
    }
    
  • 相关阅读:
    TensorFlow进阶(三)---变量的创建、初始化
    TensorFlow进阶(二)---张量的操作
    TensorFlow进阶(一)----张量的阶和数据类型
    TensorFlow入门
    卷积神经网络识别手写数字实例
    解决在win系统下使用DOS命令开启TensorBoard的问题及方法步骤
    TensorFlow------单层(全连接层)实现手写数字识别训练及测试实例
    TensorFlow------TFRecords的读取实例
    TensorFlow------TFRecords的分析与存储实例
    TensorFlow------读取二进制文件实例
  • 原文地址:https://www.cnblogs.com/impw/p/15473612.html
Copyright © 2011-2022 走看看