What are .sock files and how to communicate with them
Asked Answered
T

2

18
  • 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

Telephony answered 1/10, 2018 at 22:41 Comment(3)
Please supply some context so we know what you're asking about. Where did you encounter a '.sock' file?Pillbox
James, the .sock file has been preprogrammed to provide a "challenge-response" algorithm. For ex: Me to .sock: "MyUserName" .sock to me: "Here is your token: a_token". I need to figure out how to communicate with the .sock in a bidirectional wayTelephony
Hello and welcome on StackOverflow!!! In order to get your a better answer, and your answer don't get deleted later on, you would have to read and follow some basic rules and conventions that will help everyone to communicate and help better. Please go to stackoverflow.com/help and read about the topic "What topics can I ask about here?", and "What types of questions should I avoid asking?".Manful
S
6

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
Subaxillary answered 23/1, 2020 at 12:23 Comment(0)
P
2

Here's detailed info on working with sockets in Python

https://pymotw.com/2/socket/uds.html

You can communicate with sockets using netcat-openbsd or socat

nc -U <path_to_socket_file>

socat - UNIX-CONNECT:<path_to_socket_file>

source for the second part: https://unix.stackexchange.com/questions/26715/how-can-i-communicate-with-a-unix-domain-socket-via-the-shell-on-debian-squeeze

UPDATE: here's an example of a socket server taken from the first link

import socket
import sys
import os

server_address = './uds_socket'

# Make sure the socket does not already exist
try:
    os.unlink(server_address)
except OSError:
    if os.path.exists(server_address):
        raise

# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Bind the socket to the port
print >>sys.stderr, 'starting up on %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()
    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall(data.upper())
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()

save this into a file called sock.py and run

~/Development/temp ᐅ python sock.py
starting up on ./uds_socket
waiting for a connection

then connect using socat

~/Development/temp ᐅ socat - UNIX-CONNECT:uds_socket
hello
HELLO

write something - and you'll receive the same thing but in uppercase as a reply.

Psychologism answered 1/10, 2018 at 23:4 Comment(1)
I installed the netcat-openbsd and socat. It looks like I can send a message to the .sock but can't seem to collect the response it is generating: nc: user:~/test$ nc -U /path/to/the.sock user:~/test$ socat: user:~/test$ socat - UNIX-CONNECT:/path/to/the.sock user:~/test$ How do I collect the response being generated by /path/to/the.sock .Telephony

© 2022 - 2024 — McMap. All rights reserved.