Unix vs BSD vs TCP vs Internet sockets?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 01:23:43

问题


I am reading The Linux Programming Interface and it describes several different types of socket used on Linux:

  • Unix domain
  • Berkeley
  • TCP
  • Internet

One of the things the book said is that if you want to communicate between remote hosts, you couldn't use Unix domain sockets because they are for IPC on the same host. You have to use "Internet" sockets.

However, I am still a little confused how this relates with "TCP" sockets, Berkeley sockets and the other 2? What is their relationship? Why would you have an Internet socket as well as a TCP socket?

In short I am trying to understand all (have I missed any out?) the various different types of Unix sockets and under what circumstances I would use them?


回答1:


A socket is an abstraction. The tag definition used on SO for a socket is as good as any:

An endpoint of a bidirectional inter-process communication flow. This often refers to a process flow over a network connection, but by no means is limited to such.

So from that a major distinction are sockets that (1) use a network and (2) sockets that do not.

Unix domain sockets do not use the network. Their API makes it appear to be (mostly) the same to the developer as a network socket but all the communication is done through the kernel and the sockets are limited to talking to processes on the machine upon which they are running.

Berkeley sockets are what we know as network sockets on POSIX platforms today. In the past there were different lines of Unix development (e.g. Berkeley or BSD, System V or sysV, etc.) Berkeley sockets essentially won in the marketplace and are effectively synonymous with Unix sockets today.

Strictly speaking there isn't a TCP socket. There are network sockets that can communicate using the TCP protocol. It's just a linguist shorthand to refer to them as a TCP socket to distinguish them from a socket using another protocol e.g. UDP, a routing protocol or whatnot.

An "Internet" socket is a mostly a meaningless distinction. It's a socket using a network protocol. That eliminates Unix domain sockets, but most network protocols can be used to communicate on a LAN or the Internet, which is just collection of networks. (Though do note there are protocols specific to local networks as well as those that manage collections of networks.)



来源:https://stackoverflow.com/questions/22897972/unix-vs-bsd-vs-tcp-vs-internet-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!