tcp client program development

Man of few words said, directly to the question

 

One: The client total is divided into five large:

1. Create a client socket

2. to establish a connection and server socket

3. Send data

4. The transmission and reception

5. Close the client socket

 

2: Introduction of socket classes

Introducing the socket module
import socket

Create a client socket object
socket.socket (AddressFamily, Type)

send (data) indicates transmission data, data is binary data

recv (buffersize) represented by the reception data, buffersize length of the data is received per

Three: Program Development

socket Import 


DEF main ():
# create a tcp client sockets
tcp_client_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
# establish and server applications to connect
tcp_client_socket.connect (( "192.168.28.1", 6666 ) )
# ready to send data
send_data = "Hello, you server!." encode ( "GBK")
# transmit data
tcp_client_socket.send (the send_data)
# receiving data, the maximum number of bytes of the received data 1024
recv_data = tcp_client_socket .recv (1024)
# returned binary data is directly sent by the server program
Print (recv_data)
# decodes the data
recv_content = recv_data.decode ( "GBK")
Print ( "as received data server:", recv_content)
# Close the socket
tcp_client_socket.close ()
IF the __name__ == '__main__':
main()

*****************************************

If the spectators do not understand it, I'll give you an example:

Create socket equivalent of buying a mobile phone

And establish a connection to the server equivalent of you who telephoned his

You open up the phone to send data equivalent to want what they are saying

Receive data you received the equivalent of what they are saying

Close the client is equivalent to the call is finished, hang up the phone

This is not good when it comes to understanding some of it?

 

***** ***** NOTE point
Windows encoding format is GBK
MAC OS encoding utf-8 format

Parameter 1: 'AF_INET', the type of address indicates IPv4

Parameter 2: 'SOCK_STREAM', represents TCP transport protocol type

********************************************************************

The first hair blog, blog park is also not very good, if you like it, the future will be updated regularly blog,

Hope bigwigs mouth mercy, the younger brother is a white, what improvements please leave a message;

         Thank you!

 




 

Guess you like

Origin www.cnblogs.com/python-No/p/12014212.html