Introduction:
# Coding = UTF-8 ''' Note: Socket originated from the Unix of the University of California at Berkeley in the 20th century, which is what we often call bsdunix. Sometimes it is also called "BSD socket ". At the beginning, it was designed as a communication between host applications, that is, inter-process communication, also known as IPC. There are two types: file-based and network-based. AF: Address Family PF: valid port numbers of the protocol family are 0 to 65535 ''' # create a TCP/IP Socket socket (socket_family, socket_type, protocol = 0) # et_family socket type socket_type which transmission method is TCP or UDP import sockettcpsocket = socket. socket (socket. af_inet, socket. sock_stream) # create a UDP/IP socket udpsocket = socket. socket (socket. af_inet, socket. sock_dgram) ''' function description server socket function S. BIND () bind the address (host, port number pair) to socket S. listen () starts TCP listening S. accept () passively accepts TCP customers' connections, and waits for the arrival of the connection. client socket function S. connect () Actively initializes the TCP server connection S. the extended version of the connect_ex () connect () function returns an error code when an error occurs, rather than throwing a socket function s with an exception of public usage. recv () receives TCP Data S. send () sends TCP Data S. sendall () completely sends TCP Data S. recvfrom () receives UDP data S. sendto () sends UDP data S. getpeername () connects to the remote address s of the current socket. getsockname () Address of the current socket S. getsockopt () returns the specified socket parameter S. setsockopt () sets the parameter S of the specified socket. close () Close socket blocking-oriented socket methodss. setblocking () sets the socket blocking and non-blocking mode S. setTimeout () sets the timeout time for blocking socket operations S. gettimeout () gets the timeout value for blocking socket operations. File-oriented socket function S. fileno () socket file descriptor S. makefile () creates a file ''' associated with the socket '''
TCP Server
# Coding: UTF-8 # simplest case: one server and one client's TCP Service # client: ''' the running process of the client: create a socket binding port and start loop listening. Accept client connection. Loop communication close client connection close socket ''' # The Server accepts client characters, and let's say one sentence import socketfrom time import ctime # defines several parameters, such as address port number and other host = ''Port = 8808 # Here is the integer bufffersize = 1024 ADDR = (host, Port) # print ADDR # create and bind tcpsockser = socket. socket (socket. af_inet, socket. sock_stream) tcpsockser. BIND (ADDR) tcpsockser. listen (10) While true: Print 'the TCP server I S starting .... ', ctime () tcpsockclt, ADDR = tcpsockser. accept () print 'the client is ', ADDR,' ---- ', ctime () print tcpsockclt. recv (bufffersize) tcpsockclt. send ("server: I am a server! ") Tcpsockclt. Close () tcpsockser. Close ()Tcp client
# Coding: UTF-8 # python2.7 ''' the logic of the client creates a socket to connect to the server for cyclic communication, sending, and receiving messages. Disable socket ''' import socketfrom time import ctimehost = '2017. 0.0.1 'port = 8808 buffersize = 1024 ADDR = (host, Port) tcpsockclt = socket. socket (socket. af_inet, socket. sock_stream) tcpsockclt. connect (ADDR) While true: Print 'client is ready... 'tcpsockclt. send ('client: I am a client! ') Data = tcpsockclt. Recv (buffersize) if not data: Break print data tcpsockclt. Close ()