Socket also called “ Socket ”, Applications usually go through " Socket " Send a request to the network or answer a network request , To enable communication between hosts or processes on a computer .
We write network programming , It belongs to application layer Of , When we write the program , Need to send data to Transport layer , But the communication between the application layer and the transport layer needs to follow the relevant protocols , So it provides Socket To replace the transport layer protocol .Socket Is the application layer and TCP/IP Intermediate software abstraction layer for protocol family communication
Python in , We use it socket() To create a socket , The grammar is as follows
socket.socket(family = AF_INET [,type = SOCK_STREAM])
server:
import socket
# Create a server socket object
server = socket.socket(type= socket.SOCK_DGRAM)
# Bind socket
ip_port = ("127.0.0.1",8080)
server.bind(ip_port)
while True:
# Accept client messages
msg,addr =server.recvfrom(1024)
msg = msg.decode("UTF-8")
print("client>>>:{}".format(msg))
# Reply client
content = input(" Please enter your reply message :")
content = content.encode("UTF-8")
server.sendto(content,addr)
# Close the server socket
server.close()
client:
import socket
# Create an object
client = socket.socket(type=socket.SOCK_DGRAM)
# Send a message to the server
ip_port = ("127.0.0.1",8080)
while True:
content = input(" Please enter your chat content :")
# With UTF-8 Yes content Encoding , Get binary type objects ,encode Encoding
content = content.encode("UTF-8")
# Send a message to the server ,client.sendto send out UDP data , Send data to socket address The form is (ip Address ,port) tuples , Specified remote address
client.sendto(content,ip_port)
# Accept server messages
# Accept the message returned from the server ,client.recvfrom() Accepted UDP data , But back to (data,address),data Is the string containing the received data
msg,addr = client.recvfrom(1024)
msg = msg.decode("UTF-8")
# Print a message
print("server>>>:".format(msg))
# Close socket
client.close()
TCP_server:
import socket
# Create a server socket
TCP_server = socket.socket(type=socket.SOCK_STREAM)
# Binding port
ip_port = ("127.0.0.1",8080![ Insert picture description here ](https://img-blog.csdnimg.cn/20200103181202683.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzAwODIw,size_16,color_FFFFFF,t_70))
TCP_server.bind(ip_port)
TCP_server.listen(5)
print(" Waiting to connect >>>")
# take socket Create socket programming passive , Waiting for customer connection request .
# Block the server , Waiting for the client to connect
client_socket,client_addr = TCP_server.accept()
print("{} Connected ".format(client_addr))
while True:
# Receive messages from clients
recv_msg = client_socket.recv(1024)# receive 1024 Bytes
recv_msg = recv_msg.decode("UTF-8")
# Print client information
print(">>>TCP_client:{}".format(recv_msg))
# Send to client
msg = input(" Messages sent to clients :")
msg = msg.encode("UTF-8")
client_socket.send(msg)
# Close socket
client_socket.close()
tcp_server.close()
TCP_client:
import socket
#1、 Create a socket for the client
TCP_client = socket.socket(type=socket.SOCK_STREAM)
#2、 Connect to the server port
ip_port = ("127.0.0.1",8080)# Server's ip And port
TCP_client.connect(ip_port)
#3、 Send a message to the server
while True:
msg = input(" Send message to server :")
msg = msg.encode("UTF-8")
TCP_client.send(msg)
#4、 Receiving messages from the server
recv_msg = TCP_client.recv(1024)
recv_msg = recv_msg.decode("UTF-8")
#5、 Print the returned message
print(">>>TCP_server:{}".format(recv_msg))
#6、 Close socket
TCP_client.close()
May time slow down , May old friends stay ; May the people you miss say good night to you , May you never be alone . come on. !