9.5 Networking Basics

9.5 Networking Basics

C / S and B / S structure

c: client s: server client, server, computer to upload qq, micro letter
b: browser s: server browser server, Jingdong, Lynx
bs is the essence of architecture cs

s side:
a fixed IP
2 has been stabilized in operation, concurrent

osi seven agreements

Internet protocol according to different functions into seven or osi tcp / ip five or tcp / ip four

img

tcp / ip five models to explain

We will be the application layer, presentation layer, session layer and application layer to make, from the perspective of tcp / ip protocols to illustrate five of the origin and function of each layer, each layer of clear understanding of the main agreement on the principles of the Internet communications.

First, the user perceives only the uppermost layer of the application layer, each layer from top to bottom are dependent on the next level, so we began to cut from one the most, is better understood

Physical layer:

010101 electrical signal

data link layer:

The electrical physical layer packets, each set of data called a message / data frames, each data frame is divided into: a header and a data head data of two parts
- each data packet, a header and a data portion by
- Head: fixed 18 byte 6: sender address / 6: receiver address / 6: data type
-mac address: sender, receiver address, that address mac
- each card has a unique address mac: 12 hexadecimal represent (top six vendors is number six is the number of lines)
- broadcasting: communication within the same LAN, there will be a broadcast storm

Network layer :

-ip: ipv4: 32-bit binary represented: dotted decimal notation from 0.0.0.0 to 255.255.255.255, the range is limited, not shown all the network devices, so there IPv6
- Subnet Mask: is determined by the subnet mask and two ip ip is in the same network segment, do a bitwise address and subnet mask by ip
ip address: 172.16.10.1: 10101100.00010000.00001010.000000001
subnet Mask: 255.255.255.0 : 11111111.11111111.11111111.000000000
bitwise aND: 172.16.10.0 10101100.00010000.00001010.000000000
-172.16.10.10 / 24-ip address and subnet mask contains
-ip there with mac conversion relationship
- host 172.16.10.10/24 access 172.16. 10.11 / 24
- the ARP protocol: send a broadcast packet, the destination hosts mac address
-mac address learning: mac address mapping table and the ip address
- will at first received ip / mac map add one data { '172.16.10.11 ": ddsadfgegsdgsdg}
- any kind of protocol has its header and content

Transport Layer:

tcp agreement:
- three-way handshake, four wave (focus, Figure), how the connection is established, how to send data to ensure reliable, disconnect how to disconnect, there is a state in the process of establishing a connection

img

​ -服务的如果看到大量的syn_rcvd状态
​ -dos和ddos攻击:拒绝服务攻击,分布式的拒绝服务攻击
​ -端口号:端口范围0-65535,0-1023为系统占用端口
​ -udp协议:
​ -发送,不需要要响应,所以数据不可以靠,(看视频)
​ -端口
​ -通过ip+子网掩码唯一确定一台设备
​ -通过ip+子网掩码唯+端口号唯一确定一个软件
​ -半连接池

应用层:

​ -应用层由来:用户使用的都是应用程序,均工作于应用层,互联网是开发的,大家都可以开发自己的应用程序,数据多种多样,必须规定好数据的组织形式
​ -应用层功能:规定应用程序的数据格式。

上网流程分析:

​ -在浏览器输入www.baidu.com
​ -会取dns服务器通过域名解析成ip地址
​ -向ip+端口号这个地址发送请求,就会访问到百度的服务器

socket

能够唯一标示网络中的进程后,它们就可以利用socket进行通信了,什么是socket呢?我们经常把socket翻译为套接字,socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信。

Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。

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.

img

socket workflow

Start talking about the server side. Server initialized Socket, then the port binding (bind), listens to port (listen), accept blocked calls, waiting for client connections. At this point if you have a client initiates a Socket, then connect to the server (connect), if the connection is successful, then connect the client and the server is established. The client sends a data request, the server receives the request and processes the request, and then sends the response data to the client, the client reads the data, and finally close the connection, ends of an interaction

img

scoket small programming examples

# server

import socket

sk = socket.socket()  # 创建socket对象
sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)  # 为了关闭server端不报错

sk.bind(('127.0.0.1',8080))  # 绑定server端ip和端口号

sk.listen(5)  # 监听

conn,addr = sk.accept()  # 拿到连接和地址

while True:
    ret = conn.recv(1024).decode('utf8')  # 接收client端传输的数据
    print(ret)
    res = conn.send(bytes(ret.upper(),encoding='utf8'))  # 向client端发送数据
    
conn.close()  # 关闭资源
sk.close()  # 关闭资源
# client

import socket

sk = socket.socket()
sk.connect(('127.0.0.1',8080))

while True:
    info = input('>>>>>>')
    sk.send(bytes(info, encoding='utf8'))
    ret = sk.recv(1024).decode('utf8')
    print(ret)

sk.close()   # 关闭资源

Guess you like

Origin www.cnblogs.com/dadazunzhe/p/11495411.html