简介:本文将介绍如何在 Linux 下使用 netcat (nc) 工具发送和接收 UDP 和 TCP 数据。通过这些示例,你将了解如何使用 netcat 进行基本的网络通信操作。
在 Linux 下,netcat(nc)是一个功能强大的网络工具,可用于发送和接收 TCP 和 UDP 数据。以下是使用 nc 进行 UDP 和 TCP 通信的基本示例。
TCP 通信示例:
这将使 netcat 在本地机器上的 12345 端口上监听连接。当有客户端连接到该端口时,它将在终端上显示接收到的数据。
nc -l 12345
这将向本地机器的 12345 端口发送字符串“Hello, World!”。
echo “Hello, World!” | nc localhost 12345
这将使 netcat 在本地机器上的 12345 端口上监听 UDP 数据包。当有客户端向该端口发送数据时,它将在终端上显示接收到的数据。
nc -l -u 12345
这将向本地机器的 12345 端口发送 UDP 数据包,其中包含字符串“Hello, World!”。
echo “Hello, World!” | nc -u localhost 12345