How do I write to a socket via curl? Need help with raw TCP communication! or Can you write to a sock

20 Replies, 1877 Views

"Can you write to a socket via curl? Looking for a simple example."

Hey folks,

I’m trying to figure out how to write to a socket via curl for some raw TCP stuff. Like, just shoving data straight into a socket without HTTP getting in the way.

I know curl’s *usually* for HTTP, but I heard it can do raw TCP? Maybe with `--telnet` or `--raw`? Googling gives me mixed results, and the docs are... uh, *dense*.

Anyone got a quick example of how to write to a socket via curl? Like, just `echo "hello" | curl --something something`?

Bonus points if you’ve actually done this in prod and it didn’t explode.

Thanks! 🚀
Yep, you can write to a socket via curl! The `--telnet` option is what you’re looking for. Here’s a quick example:

```
echo "hello" | curl --telnet 127.0.0.1:8080
```

This sends raw TCP data. No HTTP headers, just pure bytes.

I’ve used it for testing a custom TCP server, and it worked fine. Just make sure your server’s listening on that port.

If you need more control, check out `nc` (netcat) too—it’s even better for raw socket stuff.
Wait, `--telnet`? That’s... weird. Why not use `--raw`?

Tried it once, and it kinda worked, but curl kept adding some extra stuff. Ended up switching to `socat` instead. Way cleaner for raw TCP.

If you’re stuck on curl, though, maybe `--tcp-nodelay` helps? Dunno, man. Curl’s docs are a maze.
Forget curl—use `netcat` (nc). It’s built for this.

```
echo "hello" | nc localhost 8080
```

Done. No flags, no fuss. Curl’s overkill if you just wanna write to a socket via curl alternatives.

But if you *must* use curl, `--telnet` is the closest you’ll get.
Curl *can* do it, but it’s janky. Here’s how I did it:

```
curl --raw tcp://localhost:8080 --data "hello"
```

Worked for my smoke tests, but YMMV.

Also, `telnet` is technically deprecated, so... maybe don’t rely on it?
Thanks for all the replies!

Tried `--telnet`, and it *kinda* worked, but the server didn’t like the extra bytes curl added. Switched to `nc` like some of you suggested, and it’s way cleaner.

Still curious why curl doesn’t have a proper `--raw-tcp` flag. Feels like a missed opportunity.

Anyway, problem solved—appreciate the help! 🎉
Pro tip: If you’re on Linux, just use `/dev/tcp`.

```
echo "hello" > /dev/tcp/127.0.0.1/8080
```

Bash magic. No tools needed.

Curl’s cool, but sometimes the simplest solutions are right there.
I’ve actually used `--telnet` in prod to debug a Redis socket. It worked, but curl kept timing out if the server didn’t respond fast enough.

Add `--max-time 1` to avoid hanging.

Still, `nc` or `socat` are better for raw TCP. Curl’s better at HTTP.
Why not just use Python?

```python
import socket
s = socket.socket()
s.connect(("localhost", 8080))
s.send(b"hello")
```

Way more control, and you’re not fighting curl’s quirks.
Curl’s `--telnet` flag is *supposed* to work, but it’s finicky. Sometimes it adds Telnet negotiation crap.

If you’re on macOS, `nc` behaves weirdly too.

Honestly, `socat` is the only tool that’s never let me down for raw sockets.



Users browsing this thread: 1 Guest(s)