zoukankan      html  css  js  c++  java
  • Java 端口转发

    Java 端口转发

    上班的网络是通过http代理的。因为前段时间搬了工位,这几天同事的ip一直不能通过代理上网,但是我的可以。

    为了能让他也能上网,所以用java做了下转发。他电脑配置代理服务器为我电脑的ip

    方案

    本机监听端口,比如8888,收到的请求直接转发到我们的代理服务器。这样代理服务器以为是我电脑访问,所以会放行。因为共享带宽,只作为临时解决网络问题的方案。

    TranslatePort.java

    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TranslatePort {
    
        public static void main(String[] args) {
            try {
                //获取本地监听端口、远程IP和远程端口
                int localPort = Integer.valueOf(args[0]);
                String remoteIp = args[1];
                int remotePort = Integer.valueOf(args[2]);
    
                //启动本地监听端口
                ServerSocket serverSocket = new ServerSocket(localPort);
                System.out.println("localPort="+localPort + ";remoteIp=" + remoteIp +
                        ";remotePort="+remotePort+";启动本地监听端口" + localPort + "成功!");
                Socket clientSocket = null;
                Socket remoteServerSocket = null;
                while(true){
                    try {
                        //获取客户端连接
                        clientSocket = serverSocket.accept();
                        System.out.println("accept one client");
                        //建立远程连接
                        remoteServerSocket = new Socket(remoteIp ,remotePort);
                        System.out.println("create remoteip and port success");
                        //启动数据转换接口
                        (new TransPortData(clientSocket ,remoteServerSocket ,"1")).start();
                        (new TransPortData(remoteServerSocket ,clientSocket,"2")).start();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    //建立连接远程
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    TransPortData.java

    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class TransPortData extends Thread {
    
        Socket getDataSocket;
        Socket putDataSocket;
    
        String type;
    
        public TransPortData(Socket getDataSocket, Socket putDataSocket, String type) {
            this.getDataSocket = getDataSocket;
            this.putDataSocket = putDataSocket;
            this.type = type;
        }
    
        @Override
        public void run() {
            try {
                InputStream in = getDataSocket.getInputStream();
                OutputStream out = putDataSocket.getOutputStream();
                Long lastTime = System.currentTimeMillis();
                while (true) {
                    //读入数据
                    byte[] data = new byte[1024];
                    int readlen = in.read(data);
                    if (System.currentTimeMillis() - lastTime > 60000L) {
                        break;
                    }
                    if (getDataSocket.isClosed() || putDataSocket.isClosed()) {
                        break;
                    }
                    //如果没有数据,则暂停
                    if (readlen <= 0) {
                        Thread.sleep(300);
                        continue;
                    }
                    lastTime = System.currentTimeMillis();
                    out.write(data, 0, readlen);
                    out.flush();
                }
            } catch (Exception e) {
                System.out.println("type:" + type);
                e.printStackTrace();
            } finally {
                //关闭socket
                try {
                    if (putDataSocket != null) {
                        putDataSocket.close();
                    }
                } catch (Exception exx) {
                }
    
                try {
                    if (getDataSocket != null) {
                        getDataSocket.close();
                    }
                } catch (Exception exx) {
    
                }
            }
        }
    
    }
    
    

    参考:https://blog.csdn.net/weixin_34409357/article/details/92052664

  • 相关阅读:
    西门子SCL读写DB数据
    LeetCode8.字符串转换整数(atoi) JavaScript
    LeetCode8.字符串转换整数(atoi) JavaScript
    WebRequestSugar
    iosblock用法
    datasci
    UINavigationController学习笔记
    iOSTab bar
    自定义tab bar控件 学习资料
    Ios tab Bar 使用方法
  • 原文地址:https://www.cnblogs.com/jimmyfan/p/11928567.html
Copyright © 2011-2022 走看看