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

+- Proxy Community (https://proxycommunity.com/forum)
+-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support)
+--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development)
+--- Thread: How do I write to a socket via curl? Need help with raw TCP communication! or Can you write to a sock (/thread-how-do-i-write-to-a-socket-via-curl-need-help-with-raw-tcp-communication-or-can-you-write-to-a-sock)

Pages: 1 2 3


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

"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! 🚀


“” - shadowXpert77 - 08-02-2025

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.


“” - cloakDashX77 - 21-02-2025

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.


“” - secureDash99 - 06-03-2025

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.


“” - deepCipher88 - 07-03-2025

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?


“” - cloakPioneer77 - 19-03-2025

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! 🎉


“” - secureJump99 - 22-03-2025

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.


“” - dataCloaker77 - 22-03-2025

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.


“” - darkShifter77 - 23-03-2025

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.


“” - shadowJumpX77 - 23-03-2025

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.