Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are .sock files and how to communicate with them

Tags:

sockets

netcat

  • What are .sock files?
  • How can I communicate with a .sock file?

Elaborating on the 2nd bullet, I understand that .sock files are for Inter-process communication. How can I 'communicate' with them? Let us say a sock file is designed to respond in a specific way (For ex: it takes the input 'time' and prints out the current time).

I prefer higher level programming languages (python) more than C/C++ . It'd also be better if someone can point me to some application (like nc maybe?) that I can use to communicate with .sock files in a quick and dirty way?

Thanks

like image 906
Julian Simpson Avatar asked Oct 01 '18 22:10

Julian Simpson


People also ask

What are socket files used for?

A socket is a special file used for inter-process communication, which enables communication between two processes. In addition to sending data, processes can send file descriptors across a Unix domain socket connection using the sendmsg() and recvmsg() system calls.

How do I create a .sock file?

To create a UNIX domain socket, use the socket function and specify AF_UNIX as the domain for the socket. The z/TPF system supports a maximum number of 16,383 active UNIX domain sockets at any time. After a UNIX domain socket is created, you must bind the socket to a unique file path by using the bind function.

How do I interact with a UNIX socket?

Using -u along with -U tells netcat that it is a UNIX-domain datagram socket. Similarly, while using socat , use UNIX-CLIENT option. Using this option you can connect to both UNIX-domain stream and UNIX-domain datagram sockets.

What is the difference between an open file and a socket?

The main difference between sockets and files is that the operating system binds file descriptors to a file or device when the open() call creates the file descriptor.


1 Answers

Sock files are socket files they are endpoints in communication pipes.

how to create socket files:

  • let uwsgi create them when interacting with servers(e.g. nginx) sudo uwsgi --ini /path/to/ini/file/ In the ini file you need to have passed a path to where you want to add the socket file .ini files will on unix sysytems live at /etc/uwsgi/sites/*.ini

  • create socket files using a high level language try python: python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/test.sock')"

  • use nc Server side have: nc -l -p 1234 Client side have: nc -l -p 1234 That way you have a open socket that can communicate. I leave this here
like image 94
zerone Avatar answered Oct 11 '22 05:10

zerone