Network Programming [First Part] The socket-based programming protocol tcp

The server - client connections likened to process both sides call

 

A client

Initiative of the party:

(Data transmission to the server after receiving a request ->)> - instantiates a client socket object -> sends a connection request as the server sending the request success message -> (server receives data after a data feedback information to the client, confirming the success of the operation ->) shutdown process

####

- phone call

Buy a mobile phone -> phone -> speak (Send Message) -> Listen opposite speak (received information) -> after the completion of communication hang

Import socket 

# buy a mobile phone - Family Sockets | Port Protocol 
Phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 

# direct call connection server - to address - ip | port number 
phone.connect (( ' 127.0.0.1 ' , 1000 )) 

# after the phone is turned message 
phone.send ( ' Hello ' .encode ( ' UTF-. 8 ' )) 

# receiving feedback information received by a specified amount 
Data = phone.recv (100 )
 Print ( ' information: ' , Data) 

# shutdown 
phone.close ()

 

II. Server

A passive party:

Examples of a socket object -> * Set a listener address ( providing a computer's unique identification -ip + port number ) * -> * Set the maximum number of monitor * ->

Receiving a client connection request -> After a successful connection terminal receiving a client message sent -> send feedback information to the client -> close the connection request (may be more than one connection request) -> no connection request after closing process

####

- phone call

Buy phone -> * buy a phone card (only one device) [provided to the client an address to connect to the server] * -> * Boot (in a listening state, there is a maximum number of listeners) * ->

Take a phone call -> received other information -> Reply to a message -> Hang up the phone after calling -> Off (no monitor)

Import socket 

# buy a mobile phone - Family Sockets | Port Protocol 
Phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 

# buy phone cards - a listen address - ip | port number 
phone.bind (( ' 127.0.0.1 ' , 1000 )) 

# start listening specified maximum number of backlog ** - ** semi connection pool where all calls are waiting in 
phone.listen (. 5 ) 

# , etc. telephone - answer the phone after the phone will be switched on (return sock , addr) the Accept () -> (socket Object, address info) 
# tcp link | client address? 
Link, addr = phone.accept ()     #     ** out a call from the backlog ON ** 

# Telephone number of receiving information to specify a switched receive information 
Data = link.recv (100 )
 Print ( ' information:' , Data) 

# After receiving the information, the feedback information to a client terminal, to tell that he has successfully received 
link.send (Data) 

# completion of all operations Link turned off, and then shut down 
link.close () 
phone.close ()

 

Guess you like

Origin www.cnblogs.com/xxp1624/p/11240789.html