I. Socket concept 1. Understanding sockets
The two programs on the network realize the exchange of data through a two-way communication connection, and one end of the connection is called a socket.
Establish a network communication connection with at least a pair of port numbers (sockets). The socket is essentially a programming interface (API), a TCP/IP encapsulation, and TCP/IP is also available
For programmers to do network development of the interface, this is the socket programming interface; HTTP is a sedan that provides a specific form of encapsulation or display of data;
The socket is the engine that provides the ability to communicate over the network.
The English literal of the socket is "hole" or "socket". As the BSD UNIX process communication mechanism, take the latter one meaning. Often also referred to as "sockets," which describe
The IP address and port, which is a handle to a communication chain, can be used to implement communication between different virtual machines or different computers. Hosts on the internet are typically shipped
Multiple service software is available and several services are provided. Each service opens a socket and binds to a port, and the different ports correspond to different
The service. The socket is like a porous socket, as it is in English. A host is like a room filled with various sockets, each with a number,
Some sockets offer 220 vac, some offer 110 vac, and others offer cable TV programs. The customer software plugs into a different number of sockets,
will be able to get different services.
A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, thesocket is actually a façade mode,
It hides the complex TCP/IP protocol family behind the socket interface, and for the user, a simple set of interfaces is all, allowing the socket to organize the data to conform to the specified protocol.
2.socket Concept Socket Layer
Understanding sockets
A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, thesocket is actually a façade mode, which puts the complex TCP/IP association
The family is hidden behind the socket interface, for the user, a simple set of interfaces is all, let the socket to organize the data to meet the specified protocol.
3. History of Socket sockets
Sockets originated in the the 1970s UC Berkeley version of Unix, which is what people call BSD Unix. So sometimes people also refer to sockets as "Berkeley sockets
"or" BSD sockets ". Initially, sockets are designed to be used for communication between multiple applications on the same host. This is also called interprocess communication, or IPC. There are two types of sockets (or
There are two races), which are file-based and web-based, respectively.
Socket family based on file type
Socket family name: Af_unix
Unix all files, file-based sockets are called by the underlying file system to fetch data, two sockets process running on the same machine, you can access the same file system to complete the communication indirectly
Socket family based on network type
Socket family name: af_inet
(There are also af_inet6 used for IPv6 and some other address families, but they are either used only on a platform, or have been discarded, or are rarely used, or are not
Implementation, Af_inet is the most widely used one in all address families, Python supports many address families, but since we only care about network programming, most of the time I use only af_inet)
4.TCP Protocol and UDP protocol
TCP(transmission Control Protocol) Reliable, connection-oriented protocol (eg: call), low transmission efficiency full duplex communication (send cache & receive cache), byte stream oriented. Using TCP
Application: Web browser, e-mail, file transfer program.
UDP(User Datagram Protocol) unreliable, non-connected services, high transmission efficiency (before sending are chosen adequately), a pair of one or one-to-many, many-to-one, many-to-many, message-oriented, to do their best to serve
Traffic, no congestion control. Applications that use UDP: Domain Name System (DNS), video streaming, voice over IP (VoIP).
I know that you don't understand that, directly.
Two. Socket (socket) First Use 1. TCP protocol-based socket
TCP is link-based, you must start the server, and then start the client to link the server
Server side:
1 ImportSocket2SK =Socket.socket ()3Sk.bind (('127.0.0.1', 8080))#bind an address to a socket4Sk.listen ()#Monitoring Links5CONN,ADDR = Sk.accept ()#Accept Client Links6RET = CONN.RECV (1024)#Receiving client Information7 Print(ret)#Print Client Information8Conn.send (b'Hi')#send a message to the client9Conn.close ()#close the client socketTenSk.close ()#to turn off server sockets (optional)
Server Side
Client Side
1 ImportSocket2SK = Socket.socket ()#Create a customer socket3Sk.connect (('127.0.0.1', 8898))#try to connect to the server4Sk.send (b'hello!')5RET = SK.RECV (1024)#dialog (send/Receive)6 Print(ret)7Sk.close ()#Close Client sockets
Client Side
You may experience the following issues when restarting the server:
Workaround:
1 #Add a socket configuration to reuse IP and ports2 ImportSocket3 fromSocketImportsol_socket,so_reuseaddr4SK =Socket.socket ()5Sk.setsockopt (sol_socket,so_reuseaddr,1)#That 's it, in front of BIND plus6Sk.bind (('127.0.0.1', 8080))#bind an address to a socket7Sk.listen ()#Monitoring Links8CONN,ADDR = Sk.accept ()#Accept Client Links9RET = CONN.RECV (1024)#Receiving client InformationTen Print(ret)#Print Client Information OneConn.send (b'Hi')#send a message to the client AConn.close ()#close the client socket -Sk.close ()#to turn off server sockets (optional)
View Code2. Socket based on UDP protocol
Server side:
1 ImportSocket2Udp_sk = Socket.socket (type=socket. SOCK_DGRAM)#create a socket for a server3Udp_sk.bind (('127.0.0.1', 9000))#Binding Server Sockets4MSG,ADDR = Udp_sk.recvfrom (1024)5 Print(msg)6Udp_sk.sendto (b'Hi', addr)#dialog (receive and send)7Udp_sk.close ()#Turn off server sockets
Server Side
Client side:
1 import socket 2 ip_port= ( " 127.0.0.1 ", 9000 3 Udp_sk=socket.socket (Type=socket. SOCK_DGRAM) 4 udp_sk.sendto (b " Span style= "COLOR: #800000" >hello " ,ip_ Port) 5 back_msg,addr=udp_sk.recvfrom (1024 ) 6 print (Back_msg.decode ( " utf-8 "), addr)
Client SideDetailed description of 3.socket parameters
Socket.socket (Family=af_inet,type=sock_stream,proto=0,fileno=none)
创建socket对象的参数说明:
Family |
The address series should be af_inet (the default), Af_inet6,af_unix,af_can or af_rds. (The Af_unix domain is actually using a local socket file to communicate) |
Type |
The socket type should be SOCK_STREAM (the default), Sock_dgram,sock_raw, or one of the other Sock_ constants. Sock_stream is a TCP-based, guaranteed (that is, the data is guaranteed to be transmitted correctly to each other) socket for connection, which is used for data transmission. sock_dgram is a UDP-based, unsecured message-oriented socket that is used to send broadcast information over the network. |
Proto |
The protocol number is usually zero, can be omitted, or, in the case of an address family of Af_can, the protocol should be one of Can_raw or can_bcm. |
Fileno |
If Fileno is specified, the other parameters are ignored, causing the socket with the specified file descriptor to be returned. Unlike SOCKET.FROMFD (), Fileno returns the same socket, not duplicates. This may help to close a separate outlet using Socket.close (). |
Python Foundation--socket