了解套接字基础知识

发布于 2024-10-14 10:08:40 字数 368 浏览 2 评论 0原文

我一直在阅读基本的网络编程知识,但是很难找到关于套接字到底是什么以及它与 OSI 或 TCP/IP 堆栈如何相关的直接解释。

  1. 有人可以向我解释一下什么是套接字吗?它是程序员或 API 定义的数据结构,还是网卡上的硬件设备?

  2. 上述网络模型的哪些层处理“原始”套接字?传输层?网络层?

  3. 就它们之间传递的数据而言,套接字是基于文本的还是二进制的?

  4. 是否有基于套接字的网络编程的替代方案?或者所有网络应用程序都使用某种形式的套接字吗?

如果我能读到这么多,我应该对我正在阅读的其他内容有一个非常清晰的理解。感谢您的帮助!

I've been reading up on basic network programming, but am having a difficult time finding a straight-forward explanation for what exactly and socket is, and how it relates to either the OSI or TCP/IP stack.

  1. Can someone explain to me what a socket is? Is it a programmer- or API-defined data structure, or is it a hardware device on a network card?

  2. What layers of the mentioned network models deal with "raw" sockets? Transport layer? Network layer?

  3. In terms of the data they pass between them, are socket text-based or binary?

  4. Is there an alternative to sockets-based network programming? Or do all networked applications use some form of socket?

If I can get this much I should have a pretty clear understanding of everything else I'm reading. Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇到 2024-10-21 10:08:40

简短的回答:

  1. Socket 是 IP 连接端点的抽象 - 因此,如果您将其视为 API 结构,那么您的想法并不遥远。请阅读 http://en.wikipedia.org/wiki/Internet_socket
  2. 互联网层,即 IP 协议。在实践中,您通常使用显式绑定到特定传输层参数(数据报/UDP 或流/TCP)的套接字。
  3. 套接字以网络字节顺序发送数据 - 无论是文本还是二进制,取决于上层协议。
  4. 理论上,可能是的 - 但实际上所有 IP 流量都是使用“套接字”完成的

Short answers:

  1. Socket is an abstraction of an IP connection endpoint - so if you think of it as an API structure, you are not very far off. Please do read http://en.wikipedia.org/wiki/Internet_socket
  2. Internet layer i.e. IP Protocol. In practice you usually use explicitly sockets that bind to a certain transport layer parameters (datagram/UDP or stream/TCP)
  3. Sockets send data, in network byte order - whether it is text or binary, depends on the upper layer protocol.
  4. Theoretically, probably yes - but in practice all IP traffic is done using 'sockets'
海未深 2024-10-21 10:08:40

Socket是操作系统提供的一种软件机制。顾名思义,您可以将其视为“电源插座”或某些电气连接器,尽管套接字不是物理设备,而是软件机制。在现实世界中,当您有两个电气连接器时,您可以用电线将它们连接起来。以同样的方式,在网络编程中,您可以在一台计算机上创建一个套接字,在另一台计算机上创建另一个套接字,然后连接这些套接字。当您向其中一个写入数据时,您会在另一台上接收数据。还有几种不同类型的插座。例如,如果您正在编写服务器软件,您希望有一个监听套接字,它从不发送或接收实际数据,而仅监听和接受传入连接,并为每个新连接创建一个新套接字。

Socket is a software mechanism provided by the operating system. Like its name implies, you can think of it like an "electrical outlet" or some electrical connector, even though socket is not a physical device, but a software mechanism. In real world when you have two electrical connectors, you can connect them with a wire. In the same way in network programming you can create one socket on one computer and another socket on another computer and then connect those sockets. And when you write data to one of them, you receive it on the other one. There are also a few different kinds of sockets. For example if you are programming a server software, you want to have a listening socket which never sends or receives actual data but only listens for and accepts incoming connections and creates a new socket for each new connection.

谁把谁当真 2024-10-21 10:08:40

用 C 语言来说,套接字是内核空间中的一种数据结构,对应于 UDP 或 TCP 会话的一个端点(在谈论 UDP 时,我非常宽松地使用会话)。它通常与本地端的一个端口号相关联,并且在会话的任一侧很少与多个“众所周知”的端口号相关联。

“原始套接字”或多或少是物理传输上的端点。它们很少用于应用程序编程,但有时用于各种诊断(traceroute、ping,可能还有其他),并且可能需要提升权限才能打开。

套接字本质上是二进制八位组传输。将套接字(至少是 TCP 套接字)视为基于文本的流并不罕见。

如果你挖掘得足够深的话,我还没有看到一个不涉及套接字之类的编程模型,但肯定还有其他的网络模型。 “/net/”伪文件系统,其中打开“/net/127.0.0.0.1/tcp/80”(或“tcp/www”)将为您提供一个文件句柄,其中写入最终位于本地主机上的 Web 服务器上只是一个。

A socket, in C parlance, is a data structure in kernel space, corresponding to one end-point of a UDP or TCP session (I am using session very loosely when talking about UDP). It's normally associated with one single port number on the local side and seldom more than one "well-known" number on either side of the session.

A "raw socket" is an end-point on, more or less, the physical transport. They're seldom used in applications programming, but sometimes used for various diagnostic things (traceroute, ping, possibly others) and may required elevated privileges to open.

Sockets are, in their nature, a binary octet-transport. It is not uncommon to treat sockets (TCP sockets, at least) as being text-based streams.

I have not yet seen a programming model that doesn't involve something like sockets, if you dig deep enough, but there have certainly been other models of doing networking. The "/net/" pseudo-filesystem, where opening "/net/127.0.0.0.1/tcp/80" (or "tcp/www") would give you a file handle where writes end up on a web server on localhost is but one.

烂柯人 2024-10-21 10:08:40

假设您的电脑在家中,并且您打开了两个浏览器窗口。

一个查看facebook 网站,另一个查看Yahoo 网站

与 facebook 的连接将是:

您的 PC – IP1+端口 30200 ——– facebook IP2 +端口 80(标准端口)

组合 IP1+30200 = 客户端计算机上的套接字 code> 和 IP2 + 端口 80 = facebook 服务器上的目标套接字

与 Yahoo 的连接将是:

您的 PC – IP1+端口 60401 ——–Yahoo IP3 +端口 80(标准端口)

组合 IP1+60401 = 客户端计算机上的套接字 code> 和 IP3 + 端口 80 = Yahoo 服务器上的目标套接字

Suppose your PC at home, and you have two browser windows open.

One looking at the facebook website, and the other at the Yahoo website.

The connection to facebook would be:

Your PC – IP1+port 30200 ——– facebook IP2 +port 80 (standard port)

The combination IP1+30200 = the socket on the client computer and IP2 + port 80 = destination socket on the facebook server.

The connection to Yahoo would be:

your PC – IP1+port 60401 ——–Yahoo IP3 +port 80 (standard port)

The combination IP1+60401 = the socket on the client computer andIP3 + port 80 = destination socket on the Yahoo server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文