C Language: based on the minimum network socket element design

C Language: based on the minimum network socket element design

First, the design requirements

Design a set of socket-based network elements, and tested using the C language. In addition to the requirements of the physical layer network element least two layers, and each network element has a frame synchronization, error control, flow control, to complete the application data generation, processing, control - can manually input data, a separate test track, It may be automatically generated source data, the test system reliability and functionality, the receiving end checks the data and display. Each analog switch function, a router function, and on this basis to build a switch, router hybrid network.

Second, the tool

Visual Studio2019, simulation software, a physical layer (network element having two connection (channel), the data transmission function of generating a scrambling code)

Third, a stage

3.1 Task

A frame synchronization phase is completed, error control, flow control design. Generating application data is completed, process control - can manually input data, a separate test track, may be automatically generated source data, the test system reliability and functionality, the receiving end checks the data and display.

3.2 design ideas

The design is based on the minimum NE socket redesign last program, the overall framework of the code written by the previous code and socket connection to the physical layer. On this basis, the added function framing, CRC check code generating function and the error detection functions, implement flow control is reflected in the main function.
Sending end application layer for generating a data transmission transmits data to the network layer side. Sending the client network layer receives the data of the application layer, the data framing processing, and transmits the frame to the physical layer. The physical layer receives the data, generated during the transmission error or a scrambling code, scrambling code and the frame is transmitted to the receiving end with the network layer.
After receiving data to the client network layer, the network layer frame error detection function will identify, using the CRC checking of the original data, if the data extracted from the original put correct data to the application layer of the receiving end, If the data error, the error code is sent to the application layer, but also send a feedback to the transmitting end, if the data frame is correct content sending all 1, all 0 error frame if the data content is transmitted. The transmitting side after receiving the feedback network layer determines the content of the feedback information, and then send information to the application layer, so that the application layer determines whether retransmission.
Such data transmission and retransmission of the entire process can be achieved.

3.3 functions implemented

To achieve a retransmission in stage two, send text, send pictures and other functions.

3.4 The main function design

3.4.1 framing function

When detailed analysis of the bit contents of channel transmitting a physical channel, I first transmit some data to the physical layer,
it can be clearly observed in the synchronous clock signal data and the physical layer itself transmission I sent (shaped like a
"010101" bitstream) is mixed together, so we really want to own can not be extracted from the physical layer to the
signal received. So I need to make a deal on the content of our data transmission: package framing.
Encapsulated frame format is as follows:
Here Insert Picture Description
data format regardless of the user selection at the application layer is how we regarded as converting binary coded data, ID data added to the network layer, the network layer, so that its checksum calculation and encapsulated in a frame .

3.4.2 CRC check code generating function

Physical layer channels have a random bit errors, which makes the requirements for the correction of errors. This design is used in the CRC, after generating an eight-bit binary code using a CRC check on the back of eight-bit data of the data frame.
To simplify the code, we have adopted a way to help achieve the look-up table.

3.4.3 Extraction and frame error check function

The network element design, we extract the function and error checking function frame placed inside a function, so you can make the code more simple, more convenient data extraction and verification.
For the extraction frame, we use the direct extraction method, that is, if in a statement directly to the header frame end recognized, this approach may seem violent, but it does not appear the same mistake with the header data is identified as the original end of the case or frame header. If statement in the original data and the CRC are respectively extracted, and then generates a new CRC check code for the original data, two check code comparison, it can be determined there is no data error.
Since the data transmission from the channel is not credible, if an error occurs, we do not know in the end it is the eight-bit data error occurred, or eight bits of CRC error has occurred, so no error correction, for this case, we chose to retransmit the data.

3.4.4 character encoding and decoding functions

To transfer the text, the text must be encoded, for simplicity, we can only transfer the provisions of English. We use the English ASCII code conversion, and then converted to ASCII code value bit binary number.
Decoding the text is opposite to the encoding process.
example:Here Insert Picture Description

3.4.4 picture encoding and decoding function

We use base64 encoding method of the picture. After base64 encoded string generated string, and then converting each character bit binary numbers, i.e., to achieve the encoding of the picture.
The picture decoding process and the encoding process is reversed.
Example:
The picture to be transmitted: (15 × 14)
Here Insert Picture Description
strings produced after base64 encoded: a total of 1984 characters.
Here Insert Picture Description
Is converted to a binary bit stream: 15872 bits total (part)
Here Insert Picture Description

3.4 application layer and network layer design

3.4.1 sending and receiving mechanism

1, the network element transmitting side:
(1) Application layer: an application layer data generated in two ways, first, generates a random bit binary data and send it to the network layer, the second is the text entered by the user into a binary encoding stream and split into eight binary numbers before sending them to the network layer.
(2) Network layer: The network layer receives the data after the CRC Mr. Cheng, and then encapsulated in a frame, the frame and sent to the physical layer. Here Insert Picture Description
2, the network element receiving end:
(1) Network layer: The network layer receives the data sent by the physical layer, the data frame is extracted, then the extracted bit data, and generates a new CRC checksum, and original checksum comparison, the data is determined whether an error occurs. If correct then sent to the application layer, if the transmitted error message.
(2) Application Layer: After receiving the application layer to the network layer data sent first determines this data is not an error message, if not, print the eight bits of data, text, or the reduced and printing; if the error message is printed Error message.

Here Insert Picture Description

3.4.2 feedback mechanism

1、接收端的网元:
接收端发送反馈主要是依靠网络层,网络层对接收的帧校验后,如果数据是正确的,则给发送端反馈一个信息,信息的内容不论。如果数据错误,不做任何处理,等待即可。(停止等待流量控制)

2、发送端的网元
发送端接收反馈并判断的也是在网络层。网络层接收到反馈帧则认为接收无误。
若没有接收反馈帧,我们规定一个等待时间,在规定时间内没有检测到反馈帧,则为超时,就要重传。

3.4.3 重传机制

重传主要是在发送端的应用层实现。
应用层接收到网络层发来的超时提示的信息后,保存本次的传输数据,并把重传判断参数调为1(重传判断参数为1则为需要重传,为0则不需要),这样在下一个传输循环中,发送的数据就会和上一次的一样,即实现了重传。
Here Insert Picture Description

3.5 测试

(1)随机数据:
发送端:左为应用层,右为网络层
Here Insert Picture Description
Here Insert Picture Description
接收端:左为应用层,右为网络层。
Here Insert Picture Description
从图片中的数据可见,第1、2次发生了误码,而且是反馈信息的误码,发送端重传了两次。
(2)传输文字的测试
测试1:在发送端应用层输入“hello”,让其发送。
发送端:左为应用层,右为网络层。
Here Insert Picture Description
接收端:左为应用层,右为网络层。
Here Insert Picture Description
测试2:在发送端应用层输入“hello,world”,让其发送。
发送端网络层:
Here Insert Picture Description
接收端应用层:
Here Insert Picture Description
(3)传输图片的测试
待传输的图片:(15×14)
Here Insert Picture Description
接收端:
Here Insert Picture Description
解码后的图片:
Here Insert Picture Description

四、阶段二

4.1 任务

完成交换接口排队模型设计——多接入的寻址设计,完成二层交换模型设计。(需结合二层交换技术)。

4.2 设计思路

本次设计依靠于配置文件中的“ne 5 三个网元三层连成环 交换机模型”(见github)。所以本次的设计是三个网元连成环状,每一个网元都是一个交换机,都能完成成帧、差错控制、寻址、转发的功能。在整个模型中,可以实现点对点及广播。

4.3 结构设计

4.3.1 网元结构

在OSI模型中,交换机本应在数据链路层,但我们出于简化网元的考虑,没有设计数据链路层,而是把交换机的功能放到我们设计的网元的网络层。这样网元仍是三层结构:应用层、网络层(数据链路层)、物理层。
Here Insert Picture Description

4.3.2 交换机模型结构设计

在我们的交换机模型中,三个网元连成环,构成环状拓扑,规定以逆时针方向发送数据。每个网元都是交换机,都可以发送、接收和转发数据,也就是说,每个网元都是一样的。
Here Insert Picture Description
如果网元1作为数据的发送端,则它向端口号为11100的物理层发出数据,网元2从端口号为12100的物理层接收数据,在网元2的网络层(数据链路层)判断是转发还是自己接收还是既转发又接收(广播)。如果是转发,则网元2从端口号为12101的物理层发出数据,网元3从端口号为13101的物理层接收数据。如果是网元2接收了数据,则网元3无需做任何操作。
对未知端口的处理。在环形拓扑中,如果不利用生成树算法,对携带未知端口号的数据会进行无限转发。

4.4 测试

略。。。

五、阶段三

5.1 任务

完成路由器设计,完成混合组网设计。

5.2 组网结构

Here Insert Picture Description

5.3 网元结构

出于简化的考虑,我们没有增加链路层,而是把链路层与网络层结合为一层,其结构与阶段三相同。

5.4 路由器设计

在最小网元设计中,由于网络拓扑简单,路由器与交换机实现的功能十分相似。在本次的设计中,我们设计的是静态路由,并且配置了静态路由表。
静态路由表的配置示例:
Here Insert Picture Description

5.5 测试

略。。。

Code: https: //github.com/ccc-hhh/C-.git

Released two original articles · won praise 0 · Views 92

Guess you like

Origin blog.csdn.net/ccooppplus/article/details/103863481