当前位置:网站首页>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
边栏推荐
猜你喜欢

Idea hot deployment

CentOS changes MySQL database directory

cf #785(div2) C. Palindrome Basis

*7-2 CCF 2015-09-2 日期计算

『每日一问』ReentrantLock加锁解锁

How can technologists start their personal brand? Exclusive teaching of top five KOLs

Read and write mongodb database files
![[GPLT] 2022 大众情人(floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)
[GPLT] 2022 大众情人(floyd)

数据查询语言(DQL)

粗柳簸箕细柳斗,谁嫌爬虫男人丑 之 异步协程半秒扒光一本小说
随机推荐
最短路问题 Bellman-Ford(单源最短路径)(图解)
Go foundation 1
保姆级Scanner类使用详解
Go基础4
数据分析之numpy基础包
What are stand-alone, cluster and distributed?
初始Flask以及简单地上手应用
~2 ccf 2022-03-1 未初始化警告
在Ubuntu中安装MySQL并创建新用户
Dynamically add multiple tabs and initialize each tab page
【代码源】每日一题 分割(nlogn&n解法)
使用nexus3发布yum私服(离线-内网)
Analysis of concat and group in MySQL_ Use of concat
作业7.21 约瑟夫环问题与进制转换
Data preprocessing
Mongodb installation and use
Week小结
office文件对应的Content-Type类型
PHP介绍
『怎么用』装饰者模式