python-study-29

Source: Internet
Author: User

Review

Last Lesson review:     1, c/s        client----- Server        Browser-----Server        which must have:            1, stable operation , has been providing services            to the outside 2, binding a fixed ip+port    2,        the Internet:2.1 Physical Connection Media         2.2  Internet Protocol (Internet Protocol i.e. English of computer interface)    3, TCP         three handshake         semi-connection pool backlog=5             Limit the number of requests at the same time, not the number         of connections Four waves to disconnect    today content:1, the software based on socket writing C/s architecture        based on TCP    protocol 2, TCP protocol sticky packet Problem
View Code

Socket communication based on TCP protocol

ImportSocket#1. Buy Mobile phonePhone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)#TCP is called a streaming protocol, and UDP is called Datagram Protocol Sock_dgram#print (phone)#2. Insert/bind Mobile card#phone.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)Phone.bind (('127.0.0.1', 8080))#3. BootPhone.listen (5)#semi-connection pool, limit the number of requests#4, waiting for the telephone connectionPrint('start ....') whileTrue:#Connecting LoopsConn,client_addr=phone.accept ()#(three-time handshake established two-way connection (client IP, port))    #PRINT (conn)    Print('there is already a connection established successfully', CLIENT_ADDR)#5. Communication: receiving \ Sending messages     whileTrue:#Communication Cycle        Try:            Print('service in the data collection ...') Data=CONN.RECV (1024)#The maximum number of bytes received, no data will be waiting in situ, that is, the amount of data sent by the sender must be >0bytes            #print (' ===> ')            ifLen (data) = = 0: Break #when the client unilaterally disconnects, the server will not receive the empty data            Print('data from the client', data) Conn.send (Data.upper ())exceptConnectionreseterror: Break    #6. Hang up the phone connectionconn.close ()#7. Shut down the machinePhone.close ()
Sever
ImportSocket#1. Buy Mobile phonePhone=Socket.socket (Socket.af_inet,socket. SOCK_STREAM)#print (phone)#2. Dial phonePhone.connect (('127.0.0.1', 8080))#specifying server-side IP and Ports#3, communication: send \ Receive Messages whileTrue:#Communication CycleMsg=input ('>>:'). Strip ()#msg= "    ifLen (msg) = = 0:Continuephone.send (Msg.encode ('Utf-8'))    #print (' has send-----> ')DATA=PHONE.RECV (1024)    #print (' has recv-----> ')    Print(data)#4. ClosePhone.close ()
Client

Access process application Layer data--os (protocol layer: port, plus TCP header, add IP header, find gateway Mac, binary number----network transport----Physical layer, data layer, network layer, Transport layer, application layer)

DNS: Horizontal recursive query for vertical iterative queries

python-study-29

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.