When working with Memcached servers, sometimes you need to test binary protocol responses without implementing a full client. The hexadecimal value 0x80 is particularly significant as it's the magic byte indicating a Memcached binary protocol request header.
Here are three practical ways to send 0x80 byte using common Unix tools:
# Method 1: Using printf with netcat printf '\x80' | nc localhost 11211 # Method 2: Using echo with netcat echo -ne '\x80' | nc 127.0.0.1 11211 # Method 3: Using dd with netcat dd if=/dev/zero bs=1 count=1 | tr '\0' '\200' | nc memcached-server 11211
The Memcached binary protocol starts each request with a 24-byte header where the first byte must be 0x80. This magic number distinguishes binary protocol requests from ASCII protocol requests.
For more thorough testing, you might want to send complete binary requests. Here's how to send a minimal GET request:
# GET request for key "test" (0x74657374) printf '\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x74\x65\x73\x74' | nc localhost 11211
When testing binary protocols, these additional commands can help:
# View raw hex output of what you're sending printf '\x80' | hexdump -C # Capture server response in hex printf '\x80' | nc localhost 11211 | hexdump -C # Use telnet for ASCII protocol comparison telnet localhost 11211
While netcat is most common, other tools can achieve similar results:
# Using socat echo -ne '\x80' | socat - TCP:localhost:11211 # Using nmap's ncat printf '\x80' | ncat localhost 11211
Remember that:
- The binary protocol requires proper header construction
- Network byte order (big-endian) is used for multi-byte values
- Different Memcached versions may respond differently
- Always test in isolated environments first
When testing Memcached server responses or debugging binary protocol interactions, you often need to send specific byte sequences. The 0x80 byte is particularly significant as it's the magic byte indicating a binary protocol request in Memcached.
The simplest approach uses netcat (nc) combined with printf:
printf '\x80' | nc localhost 11211
For more complex scenarios where you need to send multiple bytes:
printf '\x80\x00\x00\x00\x00\x00\x01\x00\x00' | nc memcached.server 11211
When netcat isn't available, consider these alternatives:
Using socat:
echo -ne '\x80' | socat - TCP:localhost:11211
Using bash's built-in networking:
echo -ne '\x80' > /dev/tcp/localhost/11211
To confirm your byte was sent correctly, use tcpdump or wireshark to monitor the network traffic. For quick verification:
sudo tcpdump -i lo -X port 11211
Then run your netcat command in another terminal to see the hex output.
- Make sure your echo command supports -e flag (GNU echo)
- Check for proper line endings (use -n flag to suppress newline)
- Verify network connectivity and firewall rules
- Confirm Memcached is running in binary protocol mode
To send a complete (though invalid) binary protocol header:
printf '\x80\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | nc localhost 11211
This sends:
- Magic byte (0x80)
- Opcode (0x01 for GET)
- Key length (0x0000)
- Total 24 bytes of header