当前位置:网站首页>TCP network application development process
TCP network application development process
2022-07-25 09:29:00 【__ Samual】
Catalog
One 、TCP Introduction of network application development process
1、TCP Introduction of client program development process
2、TCP Introduction of server program development process
3、 ... and 、TCP Server program development
Four 、TCP Attention to web applications
5、 ... and 、socket And send and recv principle
1、 know TCP socket Send and receive buffers
One 、TCP Introduction of network application development process
TCP Network application development is divided into :
TCP Client development
TCP Server program development
explain : Client program refers to the program running on user equipment The server program is a program running on the server device , Dedicated to providing data services for clients
1、TCP Introduction of client program development process
Step-by-step instructions :
1. Create client socket object
2. Establish a connection with the server socket
3. The data
4. receive data
5. Close client socket
2、TCP Introduction of server program development process
Step-by-step instructions :
1. Create a server socket object
2. Binding port number
3. Set listening
4. Waiting to accept the connection request from the client
5. receive data
6. send data
7. Close socket
Two 、TCP Client development
Step-by-step instructions :
1. Create client socket object
2. Establish a connection with the server socket
3. The data
4. receive data
5. Close client socket
Developing code :
import socket
if __name__ == '__main__':
# establish tcp Client socket
# AF_INET:ipv4 Address type
# SOCK_STREAM:tcp Transport protocol type
tcp_client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Establish a connection with the server socket
tcp_client_socket.connect(("192.168.68.160",8080))
send_content = "hello"
# Encode the string process into binary data
send_data = send_content.encode("gbk")
# Send data to the server
tcp_client_socket.send(send_data)
# Receive server data
recv_data = tcp_client_socket.recv(1024)
# Decode the binary
recv_content = recv_data.decode("gbk")
print(recv_content)
# Close socket
tcp_client_socket.close()Connect the network debugging assistant to the client instead of the server , The test results are as follows :
The client connects to the server , After successful connection , The client sends... To the server “hello” Information .

After the server sends information to the client , After the client successfully receives the information , Close the connection with the server , The information sent by the server is displayed in the console of the client .
3、 ... and 、TCP Server program development
import socket
if __name__ == '__main__':
# establish tcp The socket of the server
tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Port number multiplexing
tcp_server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
# Binding port
tcp_server_socket.bind(("",8080))
# Set listening
tcp_server_socket.listen(128)
# Waiting to accept the connection request from the client
# Be careful : When the connection between the client and the server is successful, a new socket will be returned
# tcp_serve It is only responsible for waiting to receive the connection request from the client , This socket is not used for sending and receiving messages
new_client,ip_port = tcp_server_socket.accept()
print(" Client's IP And port number :",ip_port)
# Receiving data from clients
recv_data = new_client.recv(1024)
# Decoding binary data
recv_content = recv_data.decode("gbk")
print(" Receiving data from clients :",recv_content)
# Send data to client
send_content = " The problem is being dealt with "
send_data = send_content.encode("gbk")
new_client.send(send_data)
new_client.close()
# Close the server socket , It means that the socket of the server will no longer wait for the connection request of the client
tcp_server_socket.close()When the server program runs , Use the network debugging assistant as the client , Connect with the server , When the connection is successful , The console of the server will print , Currently connected to the client IP And port number .

When sending messages to the server on the client , After the server receives the message , Will send a message to the client , Finally, close the server .

Four 、TCP Attention to web applications
When TCP The client program wants to start from TCP When the server program communicates, it must first establish a connection
TCP Client programs generally do not need to bind port numbers , Otherwise, the client will actively initiate the establishment of a connection
TCP The server program must bind the port number , Otherwise, the client cannot find this TCP Server program
listen The socket after is a passive socket , Only responsible for receiving connection requests from new clients , Can't send or receive messages
When TCP Client programs and TCP After the server program is successfully connected ,TCP The server will generate a new socket , Receive client messages using this socket
close accept The socket returned means that the communication with the client has been completed
close listen The latter socket means that the socket of the server is closed , It will cause the new client to fail Connect server , But the clients that have been successfully connected before can still communicate normally
When the client's socket calls close after , Server side recv Unblock , The length of the data returned is 0, The server can judge whether the client is offline by the length of the returned data , On the contrary, the server closes the socket , Client's recv It will also unblock , The length of the data returned is 0
5、 ... and 、socket And send and recv principle
1、 know TCP socket Send and receive buffers
When creating a TCP socket Object will have a send buffer and a receive buffer , This send and receive buffer refers to a piece of space in memory
2、send Principle analysis
send You have to send data through your network card , Applications can't send data directly through the network card , It needs to call the operating system interface , in other words , The application sends data to the send buffer first ( A piece of memory ), Then the operating system controls the network card to send the data of the sending buffer to the server network card
3、recv Principle analysis
Application software cannot accept data through network card , It needs to have an operating system interface , The operating system receives data through the network card , Write the received data to the receive buffer ( A piece of memory ), The application program then obtains the data sent by the client from the receiving buffer
边栏推荐
猜你喜欢

@5-1 CCF 2019-12-1 报数

*6-1 CCF 2015-03-2 数字排序

¥1-3 SWUST oj 942: 逆置顺序表

~4.2 ccf 2021-12-1 序列查询

~4.1 剑指 Offer 05. 替换空格
![[GKCTF 2021]easynode](/img/f0/1daf6f83fea66fdefd55608cbddac6.png)
[GKCTF 2021]easynode

Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground

数据控制语言(DCL)

~3 ccf 2022-03-2 出行计划

matplotlib数据可视化三分钟入门,半小时入魔?
随机推荐
Detailed explanation of pipeline pipeline mechanism in redis
Neural network method -- Boston house price (regression problem)
[GKCTF 2021]easynode
MySQL appends a string to the string of a field in the table [easy to understand]
What is anemia model and congestion model?
office文件对应的Content-Type类型
一文搞懂try、catch、finally(包含return)执行流程(全网最详细解析)
多态和接口
Flask SSTI注入学习
Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground
~1 ccf 2022-06-2 寻宝!大冒险!
CentOS changes MySQL database directory
为什么要使用JSON.stringify()和JSON.parse()
Numpy array attribute, shape changing function, basic operation
Understand why we should rewrite the equals method and hashcode method at the same time + example analysis
How can technologists start their personal brand? Exclusive teaching of top five KOLs
[GYCTF2020]Node Game
使用nexus3发布yum私服(离线-内网)
What is the difference between mongodb and redis
作业7.19 顺序表