Socket Connections

Socket Connections
2010年01月24日
  《Beginning Linux Programming 4th Edition》 Chapter 15: Sockets
  注:我不想把socket译成套接字,因为,个人认为socket更能清淅地表达它的意思。
  You can think of socket connections as telephone calls into a busy building. A call comes into an organization and is answered by a receptionist who puts the call through to the correct department (the server process) and from there to the right person (the server socket). Each incoming call (client) is routed to an appropriate end point and the intervening operators are free to deal with further calls. Before you look at the way socket connections are established in Linux systems, you need to understand how they operate for socket applications that maintain a connection.
  可以把socket连接想像成一座繁忙的大厦里的电话系统。电话打进来后,接线员(服务器进程)接听,并把它转接给相关的个人(服务器socket)。打进来的电话(客户端)被转接给正确的终端后,中介操作者继续等待更多的呼叫。在你学习Linux 系统socket连接的实现之前,你需要理解它们是如何操作socket应用程序来管理连接的。
  First, a server application creates a socket, which like a file descriptor is a resource assigned to the server process and that process alone. The server creates it using the system call socket, and it can’t be shared with other processes.
  首先,服务器应用程序创建一个socket,它就像一个分配给服务器进程的文件描述符。服务器调用函数[b]socket[/b]来创建它,这是此进程专用的,其它进程可不以共享之。
  Next, the server process gives the socket a name. Local sockets are given a filename in the Linux file system, often to be found in /tmp or /usr/tmp. For network sockets, the filename will be a service identifier (port number/access point) relevant to the particular network to which the clients can connect. This identifier allows Linux to route incoming connections specifying a particular port number to the correct server process. For example, a web server typically creates a socket on port 80, an identifier reserved for the purpose. Web browsers know to use port 80 for their HTTP connections to web sites the user wants to read. A socket is named using the system call bind. The server process then waits for a client to connect to the named socket. The system call, listen, creates a queue for incoming connections. The server can accept them using the system call accept.
  接下来,服务器进程给socket命一个名字。通常,在Linux文件系统中,为本地sockets所命的文件名可以在/tmp或者/usr/tmp目录下找到,网络sockets的命名是一个相关的客户端可以连接的特定网络的服务标识(端口号/访问点)。此标识符允许Linux转发传入的连接指定特定的端口号到正确的服务进程。比如,web服务器通常是在80端口创建一个socket,出于这个目的,80端口的标识符就会被预留出来。web服务器知道在到用户的HTTP连接中如何使用80端口来传送用户想读取的内容。socket的命名是通过调用函数[b]bind[/b]来实现,然后服务进程等待客户连接到这个已经命名的socket。函数[b]listen[/b],创建一个传入连接的队列,服务器可以使用函数[b]accept[/b]来接受这些请求。
  When the server calls accept, a new socket is created that is distinct from the named socket. This new socket is used solely for communication with this particular client. The named socket remains for further connections from other clients. If the server is written appropriately, it can take advantage of multiple connections. A web server will do this so that it can serve pages to many clients at once. For a simple server, further clients wait on the listen queue until the server is ready again.
  服务器调用[b]accept[/b] ,已命名的socket会创建一个与之不同的新socket。这个新的socket用来与特定的客户端通信。已命名的socket继续等待更多的其他的客户端连接。如果服务器写得合适,它可以用来多个连接。web服务器就是这样,它可以同时为许多用户提供页面。做为一个简单的服务器,更多的客户只能在listen队列中等待,直到服务器再次准备好。
  The client side of a socket-based system is more straightforward. The client creates an unnamed socket by calling socket. It then calls connect to establish a connection with the server by using the server’s named socket as an address.
  以socket 基础的客户端就更直接了,客户端调用[b]socke[/b]t函数创建一个无名socket,然后,调用[b]connect[/b]函数将服务器已命名的socket做为地址来实现一个连接。
  Once established, sockets can be used like low-level file descriptors, providing two-way data communications.
  一旦建立,sockets 可以像低级的文件描述符一样,提供双向的数据通讯。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值