socket tcp server | socket tcp client -> the cycle

# ### cycle message 
Import socket 
SK = socket.socket () 
# registered in the network that host 
sk.bind (( "127.0.0.1", 9001)) 
# listening port 
sk.listen () 

the while True: 
	# establish three-way handshake 
	Conn, addr = sk.accept () 
	the while True: 
		RES = conn.recv (1024) 
		# byte stream into the original string 
		RES2 = res.decode ( "UTF-. 8") 
		Print (RES2) 
		strvar = input ( "What you want to send a message to the client-side of it:?") 
		conn.send (strvar.encode ( "UTF-8")) 
		IF strvar == "q": 
			BREAK 

# performed four times and waved 
conn.close () 

# 9000 port refund system 
sk.close ()
# ### client 

Import socket 
SK = socket.socket () 
sk.connect (( "127.0.0.1", 9001)) 

: the while True 
	strvar the INPUT = ( "Please enter the message you want to send") 
	data sent # requires a binary byte stream 
	sk.send (strvar.encode ( "UTF-. 8")) 
	RES = sk.recv (1024)	 
	IF B RES == "Q": 
		BREAK 
	Print (res.decode ( "UTF-. 8") ) 
	
# close the connection 
sk.close ()

  

 

  

Guess you like

Origin www.cnblogs.com/huangjiangyong/p/10960921.html