How to use NetCat for Windows to send a binary file to a TCP connection?
Categories:
Transferring Binary Files with NetCat on Windows

Learn how to effectively use NetCat for Windows to send binary files over a TCP connection, covering setup, execution, and common pitfalls.
NetCat, often referred to as the 'TCP/IP Swiss Army Knife', is an incredibly versatile tool for network operations. While commonly associated with Linux, a robust version is also available for Windows. This article focuses on a specific, yet powerful, use case: sending binary files over a TCP connection from a Windows machine using NetCat. This technique is invaluable for tasks like transferring executables, archives, or any non-text data between systems, especially in environments where traditional file transfer protocols might be restricted or unavailable.
Understanding NetCat for Binary Transfers
NetCat's core functionality involves reading and writing data across network connections using TCP or UDP. For binary files, the key is to ensure that NetCat handles the data as raw bytes without any character encoding transformations. On Windows, the nc.exe
executable behaves similarly to its Unix counterpart, allowing redirection of standard input/output to and from files. This makes it ideal for piping binary file contents directly into a network stream.
flowchart TD A[Sender (Windows)] --> B{Establish TCP Connection} B --> C[Read Binary File] C --> D[Pipe File Content to NetCat] D --> E[NetCat Sends Data over TCP] E --> F[Receiver (Linux/Windows)] F --> G[NetCat Receives Data] G --> H[Pipe Received Data to File] H --> I[Write Binary File]
Process flow for sending a binary file using NetCat.
Setting Up the Environment
Before you can send files, you need to ensure NetCat is installed on your Windows machine and that you have a receiving end ready. For this guide, we'll assume a Linux machine as the receiver, but the principles apply to another Windows machine as well. You'll need to download a Windows-compatible version of NetCat (e.g., nc.exe
from various open-source distributions). Place nc.exe
in a directory included in your system's PATH, or navigate to its directory in your command prompt.
ncat
or the original nc.exe
from various open-source projects.Sending a Binary File from Windows
To send a binary file, the Windows NetCat instance will act as the client, connecting to a listening server. The content of the binary file will be piped into NetCat's standard input, which then transmits it over the established TCP connection. The receiving end will listen on a specific port and redirect the incoming data to a new file.
1. Prepare the Receiver (Linux Example)
On your Linux machine, open a terminal and set up NetCat to listen on a specific port (e.g., 12345) and redirect all incoming data to a file named received_file.bin
. Make sure the file doesn't exist or is empty before starting.
2. Execute on Windows Sender
On your Windows machine, open Command Prompt or PowerShell. Navigate to the directory containing your binary file (e.g., my_binary.exe
). Use the type
command to output the binary file's content to standard output, and then pipe this output to nc.exe
which will connect to the Linux receiver's IP address and port.
Linux Receiver
nc -l -p 12345 > received_file.bin
Windows Sender (CMD)
type my_binary.exe | nc.exe <LINUX_IP_ADDRESS> 12345
Windows Sender (PowerShell)
Get-Content -Path .\my_binary.exe -Encoding Byte | nc.exe <LINUX_IP_ADDRESS> 12345
Verifying the Transfer
After the transfer completes (NetCat on the sender will usually exit after sending the file), you should verify the integrity of the received file. The simplest way to do this is by comparing the file sizes and, more robustly, by comparing their cryptographic hash values (e.g., MD5 or SHA256).
Linux Hash Check
md5sum received_file.bin
Windows Hash Check (CMD)
certutil -hashfile my_binary.exe MD5
Windows Hash Check (PowerShell)
Get-FileHash -Path .\my_binary.exe -Algorithm MD5
If the MD5 (or SHA256) hashes match, your binary file has been transferred successfully without corruption. This method provides a quick and effective way to move binary data across networks using a simple, ubiquitous tool like NetCat.