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