1、Socket 设计网络通讯程序协议是通讯双方的约定, 对于计算机通讯来讲, 协议有高低层之分, 有些协议直接描述物理网络上的通讯( 如以太网协议) , 有些协议描述较复杂抽象的功能( 如TCP/ IP 协议族) , 也就是说协议是分层次的, 各层协议互相协作, 构成了一个整体。TCP/ IP 协议是一个应用于Internet 的非常重要的协议族, 它包括IP 协议、TCP 协议、UDP 协议、ICMP 协议等。对应于ISO( International Standard Organization)组织制定的OSI( Open System Interconnection)网络模型, IP 协议
2、是应用于网络层的负责将信息从一个网络设备传送到另外一个网络设备; 而TCP 协议是应用于传输层的, 这层的作用是在会话层和网络层之间提供信息( 数据) 传输服务, 并校验以确保信息成功到达目标设备。与TCP 协议相对应的是UDP 协议, 这也是一个应用于传输层的协议, 与TCP 协议不同的是该协议是无连接的协议。传输层和网络层在功能上的最大区别是: 前者提供进程通讯能力, 后者不提供进程通讯能力。在进程间通讯的意义上, 网络通讯的最终地址就不仅仅是主机地址, 还包括描述通讯进程的一种标识符。为此,TCP/UDP 提出了协议端口的概念, 用于标识通讯的进程, 端口也就是进程访问网络传输服务的入口
3、点。Internet 中全局地标识一个本地进程需要一个三元组: 协议, 本地地址, 本地端口号。而一个完整的Internet 进程通讯实例是由通讯两端的各一个进程组成, 因此需要一个五元组来标识: 协议, 本地地址, 本地端口号, 远地地址, 远地端口号。这里的本地地址、远地地址是用来标识计算机的, 一般是指计算机的IP 地址。Socket 是一个网络编程接口, 可以适用于不同的网络协议。Windows 环境下使用的Socket 称为Windows Socket, 简称为Winsock。一般的网络通讯采用客户/ 服务器模型, 在这种模型中客户应用程序向服务器程序请求服务, 这种方式隐含了在建立
4、客户机/ 服务器间通讯时的非对称性。使用Socket 时, 也使用客户机/ 服务器的概念, 但是这些概念主要用在面向连接通讯建立网络链路时,好区别出请求连接和接受连接的两端。在建立了连接虚电路后, 网络两端是对等的实体, 客户机和服务器的区分也就不重要了, 两端对等地进行数据通讯。协议族是指确定一组相关的协议族, 如TCP/IP 协议族。因为Socket 接口可以在多个网络上通讯, 除了可以使用Internet 上的TCP/ IP 协议, 还可以使用UNIX 系统上的内部协议和Xerox网络服务等。Socket 类型参数一般是指明程序将Socket 用于字节流传输还是数据报传输, 也就是使用面
5、向连接或使用无连接的网络通讯, 因为在面向连接通讯中, 数据按照一个没有边界的字节流( stream) 传输;而在无连接网络通讯中, 数据按照称为数据报的独立的自包含数据包( datagram) 形式流动。因此事实上这个参数是指明使用的通讯服务类型。在使用面向连接的协议时, 相当于在连接端点之间建立了一个虚电路。也就是说, 在两个端点之间的链路看起来像直接的点到点的连接。因此在使用面向连接的程序中( 如使用TCP 协议) , 服务器段程序的Socket 要先进入一种状态, 而客户端程序的Socket 要使用函数connect ( ) 来请求连接服务器端程序, 如果服务器端的Socket 接收到
6、请求, 可以同意这个请求( 使用accept ( ) 函数) , 这样就可以在两端之间建立一个通讯虚电路了。程序配置好一个Socket 后, 就可以用它来进行网络通讯。网络通讯包括发送和接收两部分。Socket 接口提供了函数来完成这两个功能, 对于Bereley Socket API 提供了10 个函数来传输数据( 5个用于发送, 5 个用于接收) , Winsock 则使用4 个函数来完成功能( 2 个用于发送, 2 个用于接受) 。这些函数可以分为两组, 其中一组是应用于面向连接的Socket, 它们在函数参数中不要求提供目的Socket 的地址( 因为已经建立了连接, 通讯双方都知道了
7、彼此的地址) ; 而另外一组则要求在函数参数中提供目的Socket 的地址, 这组函数使用于无连接的Socket。CSocket 类是从CAsyncSocket 类派生出来的高级对象, 它支持同步操作。同CAsyncSocket 类相比它简化了Socket 编程, 降低了编程难度。而且对于CSocket 来讲, 还提供了高级的Socket 支持, 可以运用MFC 的序列化类来提供和传输序列化协议。现在网络的应用越来越广泛, 对程序员来讲,设计编制网络通讯程序是一个必然趋势, 因此了解网络( 尤其是Internet ) 的协议模型结构, 了解网络程序设计必要的基础知识, 对程序员适应社会的发展是
8、非常有帮助的。附录 英文翻译英文翻译:Socket design the network communication program The agreement is communication both sides of the agreement, for computer communication speaking, the agreement have high and low layer of points, some agreement on the network directly describe physical communication (such as Etherne
9、t protocol), some agreement is more complex than the abstract describe function (such as TCP/IP protocol race), that is the agreement is stratified, each layer of mutual cooperation agreement, constitute a whole.TCP/IP protocol is a used in the Internet very important agreement clan, it includes the
10、 IP protocol, TCP, UDP protocol, ICMP protocol. Corresponding to the ISO (International Standard Organization) of organizing the OSI (Open System Interconnection) network model, the IP protocol is used in the network layer is responsible for the information from one network equipment transmitted to
11、another network equipment; And the TCP protocol is used in the transport layer, the role of this layer is in session layer and network layer provides information between (data) transmission service, and check to ensure successful information to target equipment. And the TCP protocol is paralleled th
12、e UDP protocol, this also is a used in the transport layer protocol, and the TCP protocol of the agreement is different is no connection of the agreement.The transport layer and the network layer on the function of the biggest difference is that in the former the ability to provide process communica
13、tion, the latter does not provide process communication ability. In the process of communication between sense, network communication final address is not host address, also include a description of the process of communication an identifier. Therefore, TCP/UDP put forward the concept of the ports d
14、eal, used to identify the process of communication, port is also the process access network transmission service entry points.The global Internet to identifies a local process need a three yuan group: agreement, local address, local port. And a complete Internet communication process is an example b
15、y communication at the ends of a process the composition, therefore need a five yuan group to identify: agreement, local address, local port, remote address, far away to port. Heres the local address, remote address is used to identify a computer, usually refers to the IP address of the computer.Soc
16、ket is a network programming interface, which can be applied to different network protocol. Windows environment use Socket called Windows Socket, referred to as Winsock.The general network communication with client/server model, in this model the client application server program to request service,
17、 this way in establishing implies the client/server communication between the asymmetry. Use Socket, also use client/server concept, but these major concept in connection with to build up a network communication link, a distinction between good connection and accept the request at both ends of the c
18、onnection. In a connection a virtual circuit, the network at both ends of the equivalence entity, the distinction between the server and the client is not important, equivalent to data communication at both ends.Agreement it refers to determine a group of related agreement family, such as the TCP/IP
19、 protocol families. Because Socket interface can be in more network communication, besides can use the Internet to the TCP/IP protocol, still can use UNIX system of internal agreement and Xerox network service, etc.Socket type parameters is generally used to indicate the program will Socket byte str
20、eam or data transmission for transmission, which USES connection-oriented or use no connected network communication, because in the face of connection communication, data according to a no boundary of word throttling (the stream) transmission; And in no connection network communication, the data acc
21、ording to the independent newspaper called data from contains data packets (datagram) form flow. So in fact this parameter is specified use communication services type.In the use of connectionless protocol, the connection between the endpoint is equivalent to set up a virtual circuit. That is to say
22、, in the two ends of the link between look like direct point-to-point connections. So in use connection-oriented program (such as the use of TCP protocol), server program segment Socket to the first into a state, and a client program Socket to use connect () function to request connection server, if
23、 the server Socket receives request, can agree on the request (use accept () function) that could be set up a communications between the two virtual circuit.Good a Socket configuration program, can use it for network communication. Network communication including send and receive two parts. Socket i
24、nterface provides function to finish the two functions, for Bereley Socket API provides 10 function to transmit data (5, 5 for sent to accept a), Winsock four function is used to complete function (2, 2 for sent to accept). These functions can be divided into two groups, one group is applied in conn
25、ection-oriented Socket, they function parameters are not required to provide objective Socket address (because it has already been a connection, communication both sides know each others address); And the other group, the requirements in the function parameters of the address of the Socket provide o
26、bjective, this set of functions used in connectionless Socket.CSocket from CAsyncSocket kind of derivative senior object, it supports synchronization operation. Compared with CAsyncSocket class it eases the Socket programming, reduce the difficulty of programming. And for CSocket speaking, also offe
27、rs advanced support the Socket, can use MFC serialization class to provide and transmission serialization agreement.Now the application of network is more and more extensive, will tell to programmers, the design of network communication program is an inevitable trend, therefore understand network (especially the Internet) model structure of the agreement, understanding the network programming basics, programmers to adapt societys development to be very helpful.
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100