Network Programming (including TCP / IP protocol / socket)

01, network communication Overview
1. What is a network?
Simple sense, the network is ⼀ kinds of auxiliary double or operator to be able to connect more than ⽅ in ⼯ with ⼀ played.

The official definition of the network are:

Network is a link nodes connected by a plurality of nodes and configured, represents many objects and their interconnections.

Before 1999, it was assumed that the structure of the network are random. But with Barabasi and after Watts in 1999 were found in the scale-free and small-world networks and their findings were published in the world famous "Science", "Nature" magazine and the man realized that the complexity of the network sex. [1]

Network will help text reading, photo viewing, video playback, download transmission, games, chat and other software tools brings extremely rich life and a better enjoyment in terms of text, pictures, sound, video, etc. to the people.

2, we use object network
link for communicating with a plurality ⽅ Use then proceeds, i.e., the data transfer from ⼀ ⽅ ⼀ ⽅ to another.

Using multiple ⽅ Open networks able to be joined to the link, then the data transfer can perform while

Open networks is programmed, so that software on different computers can transfer data into ⾏, communication, i.e., between the processes as shown below:
Here Insert Picture Description
02, the TCP / the IP protocol
1, what is the network communication protocol
network communication protocol is a common network language support communication for the connection of different operating systems and different hardware architecture of the Internet, the network is a universal language.
For example, in our daily lives, some people speak English, and some people say the files, some people speak German, said The language for the same kind of language ⼀ Face to exchange, not ⾏ between the different races in order to solve different language you your language and communication barriers between people in them, now provides Kokusai Use your language and English, which is ⼀ a provision, which is protocol.
As another example, a network operator and a user computer mainframe communication, since the two data terminals using different character sets, and therefore the operator commands entered does not know each other. In order to communicate, each terminal must after a predetermined set of characters respective first converted into a standard character set characters, before entering the transmission network, after the destination terminal, and then converted to the character set of the terminal character. Thus, network communication protocols can also be appreciated that a language to communicate between each computer on the network.
Common network communication protocols are: TCP / IP protocol (the most common), IPX / SPX protocol, NetBEUI protocol.
2. What is the TCP / IP protocol
TCP / IP (Transmission Control Protocol / Internet Protocol, Transmission Control Protocol / Internet Protocol) protocol has the flexibility to support any size of network, almost all connected servers and workstations. When using TCP / IP protocol requires complex setup, each node requires at least one "IP address", a "subnet mask", a "Default Gateway", a "host name", for some beginners He said less convenient to use.
Like say your language and different kind of ⼀ Face communication, as long as there ⼀ kinds zoomed family are recognized to comply with the agreement, then the Open networks communication protocols comply with this computer is called TCP / IP protocol.
Internet ⽹ agreement includes hundreds of protocol standards, but the two most important protocol is TCP and IP protocols, therefore, the Internet home zoomed ⽹ agreement referred TCP / IP protocol
3, TCP / IP layered protocol
TCP / IP reference first network architecture model is used by the ARPANET, divided into four layers (refer to the individual layer divided into 5): a network interface layer (also known as the link layer), a network layer (also known as interconnection layers), the transport layer and application layers, each layer network call its lower layer provided to complete their needs.

Each layer has a corresponding protocol:

Network interface layer protocols: Ethernet 802.3, Token Ring 802.5, X.25, Frame relay, HDLC, PPP ATM and the like.
Network layer protocols: IP (Internet Protocol, Internet Protocol), ICMP (Internet Control Message Protocol , Control Message Protocol), ARP (Address Resolution Protocol, Address Resolution Protocol), RARP (Reverse ARP, Reverse Address Resolution Protocol) .
Transport protocols: TCP (Transmission Control Protocol, Transmission Control Protocol) and UDP (User Datagram protocol, User Datagram Protocol).
Application layer protocols: FTP (File Transfer Protocol, File Transfer Protocol), TELNET (user remote login service agreement), DNS (Domain Name Service, is the Domain Name Service), SMTP (Simple Mail Transfer Protocol , Simple Mail Transfer Protocol), NFS (network file system, network file system), HTTP (hypertext transfer protocol, hypertext transfer protocol).
Here Insert Picture Description

1) IP address

1. Definition: IP address is a host on the Internet to addressing the way, also known as an Internet Protocol address.

2、作用:⽤来在⽹络中标记⼀台电脑的⼀串数字,⽐如192.168.1.1;在本地局域⽹上是惟⼀的。
Here Insert Picture Description

3、分类:每⼀个IP地址包括两部分:⽹络地址和主机地址 ,有如下几种类型:
Here Insert Picture Description
4、私有IP:⽹络IP中,国际规定有⼀部分IP地址是⽤于局域⽹使⽤,也 就是属于私⽹IP,不在公⽹中使⽤的,它们的范围是:

  10.0.0.010.255.255.255    
 
  172.16.0.0172.31.255.255     
 
  192.168.0.0192.168.255.255

5、回环地址IP:

IP地址127.0.0.1 代表本机IP地址,等价于localhost, ⽤ http://127.0.0.1 就可以测试本机中配置的Web服务器。
127.0.0.1,通常被称为本地回环地址(Loop back address),不属于任何一个有类别地址类。它代表设备的本地虚拟接口,所以默认被看作是永远不会宕掉的接口。在windows操作系统中也有相似的定义,所以通常在不安装网卡前就可以ping通这个本地回环地址。一般都会用来检查本地网络协议、基本数据接口等是否正常的。
6、子网掩码:⼦⽹掩码不能单独存在,它必须结合IP地址⼀起使⽤。

⼦⽹掩码的作⽤: 将某个IP地址划分成⽹络地址和主机地址两部分. ⼦⽹掩码的设定

必须遵循⼀定的规则, 用来判断两个IP是否在同一个网络。

                     A: 172.25.254.18/24

                     B: 172.25.0.10/24

7、端口:端⼝就好⼀个房⼦的⻔,是出⼊这间房⼦的必经之路。

端⼝号只有整数,范围是从0到65535;

8、linux中如何查看端口呢?

在命令行输入:cat /etc/services | less 即可查看所需要的查看的服务的端口。

(当然,前提是你的主机安装了你所要查询的服务才能够查询到)

比如:查看http服务的端口:命令行输入:cat /etc/services | less (可以查看规定好的服务对应的端口号)

03、socket编程
1、为什么需要socket?
通过之前的学习, 我们知道本地间通信(IPC)有队列、同步(互斥锁、条件变量等)、以及管道等方法。

但是不同主机之间如果需要通信,则就需要用到socakt.

问题: 本地通过进程PID来唯⼀标识⼀个进程,在⽹络中如何唯⼀标识⼀个进程?

⽹络层的“IP地址”可以唯⼀标识⽹络中的主机,⽽传输层的“协议+端⼝”可以唯⼀标识主机中的应⽤程序(进程)。因此利用IP地址,协议,端⼝就可以标识⽹络的进程。

2、什么是socket?
套接字(socket)是一个抽象层,应用程序可以通过它发送或接收数据,可对其进行像对文件一样的打开、读写和关闭等操作。套接字允许应用程序将I/O插入到网络中,并与网络中的其他应用程序进行通信。网络套接字是IP地址与端口的组合。

简单来说:socket(简称套接字) 是进程间通信的⼀种⽅式, 能实现不同主机间的进程间通信,我们⽹络上各种各样的服务⼤多都是基于 Socket 来完成通信的。比如以下服务等都是基于socket 来完成通信的。

Here Insert Picture Description
3、创建socket
在 Python 中 使⽤socket 模块的函数 socket 就可以完成:

socket.socket(AddressFamily, Type)

1). Address Family:

AF_INET: IPV4⽤于 Internet 进程间通信 AF_INET6: IPV6⽤于 Internet 进程间通信

2). Type:套接字类型

SOCK_STREAM: 流式套接字,主要⽤于 TCP 协议 SOCK_DGRAM: 数据报套接字,主要⽤于 UDP 协 议

import socket
 
# 1. 创建socket对象
#       family: AF_INET(IPv4)   AF_INET6(IPv6)      ========= 网络层协议
#       type: # SOCK_STREAM(TCP)   SOCK_DGRAM(UDP) ========== 传输层协议
#       Linux: 可以认为是一个文件;
socketObj = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
print(socketObj.getsockname())
 
#2.  关闭socket对象
socketObj.close()
 
# 2. socket基本使用
import os
os.system('hostname')
 
hostname = socket.gethostname()
print("主机名:", hostname)
 
print(socket.gethostbyname('localhost'))

04、socket中的UDP与TCP
1、UDP
1)什么是UDP

UDP ⽤户数据报文协议,是⼀个⽆连接的简单的⾯向数据报文的运输层(传输层)协议。UDP不提供可靠性,它只是把应⽤程序传给IP层的数据报发送出去,但 是并不能保证它们能到达⽬的地。
由于UDP在传输数据报前不⽤在客户和服 务器之间建⽴⼀个连接,且没有超时重发等机制,故⽽传输速度很快。
UDP是⼀种⾯向⽆连接的协议,每个数据报都是⼀个独⽴的信息,包括完整 的源地址或⽬的地址,它在⽹络上以任何可能的路径传往⽬的地,因此能否到达⽬的地,到达⽬的地的时间以及内容的正确性都是不能被保证的。
2)UDP的特点

UDP是⾯向⽆连接的通讯协议,UDP数据包括⽬的端⼝号和源端⼝号信息, 由于通讯不需要连接,所以可以实现⼴播发送。
UDP传输数据时有⼤⼩限 制,每个被传输的数据报必须限定在64KB之内。
UDP是⼀个不可靠的协议,发送⽅所发送的数据报并不⼀定以相同的次序到达接收⽅。
Here Insert Picture Description

3)UDP应用场景

UDP是⾯向消息的协议,通信时不需要建⽴连接,数据的传输⾃然是不可靠 的,UDP⼀般⽤于多点通信和实时的数据业务,⽐如:

语⾳⼴播
视频
QQ
TFTP(简单⽂件传送)
SNMP(简单⽹络管理协议)
DNS(域名解释(域名服务器))等等
4)UDP⽹络程序

UDP communications services through a process: either the server or the client, need to first establish a socket () objects to communicate in the future, the server needs to bind an IP or a port. The client wants a message to the client, since UDP is a connectionless, so the client does not need to inform the server, directly enough, after the message is sent, the server replies advantage will make a response to the client, and then on the client will receive, such a communication is over. When a client decides not to send an announcement, then you can end out of socket () the objects. (Of course, if the server when others do not want to send a message, it can also be its own socket () object to close out.) Illustrated as follows:

Here Insert Picture Description

Here Insert Picture Description

experiment:

Server (UDP Server): provision of services
to create a socket bind IP and port objects ---- ---- ---- receive information send a message (a response) ---- Close

The client (UDP Client): access services provided
to create a socket object sends a message ---- ---- ---- receive messages Close

Published 22 original articles · won praise 0 · Views 181

Guess you like

Origin blog.csdn.net/weixin_45734982/article/details/104219134