Socket sockets Standard:
Located in: Application Layer-(Socket abstraction layer)--between transport layers
#SErvreImportSocketphone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)#First step: Initialize the socketPhone.bind (('127.0.0.1', 8080))#Step Two: Bind the portPhone.listen (5)#Step three: Listening portsconn, client_add = Phone.accept ()#Fourth Step: Receive connect connection request from client whileTrue:#Data Loops Try: Data=CONN.RECV (1024)#Fifth Step: Receive the data from the client information ifLen (data) = = 0: BreakConn.send (Data.upper ())#Sixth Step: reply the message to the client Print(data)exceptConnectionreseterror: Break#Transfer data offConn.close ()#Seventh Step: Transfer data off#Connection Close#phone.close () #第八步: Connection closed#for the client and the server to interact with each other to extend the time: #writes send and receive data to a while (that is, to establish a communication loop)
#ClientImportSocketphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.connect (('127.0.0.1', 8080)) whiletrue:msg=input ('Enter what is sent:') ifLen (msg) = = 0:Continuephone.send (Msg.encode ('Utf-8'))#can only pass binary bytes, so to encodeDATA=PHONE.RECV (1024) Print(data) Phone.close ()
Connect loops + Communication loops with:
#ServreImportSocketphone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)#First step: Initialize the socket#Phone.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1) reuse the port, in order to be disconnected when the system port is not released in time may be error phone.bind (‘127.0.0.1', 8080))#Step two: Bind Port Phone.listen (5)#Step Three: Listening portsWhile True:#Connection Loop conn, Client_add = Phone.accept ()#Fourth Step: Receive connect connection request from clientWhile True:#Data loopsTry: DATA=CONN.RECV (1024)#Fifth step: Receive the data from the client informationIf Len (data) = = 0:BreakConn.send (Data.upper ())#Sixth step: Reply the message to the clientPrint(data)ExceptConnectionreseterror:Break#Transmit data off Conn.close ()#Seventh step: Transfer data off#Connection Close#Phone.close () #第八步: Connection closed#To be able to interact with multiple users:#A loop is added to the Accept () receive message on the server-side communication loop, which loops the connection to multiple clients‘‘‘2 Error condition ending method: 1) The client shuts down directly: The server is in the recv () receive state, because the client is disconnected directly, so will be reported the connection error solution is to add a try except CONNECTIONRESETERROR2) The client enters a carriage return: The server is in the accept state and does not receive any information from the client, so it will continue to wait for if Len (msg) = = 0:continue‘‘‘----------------------------------------------------------------------------------------#Client (no change)ImportSocketphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.connect (('127.0.0.1', 8080))while true:msg=input (' input sent content:') If Len (msg) = = 0: continue phone.send (Msg.encode ('utf-8')) # can only pass binary bytes, so encode data=phone.recv ( 1024x768) print(data) phone.close ()
Python Socket Socket BYTE