Want to learn Python programming? Well, let's get these popular. Python Network Programming Tutorial!

Source: Internet
Author: User
Tags server port

Because the Internet Protocol contains hundreds of protocol standards, but the most important two protocols are TCP and IP protocol, so everyone put the Internet Protocol referred to as TCP/IP protocol.

Communication, both parties must know each other's identity, such as email must know the other's e-mail address. The unique identifier for each computer on the Internet is the IP address, similar to 123.123.123.123
。 If a computer is connected to two or more networks, such as a router, it will have two or more IP addresses, so the IP address is actually the computer's network interface, usually the NIC.

Private messages Small 01 can receive 0 basic introductory video set!

Many of the more advanced protocols that are commonly used are based on the TCP protocol, such as the HTTP protocol used for the browser, the SMTP protocol that sends the message, and so on.

An IP packet contains both the source and destination IP addresses, the source and destination ports, in addition to the data to be transferred.

What does a port do? When communicating with two computers, it is not enough to send only the IP address because there are multiple network programs running on the same computer. After an IP packet comes in, whether it is to the browser or QQ, you need a port number to differentiate. Each network program requests a unique port number for the operating system, so that two processes that establish a network connection between the two computers require their respective IP addresses and their respective port numbers.

Client

Most connections are reliable TCP connections. When you create a TCP connection, you initiate a connected call to the client, and the passive response is called the server.

For example, when we visit Sina in a browser, our own computer is the client, and the browser initiates a connection to Sina's server. If all goes well, Sina's server accepts our connection, a TCP connection is built up, and the subsequent communication is to send the Web content.

So, we're going to create a TCP connection-based socket that can do this:

Send the text format must conform to the HTTP standard, if the format is not a problem, then you can receive the data returned by Sina server:

The data received include the HTTP header and the Web page itself, we just need to separate the HTTP header and Web page, the HTTP header print out, the content of the Web page to save to the file:

Server

Server programming is a bit more complicated than client programming.

The server process first binds a port and listens for connections from other clients. If a client connects, the server establishes a socket connection to the client, and the subsequent communication is connected to the socket.

Therefore, the server opens a fixed port (for example, 80) to listen, creating the socket connection for each client connection. Because the server will have a large number of connections from the client, the server is able to differentiate between which client and which clients the socket connection is bound to. A socket relies on 4 items: server address, server port, client address, client port to uniquely identify a socket.

After the connection is established, the server first sends a welcome message and then waits for the client data, plus a hello to send to the client. If the client sends the exit string, the connection is closed directly.

To test this server program, we also need to write a client program:

Note that the client program runs out, and the server program will run forever, you must press CTRL + C to exit the program.

Summary

Socket programming with the TCP protocol is very simple in Python, for the client, to actively connect to the server's IP and the specified port, for the server, to first listen to the specified port, and then, for each new connection, create a thread or process to process. Typically, the server program will run indefinitely.

The same port, after being bound by a socket, cannot be bound by another socket.

Source:

UDP programming

TCP is to establish a reliable connection, and both sides of the communication can send data in the form of a stream. The relative tcp,udp is a non-connection oriented protocol.

When using the UDP protocol, you do not need to establish a connection, just need to know the other side's IP address and port number, you can directly send packets. But I don't know if I can get there.

Although the transmission of data with UDP is unreliable, its advantage is that it is faster than TCP, and the UDP protocol can be used for data that does not require reliable arrival.

Summary

UDP is used similar to TCP, but does not require a connection to be established. In addition, the server-bound UDP port and TCP port do not conflict, that is, UDP 9999 port and TCP 9999 port can be bound separately.

sever.py

Want to learn Python programming? Well, let's get these popular. Python Network Programming Tutorial!

Related Article

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.