Socket workflow

Server example:

. 1  Import Socket
 2  
. 3 Phone = socket.socket (socket.AF_INET,
 . 4                        socket.SOCK_STREAM)   # socket.AF_INET representative of network communication, socket.SOCKET_STREAM tcp protocol based on behalf equivalent to buy a mobile phone 
. 5 phone.bind (( ' 127.0. 0.1 ' 8000))   # bind phone number 
6 phone.listen (5)   # indicate there may be a few phone calls and waited. When you're on the phone, you can have several phones can come in to play 
7  Print (123456 )
 8 conn, addr = phone.accept ()   # and so on is a return phone conn represents the return of ancestral link addr represents the address of an incoming call ( other person's phone number) 
9  Print (4545454 )
 10= conn.recv Data (1024)   # 1024 represents length may be received 
. 11  Print ( ' sent by the client information ' , data.decode ())     # Note that transmission and reception are binary socket 
12 is  conn.send (Data .upper ())
 13 is  conn.Close ()
 14 phone.close ()

Client example:

1  Import socket
 2  
3 Phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM)   # buy mobile phones 
4 phone.connect (( ' 127.0.0.1 ' , 8000))   # call 
5 phone.send ( ' caiheng ' .encode ())
 . 6 Data = phone.recv (120 )
 . 7  Print (data.decode ())

 

Guess you like

Origin www.cnblogs.com/ch2020/p/12519338.html