Playing with sockets

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

Playing with sockets

Postposted on Fri Feb 07, 2014 7:38 pm

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.

Code: Select all
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.
Flatland_Spider
Gerbil Elite
 
Posts: 853
Joined: Mon Sep 13, 2004 8:33 pm
Location: The 918/539

Re: Playing with sockets

Postposted on Sat Feb 08, 2014 4:15 am

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.

Code: Select all
ncat -lU ./test.socket -c "echo Hello World." -k
way2strong
Gerbil
 
Posts: 34
Joined: Fri Jun 27, 2008 7:07 am

Re: Playing with sockets

Postposted on Sat Feb 08, 2014 10:39 am

That did it. Thanks!

Now I just need to get it to send an EOF so the connection will close.
Flatland_Spider
Gerbil Elite
 
Posts: 853
Joined: Mon Sep 13, 2004 8:33 pm
Location: The 918/539

Re: Playing with sockets

Postposted on Sat Feb 08, 2014 10:47 am

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.
notfred
Grand Gerbil Poohbah
 
Posts: 3746
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Playing with sockets

Postposted on Sat Feb 08, 2014 11:27 am

I figured it out. --recv-only was missing on the receiving side.

Code: Select all
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.
Flatland_Spider
Gerbil Elite
 
Posts: 853
Joined: Mon Sep 13, 2004 8:33 pm
Location: The 918/539


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 1 guest