通信、端点、IO、文件

什么是网络套接字(Socket)?一时还真不好回答,而且网络上也有各种解释,莫衷一是。下文将以本人所查阅到的资料来说明一下什么是Socket。

Socket定义

  Socket在维基百科的定义:

A network socket is an endpoint of an inter-process communication across a computer network. Today,
most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets.

  而在Oracle官网上的定义是:

A socket is one endpoint of a two-way communication link between two programs running on the network. 

  其实他们想表达的都是这个意思:Socket是网络上两个程序双向通讯连接的端点。

  那我们又该如何理解‘端点(endpoint)’一词呢?

  在Unix/Linux中,一切皆文件。那对于这两个操作系统而言,“端点”就是一个特殊的文件,也就是说Socket实际上就是文件。既然Socket是文件,那就可以用“打开open –> 读写write/read –> 关闭close”模式来操作它,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)

posted @ 2019-06-12 15:51  zzfx  阅读(446)  评论(0编辑  收藏  举报