• java_一对一自由聊天


    客户端

    package cn.qianfeng.kaoti03;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    
    class ReceiveSocket implements Runnable {
        private Socket s;
    
        public ReceiveSocket(Socket s) {
            super();
            this.s = s;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            BufferedReader br = null;
            try {
                while (true) {
    
                    br = new BufferedReader(new InputStreamReader(
                            s.getInputStream()));
                    String line = br.readLine();
                    if (line != null) {
                        System.out.println(line);
                    }
    
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
    
    class SendSocket implements Runnable {
        private Socket s;
    
        public SendSocket(Socket s) {
            super();
            this.s = s;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            BufferedReader br = null;
            try {
                while (true) {
    
                    br = new BufferedReader(new InputStreamReader(System.in));
                    PrintStream ps = new PrintStream(s.getOutputStream(), true);
                    String line = br.readLine();
                    ps.println("A:" + line);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            }
    
        }
    }
    
    public class TestSocket {
        public static void main(String[] args) {
            try {
                Socket s = new Socket("localhost", 8888);
                new Thread(new SendSocket(s)).start();
                new Thread(new ReceiveSocket(s)).start();
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    服务端

    package cn.qianfeng.kaoti03;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    class SendServerSocket implements Runnable {
        private Socket s;
    
        public SendServerSocket(Socket s) {
            super();
            this.s = s;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            BufferedReader br = null;
            PrintWriter ps = null;
            try {
                while (true) {
    
                    br = new BufferedReader(new InputStreamReader(System.in));
                    ps = new PrintWriter(s.getOutputStream(), true);
                    String line = br.readLine();
                    if (line != null) {
                        ps.println("B:" + line);
                    }
    
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            }
    
        }
    }
    
    class ReceiveServerSocket implements Runnable {
        private Socket s;
    
        public ReceiveServerSocket(Socket s) {
            super();
            this.s = s;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            BufferedReader br = null;
            try {
                while (true) {
    
                    br = new BufferedReader(new InputStreamReader(
                            s.getInputStream()));
    
                    String line = br.readLine();
                    if (line != null) {
                        System.out.println(line);
    
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            }
    
        }
    
    }
    
    public class TestServerSocket {
        public static void main(String[] args) {
            try {
                ServerSocket ss = new ServerSocket(8888);
                Socket s = ss.accept();
                System.out.println(s.getInetAddress().getHostName()
                        + ".....connected");
    
                new Thread(new SendServerSocket(s)).start();
                new Thread(new ReceiveServerSocket(s)).start();
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    如何辨别护照的种类
    C#枚举中使用Flags特性
    那些年,我们一起学WCF--(7)PerSession实例行为
    64位系统使用Access 数据库文件的彻底解决方法
    从Excel中导入数据时,提示“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序”的解决办法
    TortoiseSVN与VisualSVN Server搭建SVN版本控制系统
    解决Winform程序在不同分辨率系统下界面混乱
    【已解决】Https请求——基础连接已经关闭 发送时发生错误
    Entity Framework Code First学习系列目录
    PowerDesigner之PDM(物理概念模型)各种属性建立如PK,AK等
  • 原文地址:https://www.cnblogs.com/fangg/p/5562750.html
Copyright © 2020-2023  润新知