Detailed examples of python tcp

 

Introduction to TCP

 

TCP Introduction

TCP, Transmission Control Protocol (English: Transmission Control Protocol, abbreviated as TCP) is connection-oriented, reliable transport layer protocol based on a stream of bytes defined by the IETF RFC 793.

TCP communication to go through to create a connection, data transmission, terminates the connection three steps.

TCP communication model, prior to the start of communication, we must first establish relevant links in order to send data, like life, "call" "

 

TCP Features

1. The connection-oriented

Transmission communicating parties must establish a connection to the data, the two sides must be connected to allocate the necessary resources for the system kernel to manage the connection status of the connection and transmission.

Data transmission between the parties can be carried out via this connection.

Upon completion of data exchange, both sides must disconnect this connection, in order to free up system resources.

This connection is one to one, and therefore not suitable for application to broadcast a TCP-based applications use UDP broadcast protocol.

2. Reliable transmission

1) TCP uses to send response mechanism

Each segment TCP sender must obtain the recipient's response was considered the TCP segment transmission success

2) timeout retransmission

After sending end sends a segment to start the timer, if no response is received within the timing to re-send this segment.

To ensure TCP packet loss does not occur, give each packet a sequence number, but also ensures that the serial number sequentially transmitted to the receiving end of the packet reception entity. Then the receiving side has successfully received packet entity sends back a corresponding acknowledgment (the ACK); If the sender entity within a reasonable round-trip time (RTT) acknowledgment is not received, then the corresponding data packet is assumed to have been will be lost retransmission.

3) error checking

TCP checksum with a function to check if data has an error; checksum is calculated for transmission and reception.

4) flow control and congestion management

The master sends flow control to avoid too fast time to completely accept the recipient.

 

Different points of TCP and UDP

  • Connection-oriented (to create a three-way handshake confirmed, the connection has been created only for transmission.)
  • Ordered data transfer
  • Retransmission of lost packets
  • Discard duplicate packets
  • Error-free data transmission
  • Blocking / Flow Control
 

udp communication model

udp communication model, prior to the start of communication, no need to establish relevant links, only need to send data to, like life, "wrote" "

TCP communication model

udp communication model, prior to the start of communication, we must first establish relevant links in order to send data, like life, "call" "

 

tcp client

tcp client server much simpler than that, if the server is required to buy their own cell phone, check the phone card, set the tone, waiting for someone to call the process, then the client will only need to find a phone booth, pick up the phone to dial that is can flow much less

Create a python tcp client code is as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import socket
 
 
def main():
   # 1.创建套接字socket
   tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
   # 2.连接服务器
   dest_ip = input ( "请输入服务器ip:" )
   dest_port = int ( input ( "请输入服务器port:" ))
   dest_addr = (dest_ip, dest_port)
   tcp_socket.connect(dest_addr)
 
   # 3. 接收/发送数据
   send_data = input ( "请输入要发送的数据:" )
   tcp_socket.send(send_data.encode( "utf-8" ))
  
   # 接收服务器发送的数据
   recv_data = tcp_socket.recv( 1024 )
   print (recv_data.decode( "utf-8" ))
 
   # 4. 关闭套接字socket
   tcp_socket.close()
 
if __name__ = = "__main__" :
 
   main()

Ubuntu performed on the terminal:

Start tcp server in the network debugging Assistant interact:

 

tcp server

In the program, if you want to fulfill the functions of a tcp server, you need the process is as follows:

  • socket to create a socket
  • bind binding ip and port
  • listen sockets so that it becomes possible to link passive
  • accept the client's wait link
  • recv / send data transmission and reception
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import socket
 
 
def main():
   # 1.创建套接字
   tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
   # 2.绑定端口
   addr = ("", 8866 )
   tcp_server_socket.bind(addr)
 
   # 3.监听链接
   tcp_server_socket.listen( 128 )
 
   # 4.接收别人的连接
   # client_socket用来为这个客户端服务
   client_socket, client_addr = tcp_server_socket.accept()
  
   # 5.接收对方发送的数据
   recv_data = client_socket.recv( 1024 )
   print ( "接收到的数据:%s" % recv_data.decode( "utf-8" ))
  
   # 6.给对方发送数据
   client_socket.send( "hahaha" .encode( "utf-8" ))
 
   # 7.关闭套接字
   client_socket.close()
   tcp_server_socket.close()
 
 
if __name__ = = "__main__" :
   main()

Ubuntu performed on the terminal:

Start tcp server in the network debugging Assistant interact:

 

tcp Precautions

  1. Under normal circumstances tcp server needs to bind, otherwise the client can not find the server
  2. tcp client is generally not binding, because the initiative is linked server, so just make sure good server ip, port and other information like the local client can randomly
  3. tcp listen socket server through socket can be created out of the initiative will become a passive, this must be done when doing tcp servers
  4. 当客户端需要链接服务器时,就需要使用connect进行链接,udp是不需要链接的而是直接发送,但是tcp必须先链接,只有链接成功才能通信
  5. 当一个tcp客户端连接服务器时,服务器端会有1个新的套接字,这个套接字用来标记这个客户端,单独为这个客户端服务
  6. listen后的套接字是被动套接字,用来接收新的客户端的链接请求的,而accept返回的新套接字是标记这个新客户端的
  7. 关闭listen后的套接字意味着被动套接字关闭了,会导致新的客户端不能够链接服务器,但是之前已经链接成功的客户端正常通信。
  8. 关闭accept返回的套接字意味着这个客户端已经服务完毕
  9. 当客户端的套接字调用close后,服务器端会recv解堵塞,并且返回的长度为0,因此服务器可以通过返回数据的长度来区别客户端是否已经下线

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。 

Guess you like

Origin www.cnblogs.com/leijiangtao/p/11830114.html