• Java---利用程序实现在控制台聊天


    一.普通版(不能实现随意输入)

    电脑A(服务器端)

    package day;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    public class Mysever {
        public static void main(String[] args) {
            //服务器端 IP 172.16.242.163  PORT: 5566
            //Socket 插头
            //ServerSocket 插座
            try {
                ServerSocket ss = new ServerSocket(5566);
                Socket server = ss.accept();
                Scanner sca = new Scanner(System.in);
                //服务器发信息
                while (true) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String time = sdf.format(new Date());
                    System.out.println("请client发言:");
                    String msg = sca.nextLine();
                    String info = String.format("Server[%s]:%s
    ", time, msg);
                    server.getOutputStream().write(info.getBytes());
                 //接受信息
                    byte[] buf = new byte[2012];
                    server.getInputStream().read(buf);
                    String read = new String(buf);
                    System.out.println(read);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    电脑B(客户端)

    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    public class Mynet {
        public static void main(String[] args) {
            try{
                Scanner san = new Scanner(System.in);
                Socket client = new Socket("172.16.242.163",5566);
    
                while(true) {
                    byte[] buf = new byte[2048];
                    client.getInputStream().read(buf);
                    System.out.println(new String(buf));//收信息
    
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String time = sdf.format(new Date());
                    System.out.print("给service回话:");
                    String msg = san.nextLine();
                    String info = String.format("Client[%s]:%s
    ", time, msg);
                    client.getOutputStream().write(info.getBytes());//发信息
                }
    
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    
    }

    二.升级版(可以随意聊天)利用多线程

    服务器端

    package day;
    
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    public class MySend implements Runnable {
        private OutputStream os;
    
        public MySend(OutputStream os) {
            this.os = os;
        }
    
        @Override
        public void run() {
            while(true){
                Scanner sc = new Scanner(System.in);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());
                System.out.print("给用户发言:");
                String msg = sc.nextLine();
                String info = String.format("Server[%s]:%s
    ", time, msg);
                try {
                    this.os.write(info.getBytes());
                    this.os.flush();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    
    -------------------------------------------------------------------------------------------------
    
    package day;
    
    import java.io.InputStream;
    
    public class MyRead implements Runnable {
        private InputStream is;
        public MyRead(InputStream is){
            this.is = is;
        }
        @Override
        public void run() {
            while(true){
                //接收信息
                byte[] buf = new byte[2014];
                try {
                    this.is.read(buf);
                    System.out.println(new String(buf));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    --------------------------------------------------------------------------------------------------
    package day; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; //service 服务器端 public class Servertext { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(5566); Socket server = ss.accept(); new Thread(new MySend(server.getOutputStream())).start(); new Thread(new MyRead(server.getInputStream())).start(); } catch (IOException e) { e.printStackTrace(); } } } //多个用户怎么办 /* list<Socket> sss = new ArrayList<Socket>(); while(true){ Socket server = ss.accept(); sss.add(server); } */

    用户端

    package day;
    
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    public class MySend implements Runnable {
        private OutputStream os;
    
        public MySend(OutputStream os) {
            this.os = os;
        }
    
        @Override
        public void run() {
            while(true){
                Scanner sc = new Scanner(System.in);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());
                System.out.print("给服务器发言:");
                String msg = sc.nextLine();
                String info = String.format("Client[%s]:%s
    ", time, msg);
                try {
                    this.os.write(info.getBytes());
                    this.os.flush();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    
    -------------------------------------------------------------------------------------------------
    
    package day;
    
    import java.io.InputStream;
    
    public class MyRead implements Runnable {
        private InputStream is;
        public MyRead(InputStream is){
            this.is = is;
        }
        @Override
        public void run() {
            while(true){
                //接收信息
                byte[] buf = new byte[2014];
                try {
                    this.is.read(buf);
                    System.out.println(new String(buf));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    --------------------------------------------------------------------------------------------------
    package day;

    import java.net.Socket;
    //client 客户端
    public class Text {
    public static void main(String[] args) {
    try{
    Socket client = new Socket("172.16.242.163",5566);
    new Thread(new MySend(client.getOutputStream())).start();
    new Thread(new MyRead(client.getInputStream())).start();
    }catch (Exception e){
    e.printStackTrace();
    }
    }
    }
  • 相关阅读:
    C#设计一个简单的计算器,实现两个数的加,减,乘,除,求幂等计算,运行效果如下图所示:
    如何用C#中的窗体设计一款只含有加减乘除,求平方的简易版计算器?
    番外篇科普为什么1024是程序员日?2020年10月24日,程序员为啥都不放假?
    VS2019基础,C#编辑窗体时,工具箱如何调用,工具箱挡住窗体怎么办,如何改窗体名字
    C++用函数重载实现两个整数和三个浮点数的排序,按照从小到大的顺序将排序结果输出
    C++用引用形参实现两个变量的值互换
    C++用带有默认参数的函数实现,求2个或3个正整数中的最大数
    linux服务器磁盘速度测试
    CSS HACK收集
    分享一个web页面背景全屏的jquery插件Fullscreen Background
  • 原文地址:https://www.cnblogs.com/zxwen/p/9673602.html
Copyright © 2020-2023  润新知