Linux中的套接字是什么?

The socket is a mechanism that provides a connection between the two-processor system by using network stacks. In a more clear way, we can use sockets to communicate and transfer data between two systems. As we know Unix and Linux systems work on file logic. Everything in the operating system is a file where network connections are files too.

套接字是一种通过使用网络堆栈在两个处理器系统之间提供连接的机制。 以更清晰的方式,我们可以使用套接字在两个系统之间通信和传输数据。 众所周知,Unix和Linux系统可处理文件逻辑。 操作系统中的所有内容都是文件,网络连接也是文件。

用例 (Use Cases)

Sockets can be used in different cases.

套接字可以在不同情况下使用。

  • We can use socket to transfer data between two different process in same system.

    我们可以使用套接字在同一系统中的两个不同进程之间传输数据。
  • Send command to the remote system

    发送命令到远程系统
  • Download data from remote system

    从远程系统下载数据

插座类型(Socket Types)

There are four main socket types. Stream sockets and Datagram sockets are popular types.

有四种主要的插座类型。 流套接字和数据报套接字是流行的类型。

流插座 (Stream Socket)

Stream sockets uses TCP protocol for transmission. So this makes stream socket reliable way for transmission. If there is a network related problem we will get error messages about it. If we send X, Y, Z data the remote will get them in the same order X, Y, Z.

流套接字使用TCP协议进行传输。 因此,这使流套接字成为可靠的传输方式。 如果存在与网络相关的问题,我们将收到有关此问题的错误消息。 如果我们发送X,Y,Z数据,则远程将以相同的顺序获得X,Y,Z。

Stream Socket
Stream Socket
流插座

数据报套接字(Datagram Socket)

Datagram sockets uses UDP which makes the data delivery inconsistent. Another feature of datagram socket is connection-less.  The sender just send data and can not be sure whether it is arrived to the destination.

数据报套接字使用UDP,这会使数据传递不一致。 数据报套接字的另一个功能是无连接。 发件人只是发送数据,不能确定它是否到达目的地。

原始插座 (Raw Socket)

As its name suggest Raw sockets do not provide ready to use protocol. It is mainly used for creating new protocol where both side can understand. Raw sockets do not used regularly.

顾名思义,原始套接字不提供立即可用的协议。 它主要用于创建双方都可以理解的新协议。 原始插座不经常使用。

顺序数据包套接字 (Sequenced Packet Socket)

Sequenced Packet Socket is similar to the Stream Socket but have record boundaries. Sequnced Packet Socket is not popular way to use socket.

顺序数据包套接字与流套接字类似,但是具有记录边界。 顺序分组套接字不是使用套接字的流行方法。

Java套接字 (Java Socket)

Java programming language provides socket support.As we can see we need to use IP address and Port number in order to create a socket. Socket related function can be found in java.net.Socket name space The communication diagram is like below.

Java编程语言提供了套接字支持,我们可以看到需要使用IP地址和端口号来创建套接字。 与套接字相关的功能可以在java.net.Socket命名空间中找到。通信图如下所示。

Java Socket
Java Socket
Java套接字

Python套接字(Python Socket)

Python programming language also provides Socket related libraries. We can import the Socket library with the following code. Python also supports encryption of socket communication.

Python编程语言还提供了与Socket相关的库。 我们可以使用以下代码导入Socket库。 Python还支持套接字通信的加密。

import socket
LEARN MORE  HTTP Status 503 Error Code and How To Fix It?
了解更多HTTP状态503错误代码以及如何解决?

翻译自: https://www.poftut.com/what-is-linux-unix-socket/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值