Page 1 of 1

Playing with sockets

Posted: Fri Feb 07, 2014 7:38 pm
by Flatland_Spider
I'm trying to play around with Unix sockets. I'm trying to create a socket that will deliver a little text string payload, "Hello world.", each time a connection is made, and I'm going to write a little python program that will connect to the socket.

I've been working with ncat, but it's not working particularly well.

Term1:
$ echo "Hello world." | ncat -lU ./test.sock -k

Term2:
$ ncat -U ./test.sock
"Hello World"
<ctrl+d>
$ ncat -U ./test.sock
<ctrl+d>
$


It's also not cleaning up the socket, but that fixed with a script.

Any ideas? I could switch to socat, but I'm not familiar with that program.

Re: Playing with sockets

Posted: Sat Feb 08, 2014 4:15 am
by way2strong
This is out of my depth so don't expect much more from me, but this will print the message every time a connection is made.

ncat -lU ./test.socket -c "echo Hello World." -k

Re: Playing with sockets

Posted: Sat Feb 08, 2014 10:39 am
by Flatland_Spider
That did it. Thanks!

Now I just need to get it to send an EOF so the connection will close.

Re: Playing with sockets

Posted: Sat Feb 08, 2014 10:47 am
by notfred
Haven't used ncat, if you are happy programming in C there are plenty of socket tutorials around that do exactly what you are looking for. C is the real language of Unix and sockets.

Re: Playing with sockets

Posted: Sat Feb 08, 2014 11:27 am
by Flatland_Spider
I figured it out. --recv-only was missing on the receiving side.

Term 1:
ncat -lU ./test.sock -c "echo Hello World." -k

Term 2:
$ncat --recv-only -U ./test.sock
Hello World.
$


Alternately,

With that working, I'm moving on to seeing if I can get python to connect to it.

@notfred
That did cross my mind. I just wanted to get a quick prototype socket up, so I could move on to writing the python program. I'm going to try writing a C program that will create a socket one of these days, but today is not that day.

The socket I want to connect to already exists (It's the Docker socket), and as soon as python is connecting to this prototype socket, I'm going to switch over to the real socket.