• tcp转发


    Proxy.java

    package com.dc.tcp.proxy;

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class Proxy {
     
     public static void main(String[] args) {
      try {
       ServerSocket server = new ServerSocket(8081);
       while (true) {
        Socket socket = server.accept();
        Pool.getInstance().getExecutor().execute(new ProxyWorker(socket));
       }
      } catch (IOException e) {
       e.printStackTrace();
      }
      
     }

    }

    ProxyWorker.java

    package com.dc.tcp.proxy;

    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class ProxyWorker implements Runnable {

        private Socket server = null;
        private Socket client = null;

        public ProxyWorker(Socket client) {
            try {
                server = new Socket("10.126.3.163", 3306);
                this.client = client;
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            Pool.getInstance().getExecutor().execute(new ProxySender(client, server));
            Pool.getInstance().getExecutor().execute(new ProxyReceiver(client, server));
        }
    }

    ProxySender.java

    package com.dc.tcp.proxy;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class ProxySender implements Runnable {
        
        private Socket client = null;
        private Socket server = null;
        private Logger log = LoggerFactory.getLogger(ProxySender.class);
        
        public ProxySender(Socket client, Socket server) {
            super();
            this.server = server;
            this.client = client;
        }
    
        @Override
        public void run() {
            try {
                InputStream is = server.getInputStream();
                OutputStream os = client.getOutputStream();
    //            log.info("send:");
                while (true) {
                    int flag = 0;
                    if ((flag = is.read()) == -1) {
                        break;
                    } else {
                        int count = is.available();
                        if (count > 0) {
                            byte[] b = new byte[count];
                            is.read(b);
                            byte[] temp = new byte[count + 1];
                            temp[0] = (byte) flag;
                            for (int i = 0; i < b.length; i++) {
                                temp[i + 1] = b[i];
                            }
    //                        log.info(new String(temp));
                            String tempStr = "";
                            for (int i = 0; i < temp.length; i++) {
                                tempStr += " " + temp[i];
                            }
    //                        log.info(tempStr);
                            os.write(temp);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                if (client != null) {
                    try {
                        client.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (server != null) {
                    try {
                        server.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                log.info("sender close socket");
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (server != null) {
                try {
                    server.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            log.info("sender close socket");
        }
    
    }

    ProxyReceiver.java

    package com.dc.tcp.proxy;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class ProxyReceiver implements Runnable {
        
        private Socket server = null;
        private Socket client = null;
        private Logger log = LoggerFactory.getLogger(ProxyReceiver.class);
        
        public ProxyReceiver(Socket client, Socket server) {
            super();
            this.server = server;
            this.client = client;
        }
    
        @Override
        public void run() {
            try {
                InputStream is = client.getInputStream();
                OutputStream os = server.getOutputStream();
                log.info("receive:");
                while (true) {
                    int flag = 0;
                    if ((flag = is.read()) == -1) {
                        break;
                    } else {
                        int count = is.available();
                        if (count > 0) {
                            byte[] b = new byte[count];
                            is.read(b);
                            byte[] temp = new byte[count + 1];
                            temp[0] = (byte) flag;
                            for (int i = 0; i < b.length; i++) {
                                temp[i + 1] = b[i];
                            }
                            log.info(new String(temp));
                            String tempStr = "";
                            for (int i = 0; i < temp.length; i++) {
                                tempStr += " " + temp[i];
                            }
                            log.info(tempStr);
                            os.write(temp);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                if (client != null) {
                    try {
                        client.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (server != null) {
                    try {
                        server.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                log.info("receiver close socket");
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (server != null) {
                try {
                    server.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            log.info("receiver close socket");
        }
    
    }

     Pool.java

    package com.dc.tcp.proxy;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Pool {
        
        private static Pool instance = null;
        private ExecutorService executor = null;
        
        private Pool() {
            super();
            executor = Executors.newFixedThreadPool(6);
        }
        
        public static Pool getInstance() {
            if (instance == null) {
                synchronized (Pool.class) {
                    if (instance == null) {
                        instance = new Pool();
                    }
                }
            }
            return instance;
        }
        
        public ExecutorService getExecutor() {
            return executor;
        }
    
    }
  • 相关阅读:
    你可能不知道的js -- 数据类型转换
    项目开发过程中使用工具的相关配置 -- git / svn / 谷歌跨域
    博客园个人博客页面主题
    keep-alive指定条件缓存的坑
    element的多文件上传
    谷歌应用商店扩展程序
    从郭建的遭遇(自称最惨的创业者)看‘程序员的人生观’(一)
    我深表遗憾
    入门学习三
    编程入门学习二
  • 原文地址:https://www.cnblogs.com/sayaoailun/p/4939807.html
Copyright © 2020-2023  润新知