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;
} }
tcp转发的更多相关文章
- centos7 编译安装nginx+tcp转发
一.依赖 1. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install gcc-c++ 2. PCRE pc ...
- 手头没证书,如何给https做代理?Nginx TCP转发
线上的一个海外充值接口(https)经常因我朝网络问题中断,想借助hk的机器做个https反向代理又没证书. 一开始 一开始想到的办法是借助Nginx的tcp转发进行代理: 编译NGINX时加入 -- ...
- Golang TCP转发到指定地址
Golang TCP转发到指定地址 第二个版本,设置指定ip地址 代码 // tcpForward package main import ( "fmt" "net&qu ...
- Golang tcp转发 remoteAddr错误
Golang tcp 转发 第一版本 accept获取的Conn里的localAddr做为源地址,remoteAddr来做为目的地址 // tcpForward package main import ...
- [转帖]【rinetd】CentOS7.x上轻量级TCP转发工具rinetd的安装配置
[rinetd]CentOS7.x上轻量级TCP转发工具rinetd的安装配置 https://www.jianshu.com/p/2605d247b944 这一个写的更加全面了. 2019.07.0 ...
- nginx编译安装以及配置tcp转发
依赖包安装 yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl open ...
- FreeBSD NGINX TCP转发
前几天搞转发,研究了下TCP转发,现在记录下来 首先加载模块 注意:这是FreeBSD的位置.并且需要NGINX支持 load_module /usr/local/libexec/nginx/ngx_ ...
- p2p-tunnel 打洞内网穿透系列(三)TCP转发访问内网web服务
系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...
- p2p-tunnel 打洞内网穿透系列(二)TCP转发访问内网共享文件夹
系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...
- CentOS7.x上轻量级TCP转发工具rinetd的安装配置
一.实验背景 Linux下端口转发一般都使用iptables来实现,使用iptables可以很容易将TCP和UDP端口从防火墙转发到内部主机上. 如果需要将流量从专用地址转发到不在您当前网络上的机器上 ...
随机推荐
- 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项
参考动态执行T4模板:https://msdn.microsoft.com/zh-cn/library/bb126579.aspx 我项目是.NET Framework 4.5控制台应用程序写的. 执 ...
- CentOS开机无法进入系统,如何查错
开机时按e/F5按钮,进入选择系统界面 会出现 CentOS(2.6.32-...类似的选择列表,选择默认的系统然后按e: 这时会出现 root kernel ... initd... 三个选项,选择 ...
- EF4.1使用
EF分为三类: db first:首先建立数据库,然后通过ADO.Net Entity Data Model项目建立.edmx文件,这是一个xml文件主要作用就是映射类和数据表 model first ...
- Lucene.net 多条件查询搜索
最近一直在研究lucene,目的是想让网站实现像搜索引擎那样的搜索,可以快速.准确的帮用户查询出想要的结果.废话不多说,上代码实例: 1.利用BooleanQuery进行多条件搜索(比较灵活) L ...
- (转)关于URLDownloadToFile下载文件
转自:http://zhouhaijiang3.blog.163.com/blog/static/43477220200931981322497/ 在下载文件时,下载文件的目录大小写要注意和虚拟目录的 ...
- Android工作学习第5天之Activity的完全退出程序
注:本文大部分为网上转载,本人只是根据工作的需要略做整合! android 完全退出应用程序 注意:1.单例模式的学习 2.Manifest.xml,注意项目清单文件中要加上 android退出应用程 ...
- XMLHttpRequest简单总结
一.概念 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是能够: 在不重新加载页面的情况下更新网页 在页面已加载后从服务器请求数据 在页面已加载后从 ...
- MVC执行过程
HttpRuntime中的PR方法1,封装HttpContext2,获取HttpApplication 主要做3件事a,执行本事件时主要调用Init将Global编译得到类型,b,确保Appstart ...
- pod install后无反应
参考这篇文章 http://akinliu.github.io/2014/05/03/cocoapods-specs-/
- FIS
学习官网 http://fis.baidu.com/docs/beginning/getting-started.html 1. fis release: 编译并发布 fis releas ...