zoukankan      html  css  js  c++  java
  • 基于多线程的TCP socket通信经典案例

    服务器端

    package com.thinkvenus.study.socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    /**
     * 服务器端线程处理类
     */
    public class ThreadServer extends Thread {
        
        private Socket socket;
        
        public ThreadServer(Socket socket) {
            this.socket = socket;
        }
        
        /**
         * 线程执行的操作,响应客户端请求
         */
        @Override
        public void run() {
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            OutputStream os = null;
            PrintWriter pw = null;
            
            try {
                is = socket.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String msg;
                while((msg = br.readLine()) != null) {
                    System.out.println("这里是服务器端,客户端说:" + msg);
                }
                socket.shutdownInput();//关闭输入
                
                //获取输出流,响应客户端的请求
                os = socket.getOutputStream();
                pw = new PrintWriter(os);
                pw.write("欢迎您");
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                
                //关闭资源
                try {
                    if(pw != null) pw.close();
                    if(os != null) os.close();
                    if(br != null) br.close();
                    if(isr != null) isr.close();
                    if(is != null) is.close();
                    if(socket != null) socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        }
        
    }
    package com.thinkvenus.study.socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * 基于TCP的socket通信
     */
    
    public class Server {
        
        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(8888);
                System.out.println("*****************服务端即将启动,等待客户端的连接**********");
                int count = 0;
                //循环监听客户端的连接
                while(true) {
                    Socket socket = serverSocket.accept();//开始监听
                    ThreadServer ts = new ThreadServer(socket);
                    ts.start();
                    count++;
                    System.out.println("客户端的数量:"+count);
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }

    客户端:

    package com.thinkvenus.study.socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client {
        
        public static void main(String[] args) {
            try {
                Socket socket = new Socket("localhost", 8888);
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
                pw.println("username=aaron; pwd="+Math.random());
                pw.flush();
                
                socket.shutdownOutput();//关闭输出
                //获取输入流,获取服务器端的响应
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String msg;
                while((msg = br.readLine()) != null) {
                    System.out.println("我是客户端,服务端说:"+ msg);
                }
                
                //关闭资源
                br.close();
                isr.close();
                is.close();
                pw.close();
                os.close();
                socket.close();
                
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
  • 相关阅读:
    解决winfrom下TextBox不支持透明背景色
    C# Winform 怎么让按钮在Panel里居中显示
    DevExpress Cpicturebox或者Dev控件 PictureEdit 按比例的缩放加载图片
    Alluxio : 开源分布式内存文件系统
    yarn cluster和yarn client模式区别——yarn-cluster适用于生产环境,结果存HDFS;而yarn-client适用于交互和调试,也就是希望快速地看到application的输出
    Linux 反弹shell(二)反弹shell的本质
    浅谈摄像头有关的安全问题
    pyspark AttributeError: 'NoneType' object has no attribute 'setCallSite'
    大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践
    pyspark minHash LSH 查找相似度
  • 原文地址:https://www.cnblogs.com/ThinkVenus/p/6771595.html
Copyright © 2011-2022 走看看