While reading the source code of cat
command, I found cat
command supports for reading from socket. You can view the source at http://src.gnu-darwin.org/src/bin/cat/cat.c.html. But I've never use this command with socket: just quickly viewing a file or concatenating multiple files. What can I do with cat
+ socket? Can you give an interesting example of using cat
command reading from a socket? Thanks.
-
10Whatever you do, do not plug your cat in a socket: it may get electrocuted :) (I'm sorry, I couldn't resist).– Sergey KalinichenkoAug 13, 2012 at 2:36
-
Do you have a programming context for this question?– dmckee --- ex-moderator kittenAug 14, 2012 at 2:30
-
This question appears to be off-topic because it is about using unix tools. It would be better suited for Unix & Linux.– Jonathan HallFeb 27, 2014 at 19:08
-
Like this– user11153Nov 16, 2014 at 23:25
2 Answers
use "netcat" which on unix/linux is the command nc
. You most likely want to be the client socket, so something like cat <filename> | nc <ip> <port>
-
The role of that example of
cat
is just passing the contetns to/dev/stdout
(as we always do). So, I want to know, can we usecat
as a server side? Instead ofnc -l <ip> <port>
?– itchynyAug 13, 2012 at 3:01
The cat source code you found contains some magic to call socket() and connect() when you try to cat a socket. It doesn't contain any listen() or accept() so there's no way it can do the "server side" stuff. And it works with unix domain sockets, not inet sockets, so don't think it's for cat'ing stuff across an actual network. Unix domain sockets are just endpoints for local inter-process communication.
I can't imagine what use case they had in mind when they added this feature to cat.
-
Thanks. To be calm, what you are saying is perfectly right: I found no
listen
in the code. Thanks again.– itchynyAug 13, 2012 at 5:06