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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM