Learning Python 32 days (socket, tcp protocol)

  Rye 31 days, until finally network programming stage, a lot of basic knowledge of the principles and ignorant mind, feeling into the new world.

  First, the client \ server architecture

  1. Hardware C / S architecture (printer) 2. Software C / S architecture   

  2. The Internet is everywhere C / S structure   

  Such as pornographic websites is the server, your browser is the client (S architecture is also a B / C / S architecture)   

  Tencent as a server to provide your video, you have the next Tencent video client to see its video) the relationship between C / S structure and the socket:

  Learn socket is to complete the development of C / S architecture

  

  Second, the Internet Protocol

  1. How to socket-based programming, to develop one of its own C / S software architecture

  2.C / software (application layer) S is a communication network architecture based on

  3. Core network, that is a bunch of protocol, the protocol that is the standard, you want to develop a software-based network communication, you must follow these standards.

  4. Let's start with research from these standards, open our socket programming Tour

 

 

   Then our great socket to do anything at all? See below

 

 

 Two, socket in the end is what is it?

  Socket is the application layer and the intermediate software TCP / IP protocol suite to communicate abstraction layer, which is a set of interfaces. In design mode, Socket is actually a facade pattern, it is the complexity of TCP / IP protocol suite hidden behind the Socket interface for users, a simple interface is all set, let Socket to organize data in order to comply with the specified protocol.

  So, we do not need in-depth understanding of tcp / udp protocol, socket has a good package for us, we just need to follow the provisions of the socket to program, write a program that follows the natural tcp / udp standards

Third, on the nesting word

  Sockets originated in the University of California, Berkeley version of the Unix 20 1970, that is, they say, BSD Unix. Therefore, it is also sometimes called the sockets "Berkeley sockets" or "BSD sockets." At first, the socket is designed for use in communication between multiple applications on the same host. This is also known as inter-process communication, or IPC. Sockets There are two (or known to have two races), are based on file type and network-based type.

  Socket Family name: AF_UNIX

  unix everything is a file, the file socket calls is based on the underlying file system access to data, two sockets processes running on the same machine, communication can be done by accessing the same file system indirect

  Socket Family name: AF_INET

  (AF_INET6 also be used for ipv6, there are some other address families, but they are either used only for a certain platform, or that have been abandoned or rarely used, or is simply not true, all addresses family, AF_INET is the most widely used one, python supports a variety of address family, but because we only care about network programming, so most of the time I use it only AF_INET)

  A-life scenarios. You want to call a friend, first dial telephone brought a friend after hearing the phone ring, then you and your friends established a connection, you can speak. And other end of the exchange, hang up the phone to end this conversation.  Life scenarios to explain this works.

  

 

   Such a process is roughly

 

Four, socket achieve a simple transport

Client:

import  socket

your phone does = socket.socket, (socket.AF_INET, socket.SOCK_STREAM),

phone.connect (( ' 127.0.1.11 ' , 8000)) # dial telephone 

phone.send ( ' Hello ' .encode ( ' UTF-. 8 ' )) # Message 
Data = phone.recv (1024 )
 Print ( ' yield message sent by the server side: ' , Data)

Server:

import socket

Phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # buy mobile phones 
phone.bind (( ' 127.0.1.11 ' , 8000)) # bound phone card 
phone.listen (5) # Power 
Print ( ' --- -> ' )
Conn, addr = phone.accept () # like telephone 

MSG = conn.recv (1024) # received message 
Print ( ' client is sent a message: ' , MSG)
conn.send (msg.upper ()) # Message

conn.close()
phone.close()

Five, three-way handshake tcp agreement, four wave

 

 1. The three-way handshake:

  First, the client sends a (client) request to the server SYN, SEQ simultaneous transmission link, the server (server), whether good or bad returns a link seq + 1, then the client returns confirmation link ACK = y + 1

  The whole process will respond to each other, after three eventually establish two-way transmission link

  About listen () is used to store semi-SYN sponsored links, as long as there is no full three-step finish, they are half links, listen which represents the number of parameters is stored in semi-links

  But also because tcp protocol does not distinguish between good and bad responses, it is easy to receive a SYN flood attack (feeling very fast hardware, in fact I consequently do not know)

2. Data transmission:

  What's this have to say, you come to me to transfer

3. Four waving

  When you break a link, often the end of the transfer who should, who launched off after launch, as long as they confirm disconnected, but the two-way transmission, it requires four

  The server is often in order to ensure their normal operation, after completion of the data transfer will automatically disconnect the link, the client acknowledgment, the server to the client will be disconnected, then the client does not send a disconnect request, but the server has initiative to enter TIME_WAIT state and vice versa is the same.

 

  In fact, today is a lot did not quite understand, though himself a little trouble interactive server and client, but a good low ah

  Two task bar,

  1. free or look at the basic computer linux base portion 2. engage in a virtual machine server to play

 

  Today that these, need to pay close attention to digest what tomorrow to try their own way to deal with customers in the server-side delivery parameters, Oops, I feel the program is a small step closer.

  ~~~~~

 

Guess you like

Origin www.cnblogs.com/xiaoyaotx/p/12571814.html