Network programming-socket implements simple socket communication

Service

import socket
phone =socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Give the network 
, the communication with the TCP protocol #Port 0-65535 
# 0-1024 is used for the operating system 
phone.bind(( ' 127.0.0.1 ' ,8080 ) )
 #Start listening, the maximum number of pending connections in the table 
phone.listen(5 )
 #Waiting for connection # res=phone.accept() # 
res 
is a socket object and client address 
conn,client_addr= phone.accept()
 #Transceiver Message, 1024 bytes, which means the maximum received 1024 bytes 
data=conn.recv(1024 )
 # conn is responsible for sending and receiving data conn.send(data.upper()) #Close the
 connection conn.close 
()

 Client

import socket
client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
client.connect(( ' 127.0.0.1 ' ,8080 ))
 #Send data 
client.send( ' hello ' .encode( ' utf-8 ' ))
 #Receive data 
data=client.recv(1024 )
 print (data)
 # Close the connection 
client.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324933625&siteId=291194637