於 Debian / Ubuntu Linux 想要透過 Socket 互相傳送資料、聊天等等, 可以透過 nc (NetCat) 快速實現.
使用 nc 聊天
- nc -l 1234 # Server 1
- nc 127.0.0.1 1234 # 127.0.0.1 換成 Server 1 的 IP 即可.
- 這樣子雙方打得字就會戶相傳過去, 可以達到聊天的效果. (Ctrl + D 可以結束)
掃 Port - 看哪個 Port 有開放
- nc -v -w 2 localhost -z 30-80
localhost [127.0.0.1] 80 (http) open
localhost [127.0.0.1] 53 (domain) open
傳送資料
- cat filename | nc -l 9090 # 透過 9090 port 傳送 filename 的檔案內容
- nc localhost 9090 # 接收 9090 port 傳來的檔案內容
檔案傳送
- nc -l 1234 > filename # 接收 Server 先建立 (Port: 1234)
- nc 127.0.0.1 1234 < test.txt # 傳送 Server 後建立並傳送檔案內容過去. (將 127.0.0.1 換成要傳送過去的 IP 即可)
nc 與 telnet 溝通
- nc -l 1234 # 建立 1234 port socket server
- telnet localhost 1234 # 連到 socket server
- # 再來互相打字, 都可以相互接收
相關網頁
延伸閱讀
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

