Network Communication Standard---network protocol
Internet Protocol--osi Seven layer protocol
Five-tier protocol: Application layer: Application layer, presentation layer, Session layer
Transport Layer: Transport Layer
Network layer: Network layer
Data Link layer: Data link layer
Physical Layer: Physical layer
The physical layer is used to send electrical signals.
Data link layer run protocol, grouping standard. Ethernet Ethernet protocol, which specifies how electrical signals are grouped
After the electrical signal is a bunch of data, as long as the rules of how to group to get the correct data
Ethernet Regulations
A set of electrical signals forms a datagram called a frame
Each data frame is divided into: header head and Data two sections
You must have a network card to access the Internet
Each network card Factory has its own MAC address the first six vendors address after a few of their own address
Mac is unique in the world
In terms of communication, the Ethernet protocol has a physical layer and a data link layer that can already communicate.
Ethernet has a broadcast mode of operation
Broadcast packets can only be transmitted in the LAN, or the data volume is too general to cause network failure
Network Layer---IP protocol ipv4/ipv6
IP packets also fall into the head and data part of the IP header meaning the same as Ethernet
An address is obtained from the middle of the IP address and IP subnet mask, which is the network segment address
The bitwise AND operation of both the 1 is the 12 binary IP address and subnet mask to calculate
Follow this network segment address to find the range, and then find this machine.
Ipv4/ipv6 gateways are used to help cross a subnet's gateway
Cross subnets to communicate with gateways and gateways
Send the data to the gateway using the Ethernet broadcast, but get the MAC address of the gateway first
But we know only the IP address of the gateway
Therefore, the ARP protocol is required, and the IP address is used to resolve the MAC address within the LAN. This gets the MAC address with the gateway's IP address
To give the data to the gateway
If the sending end host sends a destination MAC address of FF.FF.FF.FF.FF.FF then it knows that the sender needs to get the Mac
But the host that accepts the data doesn't know if it wants a MAC address, so it needs to send an IP address so he knows he needs it.
Inside the ARP protocol.
The same subnet address is used in the broadcast mode of Ethernet
IP address and MAC address to determine where a machine is located
--------------------------------------------------------------------------------------------------------------- ----------------------------
Transport Layer runs two protocols: TCP protocol and UDP protocol get port determine which software of this machine is to be transmitted
IP plus port identifies unique software in the world
Which port of the software machine, 65,536 port 1024 is the operating system, behind all its own software
Application layer: HTTPP ftp mai protocol can set its own protocol
Before the data is usually added to the head and then the data
DHCP: Automatically assign IP addresses
DNS: domain Name service resolves URL domain addresses to IP addresses
TCP protocol:
Establish a two-way connection for the interaction of data to be sent.
Mode of operation: C to a request, then s to send a confirmation and a request, then C again send a confirmation
So I sent three times, three handshakes.
In order to confirm whether the data that I sent, you two to respond, so my data to add a sequence
If you send it back, my sequence +1 proves to be responding with my data.
TCP protocol, also known as a reliable protocol, send the package must wait for the other party to confirm receipt of my package responded, only to calculate the success
If there is no response for a period of time to send again, so is a reliable agreement.
Reliable after sending the data does not empty the memory until the response confirmation received by the other party received
UDP protocol does not need to establish a connection, known as an unreliable protocol, the data is sent out to empty memory, unequal to each other's confirmation information.
TCP has a disconnect, UDP does not
Who last confirmed the data who received the first break, four times wave broken link
--------------------------------------------------------------------------------------------------------------- ---------------------
Socket Programming:
One more socket abstraction layer between the application layer and the Transport layer
The transport protocols are packaged and used directly.
Based on the abstraction layer between the application layer and the Transport layer
Using a network-based socket: af--inet
Server-side: First initialize the socket (), and then bind the bind () to the port with the fixed address of the service side, the port to listen listen () See who is linked
Accept () block until there is a client link and connect () to form a team to link
Import Socket
Socket.socket (Socket.af_inet,socket. SOCK_STREAM)
Service side:
Import Socket
#买手机
Phone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)Open up this server end to close
#绑定手机卡
Phone.bind ((' 127.0.0.1 ', 8080)) #127.0.0.1 refers to the IP address of this machine.
#开机
Phone.listen (5) #5代表最大挂起的链接数
#等电话链接
conn,addr=phone.accept ()Wait for establish link accept () wait to establish
print (' Client ip:%s, client port:%s '% (Addr[0],addr[1]))
Data=conn.recv (1024x768)#最大收1024 recv () accepts data, the maximum amount of receive in parentheses
Print (Data.decode (' Utf-8 '))
Conn.send (Data.upper ())Send () sends data
#6, hang up the phone
conn.close ()Established link to close
#7. Shutting down the machine
phone.close ()
Client:
Import socket
#买手机
Phone=socket.socket (Socket.af_inet,socket. SOCK_STREAM) Open this port as
There's no need to bind #绑定手机卡 client, so no.
#开机 don't have to listen.
phone.connect (' 127.0.0.1 ', 8080) actively establishes the link, the front IP address is followed by the port
phone.send (' Hello '. Encode (' Utf-8 ')) send data
DATA=PHONE.RECV (1024)
Print (Data.decode (' Utf-8 '))
#关机
Phone.close () also to shut down the machine
Continuous data interactions, as well as queued links:
Service side:
Import Socket
#买手机
Phone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
#绑定手机卡
phone.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
Phone.bind ((' 192.168.11.52 ', 8080)) #127.0.0.1 refers to the IP address of this machine.
#开机
Phone.listen (5) #5 represents the maximum number of pending links
#等电话链接
print (' starting ... ')
While True:#链接循环
conn,addr=phone.accept ()
print (' Client ip:%s, client port:%s '% (Addr[0],addr[1]))
While True:#通信循环
try: #用到try是因为有时候会发生但无法预知的错误
Data=conn.recv (1024x768)#最大收1024
if not data:break #针对linux
Print (Data.decode (' Utf-8 '))
Conn.send (Data.upper ())
except Connectionreseterror:
Break
#6, hang up the phone
conn.close () #断开连接
#7. Shutting down the machine
phone.close ()
Client:
Import socket
#买手机
Phone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
There's no need to bind #绑定手机卡 client, so no.
#开机 don't have to listen.
#等电话链接
Phone.connect (' 192.168.11.52 ', 8080)
While True:
msg=input (' Please enter: '). Strip ()
phone.send (Msg.encode (' Utf-8 '))
DATA=PHONE.RECV (1024)
Print (Data.decode (' Utf-8 '))
#关机
Phone.close ()
When the client sends an empty message to the server, the server receives a reception that will get stuck.
Python Learning Network Programming--socket