Opening a socket opens a socket, which is what you see listed as file descriptor 3 in your output (socket:[5474494]
). The other three file descriptors are the standard input, output and error descriptors, which are opened by default for any process. In your case, these point to the terminal where the program is running, which is why “surni” shows up there when you write to the file descriptor.
To write to the socket, you need to use appropriate mechanisms, such as netcat
in the other direction:
echo Hello | nc localhost 9999
or, if you’re using Bash:
echo Hello > /dev/tcp/localhost/9999
However it appears you already have a connection established to port 9999 using another netcat
, so that won’t actually work in your case. You need to use the established connection...
If you’re expecting a long-running nc
to provide a way for other processes to feed into the socket, you need to set that up e.g. using a FIFO:
mkfifo socket-input
nc localhost 9999 < socket-input
echo Hello > socket-input
/dev/tcp/
but this abstracts the layers I want to interact with which are the file descriptors of a socket.Another question, do you know how to bind a process to a socket so that I can send data via the socket? I want to use this since I can't send data to the STDIN of a process (for Linux /proc//fd/ descriptors are just symlinks to a terminal)
– aDoN Apr 11 '18 at 13:19strace -p<pid> -s9999 -e write
that spy other processes and see the input and output of a process? (nano, vi, python interctive shell...., every process) – aDoN Apr 11 '18 at 14:28strace
uses ptrace to spy on other processes, it gets help from the kernel to view things from inside the target process. – Stephen Kitt Apr 11 '18 at 14:44gdb -p <pid>
and then:call write(3,"test",6)
. Still don't know how to operate with the file descriptor 3 without GDB, maybe it's impossible given sockets theory but it's a step forward – aDoN Apr 12 '18 at 10:49