CSDN专家博客精华版

为人民服务!
  首页  :: 新随笔  :: 管理

回答一位朋友的问题:关于Socket通信的概述

Posted on 2007-12-17 10:52  csdnexpert  阅读(459)  评论(2编辑  收藏  举报

关于Socket通信的阐述

一、什么是Socket


Socket常常被称为套接字,也有的书籍上称为“软插座”。它用于在两个基于TCP/IP协议的应用程序之间相互通信。
你可以通过Socket连接从其他机器读写信息,而不用关系这些信息的传递细节。Socket最早出现在UNIX系统中,它是UNIX系统主要的信息传递方式;在Windows系统中,Socket称为Winsock。


二、Socket通信原理


客户方和服务方
首先,让我们了解Socket通信中的两个基本概念:客户方和服务方。
当两个应用之间需要采用Socket通信时,首先需要在两个应用(他们可能位于同一台机器上,也可能位于不同的机器上)之间建立一个Socket连接。发起呼叫连接请求的一方,称为客户方;接受呼叫连接请求的一方称为服务方。
客户方和服务方是相对的,同一个应用可以既是客户方,也是服务方。


客户方如何呼叫连接
在客户方呼叫连接之前,它必须知道服务方在那里,所以需要知道服务方所在机器的IP地址或者机器名称;通常服务器上有很多应用在运行,客户方具体需要与那一个服务器应用建立连接并且要求享受它提供的服务呢?如果客户方和服务方事先有一个约定就好了,这个约定就是Port(端口号)。也就是说,客户方可以通过服务方所在机器的IP地址或者机器名称和端口号唯一的确定所呼叫的服务方。


服务方如何接受连接
在客户方呼叫之前,服务方必须处于侦听状态,用于侦听是否有客户要求建立连接。一旦服务方收到来自客户方的连接请求,那么,服务方可以根据需要接受连接或者拒绝连接。
连接方式有两种:同步方式(Blocking)和异步方式(noBlocking)。


发送与接收
服务方接受连接之后,会通知客户方,表示连接建立。
客户方可以发送消息给服务方,这里的消息可以是文本,也可以是二进制信息流。那么服务方如何接受来自客户方的消息呢?
原来,当客户方的消息到达服务方端口的时候,会自动触发一个事件(Event)。服务方只要接管该事件,就可以接受来自客户方的消息了。


三、Socket编程


如果采用Delphi或者C++ Builder,那么编程就非常的容易。因为他们已经封装了Socket组件,你只需要设置组件属性和接管事件就可以了。
如果采用VC++ 6.0,则比较繁琐一点。需要完全地采用Windows APIs开发。
你可以在VC++帮助中或者在Delphi的Windows SDK帮助中搜索关键字Socket,可以得到相关的开发资料(在winsock2.h和ws2_32.lib中):
the Windows Sockets specification includes the following Berkeley-style socket routines:
accept1   An incoming connection is acknowledged and associated with an immediately created socket. The original socket is returned to the listening state.
bind   Assign a local name to an unnamed socket.
closesocket1 Remove a socket from the per-process object reference table. Only blocks if SO_LINGER is set with a non-zero timeout on a blocking socket.
connect1  Initiate a connection on the specified socket.
getpeername Retrieve the name of the peer connected to the specified socket.
getsockname Retrieve the local address to which the specified socket is bound.
getsockopt  Retrieve options associated with the specified socket.
htonl2   Convert a 32-bit quantity from host byte order to network byte order.
htons2   Convert a 16-bit quantity from host byte order to network byte order.
inet_addr2  Converts a character string representing a number in the Internet standard ".'' notation to an Internet address value.
inet_ntoa2  Converts an Internet address value to an ASCII string in ".'' notation i.e. "a.b.c.d''.
ioctlsocket  Provide control for sockets.
listen   Listen for incoming connections on a specified socket.
ntohl2   Convert a 32-bit quantity from network byte order to host byte order.
ntohs2   Convert a 16-bit quantity from network byte order to host byte order.
recv1   Receive data from a connected or unconnected socket.
recvfrom1  Receive data from either a connected or unconnected socket.
select1   Perform synchronous I/O multiplexing.
send1   Send data to a connected socket.
sendto1   Send data to either a connected or unconnected socket.
setsockopt  Store options associated with the specified socket.
shutdown  Shut down part of a full-duplex connection.
socket   Create an endpoint for communication and return a socket descriptor.



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=659735