Linux what is socket file

What are socket files?

Recently, I started using tmux; I’m trying to use the pair programming feature in that software. During the process a socket file was created. My question is: what are socket files, how am I to open them on Ubuntu and how are they used?

2 Answers 2

Sockets are a special file type, similar to TCP/IP sockets, providing inter-process networking protected by the file system’s access control.

For example, when you open a listening socket in one terminal with netcat:

then send data from another terminal by:

echo mytext | nc -U socket.sock 

mytext appears on the first terminal.

By default nc stops listening after an End-of-File character.

I don’t seem to have the U option with nc, I got an error U option not defined. Is socket.sock a file you created previously¿? Can you use tmux -S and use the socket file descriptor (tipicaly 3) from one of the ends of the socket to send data on that socket¿?

A unix domain socket is a bidirectional pipe similar to a TCP/IP socket. A server listens for and accepts connections from clients, and then can communicate with the client on the newly accepted connection. What is special about unix domain sockets is that instead of having an IP address and port number, they have a file name as their address. This allows other applications that know nothing about networking to be told to open the file and read or write and the data is sent to the server instead of to the disk.

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Linux Socket

There is no commmand line tool to create sockets since a socket is always connect to a server which handles the requests sent to that socket.

Читайте также:  Сеть microsoft из linux

How to find sockets and list them?

ss --summary ss --tcp --all ss --udp --all ss --unix // unix domain sockets find / -type s -print 

Usecases — Examples

Types of Files in Linux

Named sockets vs normal/un-named sockets

Why bother about sockets?

Analogy: Call on Telephone network

Linux Pipes

Pipes are a unidirectional IPC mechanism, with one end of a pipe opened for reading and the other end opened for writing.

Both the ls and more commands run without knowing they’re connected through a pipe. They simply read from file descriptor 0 (standard input) and write to file descriptor 1 (standard output).

  • All descriptors point to active terminal session.
  • Pipes follow the mechanism of IPC(inter process communication)
  • Similar to other linux concepts like, signals, queues and sockets.

How does it happen?

  • Virtual Filesystem VFS in Kernel space 1
  • Shortcoming, can only work in parent-child
  • Named PIPES are called FIFO
# blocking mkfifo fifo cal > fifo ls | less 

Python implementation

  • socket inbuilt module along with socketserver — A framework for network servers
  • SERVER : bind , connect , close
  • CLIENT : send , recieve
  • Fixed header-size
  • Decide how to choose read/write buffer size!
  • Send bytes , raw
  • Use pickle to seriase data before sending data over socket

SoCAT

Sockets are just doors that you open on 2 computers so they can communicate with each other over the network

  • stands for SOcket CAT2
  • establish 2 bi-directional byte streams 2
    • The General Bidirectional Pipe Handler
    • EXEC/SYSTEM — fork a subprocess using
    • GOPEN read from a file on filesystem
    • PIPE — generate a pipe if not already exists
    • PTY — generates a pseudo terminal and uses its master side
    • network socket
    • file descriptor
    • a Unix domain datagram or stream socket
    • TCP
    • UDP (over both IPv4 and IPv6)
    • SOCKS 4/4a over IPv4/IPv6
    • SCTP
    • datagram and stream sockets
    • named and unnamed pipes
    • raw IP sockets
    • OpenSSL
    • on Linux even any arbitrary network device .

    socat -d -d — TCP4:www.example.com:80 \ TCP4-LISTEN:www.example.com:80, fork socat -d -d \ READLINE\!\!OPEN:file.txt,creat,trunc \ SYSTEM:’read stdin; echo $stdin’ socat UDP4-RECVFROM:161,fork \ UDP4-SENDTO:localhost:10161 // socket closes after 1st connection socat -u \ GOPEN:$HOME/README.adoc \ TCP-LISTEN:5778,reuseaddr socat -u \ TCP:localhost:5778 \ STDOUT > /path/to/download/file socat tcp-listen:8000,reuseaddr,fork \ file:/dev/ttyUSB0,nonblock,waitlock=/var/run/tty0.lock,b115200,raw,echo=0 socat -d -d \ pty,raw,echo=0 \ pty,raw,echo=0

    1. Source, alias for STDIO
    2. Sink, check man page for all aliases
    3. Options: retries, timeouts etc

    Use cases

    1. Use SOCAT for port forwarding
      1. socat TCP-LISTEN:8080,reuseaddr,fork TCP:localhost:1313
      2. forward TCP 8080 requests to hugo server running at port 1313
      3. Tested in browser, works great
      4. Clearing up sockets takes sometime
        1. we are closing the connection, but not clearing the socket
        2. socket probably enter in TIME_WAIT state
        1. Assumes that socat is installed and already running on target in listening mode
        2. On development machine, forward local ttyUSB connection to remote serial port
        1. date | socat — GOPEN:/tmp/capture,append

        Footnotes

        Источник

        What are .sock files and how to communicate with them

        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?

        Please supply some context so we know what you’re asking about. Where did you encounter a ‘.sock’ file?

        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 way

        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?».

        2 Answers 2

        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

        Here’s detailed info on working with sockets in Python

        You can communicate with sockets using netcat-openbsd or socat

        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.

        Источник

        How do I find socket files in Linux?

        1 answer. A socket is a file for processes to exchange data. You can see more data about it using the netstat , lsof , and fuser commands.

        Where is the socket file?

        The default location for the Unix socket file that the server uses to communicate with local clients is /tmp/mysql. sock . (For some distribution formats, the directory may be different, such as /var/lib/mysql for RPM.)

        What are socket files in Linux?

        a plug is a special file used for communication between processes, which allows communication between two processes. In addition to sending data, processes can send file descriptors over a Unix domain socket connection using the sendmsg() and recvmsg() system calls.

        How do I see a socket?

        Type netstat -a -or -n -b from an elevated command prompt (administrator). -b is to display the executable involved in creating each connection or listening port. See netstat –help for a list of all the options.

        How do I access a socket on Unix?

        Check the Unix socket connection from the command line At the command line, run the following command: mysql -u root -p -S /var/run/mysqld/mysql. sock . Type a password for your root user and press Enter.

        How do you create a socket 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 one time. After you create a UNIX domain socket, you must bind the socket to a unique file path using the bind function.

        Why are they sockets files?

        The special thing about Unix domain sockets is that instead of having an IP address and port number, they have a file name as the address. This allows other applications that know nothing about networking to be told to open the file and read or write, and the data is sent to the server instead of to disk.

        Where is the Postgres socket?

        The socket file for a PostgreSQL server is located in the directory specified by the unix_socket_directory variable in postgresql. server configuration file conf. For example, on Ubuntu 11.04 with PostgreSQL 8.4, the socket directory is /var/run/postgresql and the socket file is /var/run/postgresql/. s.

        How do I use sockets in Linux?

        1. Create a socket with the socket() system call.
        2. Bind the socket to an address using the bind() system call. …
        3. Listen for connections with the listen() system call.
        4. Accept a connection with the accept() system call. …
        5. Send and receive data.

        What are regular files in Linux?

        The normal file is a most common file type found on linux system. It governs all the different files such as text files, images, binary files, shared libraries, etc.

        What are the three standard files in Unix?

        The standard UNIX file descriptors: Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr)

        Источник

Оцените статью
Adblock
detector