了解套接字基础知识
我一直在阅读基本的网络编程知识,但是很难找到关于套接字到底是什么以及它与 OSI 或 TCP/IP 堆栈如何相关的直接解释。
有人可以向我解释一下什么是套接字吗?它是程序员或 API 定义的数据结构,还是网卡上的硬件设备?
上述网络模型的哪些层处理“原始”套接字?传输层?网络层?
就它们之间传递的数据而言,套接字是基于文本的还是二进制的?
是否有基于套接字的网络编程的替代方案?或者所有网络应用程序都使用某种形式的套接字吗?
如果我能读到这么多,我应该对我正在阅读的其他内容有一个非常清晰的理解。感谢您的帮助!
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.
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?
What layers of the mentioned network models deal with "raw" sockets? Transport layer? Network layer?
In terms of the data they pass between them, are socket text-based or binary?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简短的回答:
Short answers:
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.
用 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.
假设您的电脑在家中,并且您打开了两个浏览器窗口。
一个查看
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 havetwo browser
windows open.One looking at the
facebook website
, and the other at theYahoo 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
andIP2 + 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
.