day01- network infrastructure

First, knowledge 

1, socket.socket create a socket, which takes two parameters:

  • Address Family: you can choose AF_INET (Internet for inter-process communication) or AF_UNIX (for inter-process communication on the same machine), commonly used in the actual work AF_INET
  • Type: socket type may be SOCK_STREAM (stream sockets, primarily for the TCP protocol) or SOCK_DGRAM (datagram socket, primarily for UDP protocol

      Sockets use the process and the process is very similar to using a file

      Create a socket -> using the socket / receive data -> Close the socket

2, TCP three-way handshake (a connection) and a four-way handshake (disconnected)

3, TCP long / short connection scenarios

  • Long connections used for frequent operation, point to point communications, and the number of connections can not be too often.

    Each TCP connection requires three-way handshake, it will take time, if each operation is first connected,

    For example: with a long connection to the database connection, if the connection is frequent communication with short socket will cause an error, and frequently socket creation is a waste of resources.

  • And like WEB site http services are generally short link because a long connection to the server, it will consume some resources, like WEB site so often thousands or even millions of client connections will be more connected with the short Province Some resources, concurrent capacity, but each user does not need to use the case of short frequent operation even better.

Second, the code sample

(A) the UDP Code

1、client

2、server

 (B) TCP Code

1、client

2、server

(C) Downloader

Guess you like

Origin www.cnblogs.com/micfox/p/11087788.html