Python-socket Network Programming

Source: Internet
Author: User

Network Programming Socket INTRODUCTION

There are many types of data that are passed between computers, such as video, pictures, files, etc., but there are many protocols on the network that support different types of data, such as HTTP, SMTP, DNS, FTP, SSH, ICMP, and so on. In fact, these protocols are also some data flow exchange, but work in a different way, no matter how they change, are inseparable from the exchange of data, the exchange of data is nothing more than send (send) and receive (receive) two actions, but this sending and receiving is too complex to implement, So the underlying thing is encapsulated, the product of this package is the socket, call socket is called TCP/IP.

Before writing the code to declare that TCP/IP does not require our attention, because the socket is encapsulated, so the first step to declare the socket (import socket), the second step to declare the protocol (socket. TCP/IP), the third step Connect (connect (a.ip,a.port)), the fourth step to send data (scoket.send (hello)), receive data (recv ()), close the connection (Socket.close ()) based on this pseudo-code step, Can implement a simple send side, the receiver (server side), the first step to declare the socket (import socket), the second step to declare the protocol (socket. TCP/IP), the third step listens on the port (Socket.listen (Ip,port)), the fourth step waits for the data (waiting), the fifth step to collect the data (recv ()), and finally can send the data (send ()).

Write a try it!!!

Client code


Service-side code

Test process: First open the server, and then open the client. You can now send and receive only one piece of data.


Just now the English character is transmitted, what if the text character in the transmission is the effect?

Client code

Service-side code

Test:


This error can be resolved by encode and decode, I do not make changes to the service side, only changed the client code, after the completion of the test without error, successfully sent back Chinese characters


The above code can only accept and send data once, so you want to implement multiple send and receive?

Client code


Service-side code

Now that there is no problem using a single-client connection and sending data continuously, what is the effect of trying to disconnect the client? The answer is that the server has entered a dead loop, which is no longer here, why? Since the client disconnects, the server also REVC (1024) to receive the data, has not received the data, so it entered a dead loop. The solution is to add a judgment statement and try it out.

Service-side code


Is that the problem again? The effect we want is that after client A is disconnected, the server then connects to client B, and now the server is disconnected, how can I continue? The server side should continue to keep the connection to the receiving client, so also modify the service-side code. You also need to add a while loop.

Now first open the server Terminal program, and then open two client programs, one of the client disconnects, see the results. You will find that the server is connected to a second client.


Test it again.

What???? The hair was just jammed ... Because you cannot send null, to modify the client code, add a judgment

Python-socket Network Programming

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.