Introduction to Socket Communication principle

Source: Internet
Author: User

Introduction to Socket Communication principleWord 1011 Read 1766 comments 2 likes What is a socket

Computers, as the name implies, are used to do calculations. Therefore, input and output are required, and the conditions for calculation are entered, and the calculation results are output. These inputs and outputs can be abstracted as I/O (Input Output).

UNIX computer processing Io is through the abstraction of the file. There is also input and output between different processes of the computer, that is, communication. So this communication is also done through the abstract file descriptor of the file.

On the same computer, the process can communicate this way, if it is a different computer? A network socket (socket) is used to communicate with different computers on the network. Sockets are an abstraction of communication between different computers. He works on an abstraction between the application layer and the transport layer in the TCP/IP protocol. Such as:


Socket.jpg Server Communication

The socket guarantees communication between different computers, that is, network communication. For Web sites, the communication model is communication between client servers. Each of the two terminals establishes a socket object and then transmits the data through the socket object. Typically the server is in a wireless loop waiting for the client to connect:


Socket3.jpgsocket Communication Example

The socket interface is an interface provided by the operating system that invokes the operating system. Of course, high-level language generally also encapsulates a good function interface, below the Python code to write a simple socket server example:

server.py

Import Sockethost =' localhost '# server host Address port =5000# server listener Port buffer_size =2048# Read Data size# Create a Socket sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)# Bind host and Port Sock.bind ((host, Port))# Open Socket Monitor Sock.listen (W) print ' Server start, listening {} '. Format (PORT) while True: # Connection is established, connection is blocked for conn, addr = Sock.accept () while True: # Read data, data is not yet blocked by Conn.recv (buffer_size) If Len (data): print ' Server recv Da TA: {} '. Format (data) conn.send (data) print ' Server send data: {} '. Format (data) else: print ' Server Recv over ' break conn.close () sock.close ()          

client.py

Import Sockethost =' localhost ' PORT =5000buffer_size =1024# Create a client socket sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)# Connect to Server Sock.connect ((HOST, PORT))Try:message = "Hello" # initiate data to server Sock.sendall (message) amount_received = Span class= "Hljs-number" >0 amount_expected = len (message) while amount_received < Amount _expected: # the data returned by the receiving server = SOCK.RECV (10) amount_received + = Len (data) print  ' Client Received: {} '. Format (data) except Socket.errno, E: print  ' socket error: {} ' . Format (e) except Exception, E: print finally: print  ' Closing connection to the server ' Sock.close ()  
TCP Three-time handshake

Python code write sockets are simple. How does the legendary TCP three handshake show? What is a three-time handshake?

    • First grip: First the client sends a SYN, requests a connection,
    • Second grip: The server receives a confirmation and sends a SYN ACK reply
    • Third Grip: The client receives a reply from the server and sends a confirmation to the server to establish the connection.

In the following analogy,

C: About?

S: About

C: Okay.

Date

This establishes a TCP connection session. If the connection is to be disconnected, the approximate process is:


Tcp.png

It is also clear that the three-time handshake of the socket specific process.

    1. The client socket object is blocked after the connect call, and this process sends a SYN.
    2. The server socket object calls the Accept function and blocks until the client sends a SYN, and then sends a SYN and ACK reply
    3. After the client socket object receives the reply sent by the server, it sends an ACK to the servers and returns the connect call to establish the connection.
    4. The server socket object accepts the client's last handshake to determine the ACK to return the Accept function to establish the connection.

At this point, the client and the server socket communication connection is established, the remaining is two end of the connection object to send and receive data, so as to complete network communication.

Introduction to Socket Communication principle

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.