Linux socat轻松实现TCP/UDP端口转发

 1、TCP端口转发

socat -d TCP4-LISTEN:80,reuseaddr,fork TCP4:127.0.0.1:8080

2、UDP端口转发

socat -T 600 UDP4-LISTEN:5353,reuseaddr,fork UDP4:114.114.114.114:53

3、文件传输

服务端:

socat -u open:FILENAME tcp-listen:12345

客户端

socat -u tcp:ServerIP:12345 open:LOCALFILE,create

【说明】 

-u 表示数据单向传送,从第一个参数传递到第二个参数;-U则表示从第二个参数传送到第一个参数。
open 表示使用系统调用open()打开文件,不能打开unix域socket。
tcp-listen 表示监听tcp端口。
create 表示如果文件不存在则创建。
传输结束后两端均退出。

4、读写分离

(使用!!符号,左侧表示读,右侧表示写)

socat open:hello.html!!open:log.txt,create,append tcp-listen:12345,reuseaddr,fork

【说明】

open:hello.html 表示读hello.html文件。
open:log.txt 表示收到的数据写入log.txt文件。
reuseaddr 见socket的SO_REUSEADDR。
fork 请求到达时,fork一个进程进行处理。
在bash下,需要用对!进行转义。

参考:http://www.dest-unreach.org/socat/doc/socat.html

原文地址:https://www.cnblogs.com/imzye/p/7381764.html