First, the basics of network programming

 

 

 

Development and infrastructure network concept computer network

Question: What is the network in the end? How is the communication between the computer?

Early: Online

 Ethernet: LAN and switch

broadcast

  Between host "all" mode of communication network in which each signal sent by the host are replicated and forwarded unconditionally, all hosts can receive all the information (regardless of whether you need), because of its no path selection, so its network costs can be very low. Cable television network is a typical broadcast network, we actually receive TV signals of all channels, but only to restore a channel signal into a picture. In the data broadcast networks are also allowed, but its scope is limited to a local area network switcher prohibits the broadcast data through the router, the broadcast data to prevent the affected host a large area.

ip address ip protocol

  • Agreement called network address ip protocol, which defines the address of the called ip address, v4 version i.e. widely used IPv4, which provides 32-bit network address is represented by binary
  • Range 0.0.0.0-255.255.255.255
  • An ip address written as four decimal numbers generally, for example: 172.16.10.1
  • IP addresses reserved

a network-based ~ 10.255.255.255 10.0.0.0 B class net 172.31.255.255 ~ 172.16.0.0 C class net 192.168.0.0 ~ 192.168.255.255

 

mac address

  Source and destination addresses contained in the head Origin: ethernet predetermined internet access devices must have the card, the sender address and an end address of the card receiving means is, i.e. mac address.

  mac address: each NIC are fired on a world unique mac address, a length of 48 binary factory, generally indicated by 12 hexadecimal numbers (the first six numbers is the vendor, after six pipelined number)

arp protocol - correspondence between the query IP address and MAC address

  ARP, i.e., ARP (Address Resolution Protocol), is to obtain a TCP / IP protocol based on the IP address of the physical address.
  Host containing the target IP address broadcasts an ARP request to send information to all hosts on the network, and receiving a return message, in order to determine the physical address of the destination.
  Upon receipt of the return message the IP address and physical address is stored in the local ARP cache and retain a certain time, direct access to the ARP cache when the next request to conserve resources.
  ARP is established, the host on the network can send a self-ARP response message on the basis of mutual trust each host in the network, the other host does not detect the authenticity of the message received when the response message will be credited into the local ARP cache; thus the attacker can send a pseudo host ARP response packet, it sends the information does not reach the expected arrival of the host or the wrong host, which constitutes an ARP spoofing. ARP query command can be used a correspondence relationship the local ARP cache IP addresses and MAC addresses, add or delete a static correspondence relationship. Related agreements have RARP, proxy ARP. NDP used in place of ARP in IPv6. 

WAN router

router

  A router (Router), is connected to the Internet each local area network device, it will automatically selected and set route according to the situation of the channel, the best path, the transmission signal according to the predecessor sequence. Router is the hub of the Internet, "traffic police." Currently router has been widely used in various industries, various grades of products have become the main force of the backbone network interconnection and interoperability with the Internet backbone business backbone to achieve a variety of internal network connection. The main difference between routing switches and switches that occur in the second layer (data link layer) the OSI reference model, routed place in the third layer, i.e. network layer. This difference determines the need to use different routing and switching information during the movement of the control information, so that the two ways to achieve the respective functions are different.
  A router (Router) devices, also known as gateway (Gateway) connected to the plurality of separate logical network for the so-called representative of the logical network is a separate network or a subnet. When the data transfer from one subnet to another, it can be accomplished by routing the router. Thus, having determined the router IP network address and path selection function, it can in a multi-networking environment, establish a flexible connection, the available disparate data packets and media access method to connect various subnets, routers, or only accept the source station information other routers, is a scheme for connecting devices on the network layer.  

local area network

  局域网(Local Area Network,LAN)是指在某一区域内由多台计算机互联成的计算机组。一般是方圆几千米以内。局域网可以实现文件管理、应用软件共享、打印机共享、工作组内的日程安排、电子邮件和传真通信服务等功能。局域网是封闭型的,可以由办公室内的两台计算机组成,也可以由一个公司内的上千台计算机组成。  

子网掩码 

  所谓”子网掩码”,就是表示子网络特征的一个参数。它在形式上等同于IP地址,也是一个32位二进制数字,它的网络部分全部为1,主机部分全部为0。比如,IP地址172.16.10.1,如果已知网络部分是前24位,主机部分是后8位,那么子网络掩码就是11111111.11111111.11111111.00000000,写成十进制就是255.255.255.0。

  知道”子网掩码”,我们就能判断,任意两个IP地址是否处在同一个子网络。方法是将两个IP地址与子网掩码分别进行AND运算(两个数位都为1,运算结果为1,否则为0),然后比较结果是否相同,如果是的话,就表明它们在同一个子网络中,否则就不是。 

例如:
比如,已知IP地址172.16.10.1和172.16.10.2的子网掩码都是255.255.255.0
请问它们是否在同一个子网络?两者与子网掩码分别进行AND运算, 172.16.10.1:10101100.00010000.00001010.000000001 255255.255.255.0:11111111.11111111.11111111.00000000 AND运算得网络地址结果:10101100.00010000.00001010.000000001->172.16.10.0
172.16.10.2:10101100.00010000.00001010.00000010 255255.255.255.0:11111111.11111111.11111111.00000000 AND运算得网络地址结果:10101100.00010000.00001010.000000001->172.16.10.0 结果都是172.16.10.0,因此它们在同一个子网络。

总结一下,IP协议的作用主要有两个,一个是为每一台计算机分配IP地址,另一个是确定哪些地址在同一个子网络。

tcp协议和udp协议

用于应用程序之间的通信。如果说ip地址和mac地址帮我们确定唯一的一台机器,那么我们怎么找到一台机器上的一个软件呢?

端口

  我们知道,一台拥有IP地址的主机可以提供许多服务,比如Web服务、FTP服务、SMTP服务等,这些服务完全可以通过1个IP地址来实现。那么,主机是怎样区分不同的网络服务呢?显然不能只靠IP地址,因为IP 地址与网络服务的关系是一对多的关系。实际上是通过“IP地址+端口号”来区分不同的服务的。

TCP协议

  当应用程序希望通过 TCP 与另一个应用程序通信时,它会发送一个通信请求。这个请求必须被送到一个确切的地址。在双方“握手”之后,TCP 将在两个应用程序之间建立一个全双工 (full-duplex) 的通信。

  这个全双工的通信将占用两个计算机之间的通信线路,直到它被一方或双方关闭为止。

TCP是因特网中的传输层协议,使用三次握手协议建立连接。当主动方发出SYN连接请求后,等待对方回答SYN+ACK[1],并最终对对方的 SYN 执行 ACK 确认。这种建立连接的方法可以防止产生错误的连接。[1] 
TCP三次握手的过程如下:
客户端发送SYN(SEQ=x)报文给服务器端,进入SYN_SEND状态。
服务器端收到SYN报文,回应一个SYN (SEQ=y)ACK(ACK=x+1)报文,进入SYN_RECV状态。
客户端收到服务器端的SYN报文,回应一个ACK(ACK=y+1)报文,进入Established状态。
三次握手完成,TCP客户端和服务器端成功地建立连接,可以开始传输数据了。
tcp三次握手
建立一个连接需要三次握手,而终止一个连接要经过四次握手,这是由TCP的半关闭(half-close)造成的。
(1) 某个应用进程首先调用close,称该端执行“主动关闭”(active close)。该端的TCP于是发送一个FIN分节,表示数据发送完毕。
(2) 接收到这个FIN的对端执行 “被动关闭”(passive close),这个FIN由TCP确认。
注意:FIN的接收也作为一个文件结束符(end-of-file)传递给接收端应用进程,放在已排队等候该应用进程接收的任何其他数据之后,因为,FIN的接收意味着接收端应用进程在相应连接上再无额外数据可接收。
(3) 一段时间后,接收到这个文件结束符的应用进程将调用close关闭它的套接字。这导致它的TCP也发送一个FIN。
(4) 接收这个最终FIN的原发送端TCP(即执行主动关闭的那一端)确认这个FIN。[1] 
既然每个方向都需要一个FIN和一个ACK,因此通常需要4个分节。
注意:
(1) “通常”是指,某些情况下,步骤1的FIN随数据一起发送,另外,步骤2和步骤3发送的分节都出自执行被动关闭那一端,有可能被合并成一个分节。[2] 
(2) 在步骤2与步骤3之间,从执行被动关闭一端到执行主动关闭一端流动数据是可能的,这称为“半关闭”(half-close)。
(3) 当一个Unix进程无论自愿地(调用exit或从main函数返回)还是非自愿地(收到一个终止本进程的信号)终止时,所有打开的描述符都被关闭,这也导致仍然打开的任何TCP连接上也发出一个FIN。
无论是客户还是服务器,任何一端都可以执行主动关闭。通常情况是,客户执行主动关闭,但是某些协议,例如,HTTP/1.0却由服务器执行主动关闭。[2] 
tcp四次挥手

 

UDP协议

  当应用程序希望通过UDP与一个应用程序通信时,传输数据之前源端和终端不建立连接。

  当它想传送时就简单地去抓取来自应用程序的数据,并尽可能快地把它扔到网络上。

tcp和udp的对比

TCP---传输控制协议,提供的是面向连接、可靠的字节流服务。当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据。TCP提供超时重发,丢弃重复数据,检验数据,流量控制等功能,保证数据能从一端传到另一端。 
UDP---用户数据报协议,是一个简单的面向数据报的运输层协议。UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能保证它们能到达目的地。由于UDP在传输数据报前不用在客户和服务器之间建立一个连接,且没有超时重发等机制,故而传输速度很快

现在Internet上流行的协议是TCP/IP协议,该协议中对低于1024的端口都有确切的定义,他们对应着Internet上一些常见的服务。这些常见的服务可以分为使用TCP端口(面向连接)和使用UDP端口(面向无连接)两种。 
说到TCP和UDP,首先要明白“连接”和“无连接”的含义,他们的关系可以用一个形象地比喻来说明,就是打电话和写信。两个人如果要通话,首先要建立连接——即打电话时的拨号,等待响应后——即接听电话后,才能相互传递信息,最后还要断开连接——即挂电话。写信就比较简单了,填写好收信人的地址后将信投入邮筒,收信人就可以收到了。从这个分析可以看出,建立连接可以在需要痛心地双方建立一个传递信息的通道,在发送方发送请求连接信息接收方响应后,由于是在接受方响应后才开始传递信息,而且是在一个通道中传送,因此接受方能比较完整地收到发送方发出的信息,即信息传递的可靠性比较高。但也正因为需要建立连接,使资源开销加大(在建立连接前必须等待接受方响应,传输信息过程中必须确认信息是否传到及断开连接时发出相应的信号等),独占一个通道,在断开连接钱不能建立另一个连接,即两人在通话过程中第三方不能打入电话。而无连接是一开始就发送信息(严格说来,这是没有开始、结束的),只是一次性的传递,是先不需要接受方的响应,因而在一定程度上也无法保证信息传递的可靠性了,就像写信一样,我们只是将信寄出去,却不能保证收信人一定可以收到。 
TCP是面向连接的,有比较高的可靠性, 一些要求比较高的服务一般使用这个协议,如FTP、Telnet、SMTP、HTTP、POP3等。
而UDP是面向无连接的,使用这个协议的常见服务有DNS、SNMP、QQ等。对于QQ必须另外说明一下,QQ2003以前是只使用UDP协议的,其服务器使用8000端口,侦听是否有信息传来,客户端使用4000端口,向外发送信息(这也就不难理解在一般的显IP的QQ版本中显示好友的IP地址信息中端口常为4000或其后续端口的原因了),即QQ程序既接受服务又提供服务,在以后的QQ版本中也支持使用TCP协议了。
更多介绍

互联网协议与osi模型

互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层

每层运行常见物理设备

 

每层运行常见的协议

Guess you like

Origin www.cnblogs.com/shangping/p/12080853.html