socket server | socket Client -> Package sticky phenomenon

# ### server 
Import socket 
Import Time 
SK = socket.socket () 

# add this sentence before the bind method, you can make a port reuse 
sk.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
# tie set the address port (registered on the network host) 
sk.bind (( "127.0.0.1", 9000)) 
sk.listen () 

Conn, addr = sk.accept () 
conn.send ( ". 6" .encode ( "UTF -8 ")) 
Message =" Hello, " 
conn.send (message.encode (" UTF-. 8 ")) 
# the time.sleep (0.1) 
conn.send (" World ".encode (" UTF-. 8 ")) 


# fourth wave 
conn.Close () 
# refunded port 
sk.close () 


# ### client 
Import socket 
Import Time 
SK = socket.socket () 
sk.connect (( "127.0.0.1", 9000))
9000) )
# time.sleep(0.2)
int = RES0 (sk.recv (. 1) .decode ( "UTF-. 8")) # RES0 ". 6" 
Print (RES0) 
RES1 = sk.recv (RES0) 
Print (RES1) 
# Print (res1.decode ( "UTF -8 ")) 
RES2 = sk.recv (10) 
Print (RES2) 
sk.close () 
---------------------------- -------------------------------------------------- ---------------- 
# ### server 
Import socket 
Import Time 
SK = socket.socket () 

# before the bind method add this sentence, you can make a port reuse 
sk.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
# bind address port (registered on the network host) 
sk.bind (( "127.0.0.1", 9000)) 
sk.listen () 

conn, SK addr = .accept () 
conn.send ( "00.00012 million" .encode ( "UTF-. 8")) 
Message = "Hello,"* 20
conn.send(message.encode("utf-8"))
# time.sleep(0.1)
conn.send("world".encode("utf-8"))


# 四次挥手
conn.close()
# 退还端口
sk.close()

# ### 客户端
import socket
import time
sk = socket.socket()
sk.connect( ("127.0.0.1",9000) )

# time.sleep(0.2)
res0 = int(sk.recv(8).decode("utf-8")) #res0 "6"
print(res0)
res1 = sk.recv(res0)
print(res1)
# print(res1.decode("utf-8"))
res2 = sk.recv(10)
print(res2)
sk.use Close () 
Import struct
Import socket
# ### server
----------------------------------------------- -------------------------------------------------

socket.socket = SK () 
sk.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR,. 1) 
sk.bind (( "127.0.0.1", 9000)) 
sk.listen () 

Conn, addr = sk.accept () 
InP INPUT = ( ">>> MSG:") 
MSG = inp.encode ( "UTF-. 8") 
value of the length of the transmission data # converted by pack, into 4 bytes having a fixed length of 
res = struct.pack ( "I", len (MSG)) 

conn.send (RES) 
# Next, start the real transmission data 
conn.send (MSG) 
conn.send ( "World" .encode ( "UTF-. 8")) 

RES = conn.recv (1024) 
Print (RES) 
Print (res.decode ( "UTF-8")) 
# fourth wave 
conn.Close () 
# refunded port 
sk.close()

# ### 客户端
import socket
import struct
import time
sk = socket.socket()
sk.connect (( "127.0.0.1", 9000)) 
the time.sleep (0.1) 
# 4-byte length accepted, he is actually the number to be transmitted to the conversion. 
n-sk.recv = (. 4) 
n-= struct.unpack ( "I", n-) [0] 
Print (n-) 

# next receive data sent from the server 
RES1 = sk.recv (n-) 
Print (res1.decode ( "UTF-. 8")) 
RES2 = sk.recv (1024) 
Print (res2.decode ( "UTF-. 8")) 

# spaces not ascii encoding, attention. 
sk.send (B "i_love_you") 

# close the connection 
sk.close ()

  

Guess you like

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