To understand this network communication is really inseparable from the implementation of the flowchart, see the general many know, think this Bo master painting is good, the address is
Http://www.cnblogs.com/wangcq/p/3520400.html
1. Understand which layer it is in the transmission
2. Understanding its implementation flowchart
So, we can summarize his way of implementation and logic, the code is as follows
Server-side request 1. Create object 2. Bind the socket to the specified address, socket.bing (address) 3.3rd, after binding, the socket must be prepared, and the port is monitored Socket.listen 4. The Accept method of the socket waits for a client to request a connection 5. Processing Phase 6. The server calls the Close method of the socket to close the connection # server import socket address = (' 127.0.0.1 ', 31500) s = socket.socket (socket.af_inet, socket. SOCK_STREAM) # s = Socket.socket () s.bind (address) S.listen (5) ss, addr = s.accept () print ' This is Server ', addr ss.send (' server ') RA = ss.recv (+) print ra ss.close () s.close ()
Client creation steps: 1. Create a socket connection server 2. Connect to Server Socket.connect ((host,port)) 3 using the Connect method of the socket. Customers and servers communicate via send and Recv methods 4. Call the Close method of the socket to communicate import socket address = (' 127.0.0.1 ', 31500) s = Socket.socket ( Socket.af_inet, Socket. SOCK_STREAM) s.connect (address) data = S.RECV (+) print ' This is client ', Data s.send (' client ') S.close ()
Communication results
Python way to implement Scoket communication