Socket programming Steps
- The server creates a socket, binds the address and port, and listens for incoming connections on the port, and receives the incoming connection through the Accept function once the connection comes in.
- The client is also creating a socket. Bind the remote address and port, and then establish the connection to send the data.
Family address cluster, with the first parameter of the socket () function. Mainly has the following several
- Socket.af_unix communication with processes under a single machine
- Socket.af_inet communicates with the server, usually using this.
- SOCKET.AF_INET6 Support IPV6
The SocketType socket type, used with the second parameter of the socket () function, is commonly used to have
- Socket. Sock_stream default, for TCP protocol
- Socket. Sock_dgram for UDP protocol
Service side
ImportSocketserver=socket.socket () Server.bind ('127.0.0.1', 6969) ) Server.listen ()#Start listening .#The conn is a connection instance that the client is connecting to and is generated on the server.conn, addr = Server.accept ()#Wait for session link#print (conn,addr) # Printing to view conn,addr content#receiving Datadata = CONN.RECV (1024)#Print receive DataPrint('recv:', data)#Return DataConn.send (Data.upper ())
Client
ImportSocket#declares the socket type and generates the socket connection object at the same timeClient =Socket.socket ()#Start ConnectionClient.connect (('127.0.0.1', 6969))#Send Message Note 3. Only binary data can be sent in the above versionClient.send (b'Test Socket')#Receive Datadata = CLIENT.RECV (1024)#Print receive DataPrint('recv:', data)#Close ConnectionClient.close ()
Python Socket Communication ~ Simple instance