Socket Quick Start

//TODO:

  1. python socket module demos: https://pymotw.com/2/socket/uds.html

最近在做Proxy的工作,重新回顾和学习了很多相关的东西,这里把Socket的分类和概念梳理一下。

Socket

https://en.wikipedia.org/wiki/Socket

  • Network socket, an end-point in a bidirectional communication across a network or the Internet
  • Unix domain socket, an end-point in local bidirectional inter-process communication
  • socket(), a system call defined by the Berkeley sockets API

Unix socket vs Network socket

Unix Domain Socket

https://en.wikipedia.org/wiki/Unix_domain_socket A Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system.

IPC 也是一种概念,有多种实现的方式。

The API for Unix domain sockets is similar to that of an Internet socket, but rather than using an underlying network protocol, all communication occurs entirely within the operating system kernel. Unix domain sockets may use the file system as their address name space. (Some operating systems, like Linux, offer additional namespaces.) Processes reference Unix domain sockets as file system inodes, so two processes can communicate by opening the same socket.

Valid socket types in the UNIX domain are:

  • SOCK_STREAM (compare to TCP) – for a stream-oriented socket
  • SOCK_DGRAM (compare to UDP) – for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don’t reorder datagrams)
  • SOCK_SEQPACKET (compare to SCTP) – for a sequenced-packet socket that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent

Network Socket

仔细读一下维基, network socket是指一种连接,这种连接有很多具体的实现方法。 https://en.wikipedia.org/wiki/Network_socket Also referred to as Internet socket.

A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. The structure and properties of a socket are defined by an application programming interface (API) for the networking architecture. Sockets are created only during the lifetime of a process of an application running in the node.

Socket Address is comprised of:

  • protocol type
  • IP address
  • port number

On Unix-like operating systems and Microsoft Windows, the command-line tools netstat or ss are used to list established sockets and related information.

Several types of Internet socket are available:

  • Datagram sockets: connectless sockets, which use UDP.
  • Stream sockets: connection-oriented sockets, which use TCP.
  • Raw sockets: IP packet.

Berkeley Sockets

https://en.wikipedia.org/wiki/Berkeley_sockets Berkeley sockets is an application programming interface (API) for Internet sockets and Unix domain sockets, used for inter-process communication (IPC). 是以上几个socket的一种统一的抽象表示。

The term POSIX sockets is essentially synonymous with Berkeley sockets, but they are also known as BSD sockets.

0%