Java使用Socket编写端口转发程序

Java使用Socket编写端口转发程序

1.编写Java类PortTransfer
2.编绎后形成PortTransfer相关的class,编写start.bat测试脚本,将本地的3306的端口经由13306端口进行转发。
start.bat内容如下:

java PortTransfer -serverFrontPorts 13306 -remoteHosts 127.0.0.1 -remotePorts 3306
3.双击start.bat启动,进行测试
start.bat日志如下:
D:	emp>java PortTransfer -serverFrontPorts 13306 -remoteHosts 127.0.0.1 -remotePorts 3306
2021-04-27 11:30:45 19    service started , listen on 0.0.0.0:13306 , Remote:127.0.0.1:3306

mysql -uroot -p123456 -P13306 -A

可看到通过13306端口能进行访问:
C:Usersjxlhl>mysql -uroot -p123456 -P13306 -A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type help; or h for help. Type c to clear the current input statement.

mysql>
经验分享 程序员 微信小程序 职场和发展