Home > other >  TCP/IP, UDP, Socket communication concepts
TCP/IP, UDP, Socket communication concepts

Time:09-22

With the development of network technology, TCP/IP, UDP, Socket these words are filled with our ears, so I want to ask: 1. What is the TCP/IP, UDP? 2. Where is the Socket? 3. The Socket? 4. Will you use them? What is the TCP/IP, UDP? TCP/IP (Transmission Control Protocol/Internet Protocol) or between the Transmission Control Protocol/Internet Protocol, is a set of industry standard Protocol, it is designed for wan (WANs), UDP (User Data Protocol, User datagram Protocol) is corresponding to the TCP Protocol, it belongs to one of the TCP/IP Protocol family, here is a picture, show the relationship of these agreements, figure 1 TCP/IP Protocol family including the transport layer, network layer, link layer, now you know the relationship between the TCP/IP and UDP, where is the Socket? In figure 1, we don't see the shadow of the Socket, where it is? Or a figure to speak, the be clear at a glance, figure 2 original Socket here, what is a Socket? Socket is the application layer and the middle of the TCP/IP protocol family communication software abstraction layer, it is a set of interfaces, in the design model, the Socket is a facade pattern, it is the complex of the TCP/IP protocol family hidden behind the Socket interface, for users, a set of simple interface is all, let the Socket to organize data, in order to conform to the specified protocol, will you use them? 前人已经给我们做了好多的事了,网络间的通信也就简单了许多,但毕竟还是有挺多工作要做的,以前听到Socket编程,觉得它是比较高深的编程知识,但是只要弄清Socket编程的工作原理,神秘的面纱也就揭开了, 一个生活中的场景,你要打电话给一个朋友,先拨号,朋友听到电话铃声后提起电话,这时你和你的朋友就建立起了连接,就可以讲话了,等交流结束,挂断电话结束此次交谈, 生活中的场景就解释了这工作原理,也许TCP/IP协议族就是诞生于生活中,这也不一定, 图3 先从服务器端说起,服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接,在这时如果有个客户端初始化一个Socket,然后连接服务器(connect),如果连接成功,这时客户端与服务器端的连接就建立了,客户端发送数据请求,服务器端接收请求并处理请求,然后把回应数据发送给客户端,客户端读取数据,最后关闭连接,一次交互结束,============================================我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页 时,浏览器的进程怎么与web服务器通信的? When you use QQ chat, QQ process how to process with the server or your friends in QQ communications? It all depends on the socket? What is a socket? What are the type of socket? And the socket of the basic functions, these are all want to introduced in this paper, the main content of this paper is as follows: 1, how to communication between the network in the process? 2, what is a Socket? 3.1, 3, the basic operations of a socket socket () function is 3.2, the bind () function 3.3, listen (), connect () function is 3.4, the accept () function is 3.5, the read (), write () function such as 3.6, the close () function in 4, the socket in the TCP three-way handshake to establish a connection, a 5, four TCP handshake release socket connection 6, an example 1, how to communication between the network in the process? Local interprocess communication (IPC) there are many ways, but it can be summarized as the following four categories: messaging (pipes, FIFO, message queue) synchronous (mutexes and condition variables, read-write lock, documents and write record locks, semaphore) Shared memory (anonymous and named) remote procedure call (RPC and Sun Solaris door) but these are not the subject of this article! We want to discuss is how to communication between the network in the process? The first problem is how to uniquely identifies a process, or communication impossible! Locally can be uniquely identifies the process PID a process, but it doesn't work in the network, the TCP/IP protocol family actually has helped us to solve the problem, the network layer of the "IP address" can be a unique identifier in the network host, and the transport layer "agreement + port" can be uniquely identifies a host of applications (process), so using a triple (IP address, protocol, port) can identify the process of network, the network communication process can use this flag to interact with other process, using TCP/IP applications often use application programming interface (API) : BSD UNIX socket (socket) and UNIX System V TLI (has to be eliminated), to realize the communication between the network process, for now, almost all of the application is using the socket, and now it is the Internet era, network communication in the process is everywhere, that's why I said "everything socket", 2, and what is a socket? We already know in the network above process is the communication through the socket, that what is a socket? Originated in Unix socket, and Unix/Linux is one of the basic philosophy "all files", can use "open open - & gt; Read and write the write/read - & gt; Close close "pattern to the operation, my understanding is a Socket is an implementation of the model, the Socket is a special kind of file, some Socket function is on the action (read/write IO, open, close), these functions we introduced, at the back the Socket the origins of the word was first used in the field of network is released in February 12, 1970, found in the literature of IETF RFC33 authors for Stephen Carr, Steve Crocker and Cerf, according to the computer history museum, were named Croker's wrote:" namespace elements can be called the Socket interface, a Socket interface constitute a connection at one end, and a connection can be entirely by a pair of Socket interface, "computer history museum added:" this earlier than BSD Socket interface definitions for about 12 years, "since the basic operations of a 3, Socket Socket is" open - the write/read - close "an implementation model, then the Socket provides these operations corresponding function interface, the following TCP, for example, introduce a few basic Socket interface function, 3.1, and the Socket () function int Socket (int domain, int type, int protocol); socket函数对应于普通文件的打开操作,普通文件的打开操作返回一个文件描述字,而socket()用于创建一个socket描述符(socket descriptor),它唯一标识一个socket,这个socket描述字跟文件描述字一样,后续的操作都有用到它,把它作为参数,通过它来进行一些读写操作, 正如可以给fopen的传入不同参数值,以打开不同的文件,创建socket的时候,也可以指定不同的参数创建不同的socket描述符,socket函数的三个参数分别为: domain:即协议域,又称为协议族(family),常用的协议族有,AF_INET,AF_INET6,AF_LOCAL(或称AF_UNIX,Unix域socket),AF_ROUTE等等,协议族决定了socket的地址类型,在通信中必须采用对应的地址,如AF_INET决定了要用ipv4地址(32位的)与端口号(16位的)的组合,AF_UNIX决定了要用一个绝对路径名作为地址, type:指定socket类型,常用的socket类型有,SOCK_STREAM,SOCK_DGRAM,SOCK_RAW,SOCK_PACKET,SOCK_SEQPACKET等等(socket的类型有哪些?) Protocol, so the name implies, is specified, a common agreement, IPPROTO_TCP, IPPTOTO_UDP, IPPROTO_SCTP, IPPROTO_TIPC, respectively corresponding to the TCP transport protocol, the UDP transport protocol, STCP transport protocol, TIPC transport protocol (the protocol I will separate the opening discussion!) , note: isn't the type and protocol can be arbitrary combinations, such as SOCK_STREAM can not with IPPROTO_UDP combination, when the protocol is 0, automatically select type corresponding to the default protocol type, when we call socket to create a socket, return the socket descriptor which exists in the protocol family (address family, AF_XXX) space, but not a specific address, if you want to assign a value to it an address, you must call the bind () function, or when calling the connect (), listen () when the system will automatically be randomly assigned to a port, and 3.2, the bind () function as mentioned above the bind () function of the family of an address is assigned to a specific address socket, such as the corresponding AF_INET, AF_INET6 is an ipv4 or ipv6 address and port number assigned to a socket, int the bind (int sockfd, const struct sockaddr * addr, socklen_t addrlen); Function of the three parameters are: sockfd: namely the socket descriptor, it is through the socket () function creates, uniquely identifies a socket, the bind () function is to bind a name to this description word, addr: a const struct sockaddr * pointer, pointing to bind to sockfd protocol address, the address structure according to the address when creating the socket address protocol of different and different, such as ipv4 corresponding is: struct sockaddr_in {sa_family_t sin_family; In_port_t sin_port; Struct in_addr sin_addr; }; Struct in_addr {uint32_t s_addr. }; Ipv6 is the corresponding: struct: sockaddr_in6 {sa_family_t sin6_family; In_port_t sin6_port; Uint32_t sin6_flowinfo; Struct in6_addr sin6_addr; Uint32_t sin6_scope_id; }; Struct in6_addr {unsigned char s6_addr [16]. }; Unix domain corresponding is: # define UNIX_PATH_MAX 108 struct sockaddr_un {sa_family_t sun_family; Char sun_path [UNIX_PATH_MAX]; }; Addrlen: is corresponding to the length of the addresses, the server at startup usually binding a known address (such as IP address + port number), is used to provide services, customers can through it to have the server; nullnullnullnullnullnull