zoukankan      html  css  js  c++  java
  • 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();
    }
    }
    }
  • 相关阅读:
    算法导论--平摊分析之聚集分析
    编译器开发系列--Ocelot语言3.类型名称的消解
    编译器开发系列--Ocelot语言2.变量引用的消解
    编译器开发系列--Ocelot语言1.抽象语法树
    算法导论--散列表的数学分析(精解)链表法
    Linux2.6内核协议栈系列--TCP协议2.接收
    日常‘说说’(回归 原森雨)
    那些玩枪战我特别想听到的声音!
    友链!
    晚安背后的秘密
  • 原文地址:https://www.cnblogs.com/zxwen/p/9673602.html
Copyright © 2011-2022 走看看