Overview
IP Address and Domain Name
An IP address is the host address in a network, used for two network hosts to find each other, which is the basis for successful network communication. An IP address is generally represented as a dotted decimal string, such as 192.168.1.1.
The websites we visit daily are hosted on servers with unique IP addresses. It is difficult to distinguish different hosts by remembering IP addresses, and a website may have multiple IP addresses or the IP address may change for some reason.
Therefore, the use of domain names to represent website addresses has emerged. For example, the domain name www.baidu.com is easier to remember than an IP address. IP addresses are still used in the actual network communication packets, so a domain name resolution protocol is required to obtain the IP address corresponding to a domain name.
The following explanations are based on the IPv4 protocol.
OSI Seven-Layer Model
The Open System Interconnection (OSI) model defined by the International Organization for Standardization (ISO) is a standard framework for computer or communication systems, generally known as the OSI seven-layer model. It provides a standard for implementing network communication protocols, where two parties use the same protocol at the same layer to communicate. For the same device, the lower-layer protocol provides calling interfaces for the upper-layer protocol to package the upper-layer protocol into an underlying-layer protocol, and finally sends it to the network for transmission.
The seven layers are “Application Layer”, “Presentation Layer”, “Session Layer”, “Transport Layer”, “Network Layer”, “Data Link Layer”, and “Physical Layer”.
To simplify protocol implementation or facilitate understanding, the models of five layers or four layers have emerged. The four-layer model is generally mentioned more frequently, including “Application Layer”, “Transport Layer”, “Network Layer” and “Network Interface Layer”.
The IP address mentioned earlier belongs to the network layer.
The network layer is used to forward all network data of the host to the NIC and send it to the network through the physical layer circuit.
To facilitate explanation, the following illustration is based on the four-layer model.
Transport Layer Protocol
IP addresses solve the problem of how two hosts in a network can find each other and then send and receive messages.
There may be multiple applications running on a host, performing different network tasks. When a host with a certain IP address receives a message from another host, which application should the message be passed to?
To solve this problem, transport layer protocols evolved based on network layer protocols, which allocate different ports to local network applications. After receiving a network-layer message, the data is delivered to different applications based on different port numbers.
To meet different needs, transport layer protocols are divided into UDP and TCP protocols.
UDP Protocol
The UDP protocol has the following characteristics:
• Connectionless
• Supports one-to-one, one-to-many, and many-to-many communication
• Irreliable delivery
• Full-duplex communication
• Message-oriented
Some application layer protocols have been derived from UDP to meet different needs, with different applications specifying a default port number by default. The port number can also be changed according to the actual situation.
TCP Protocol
The TCP protocol has the following characteristics:
• Connection-oriented
• One-to-one communication
• Reliable data delivery
• Full-duplex communication
• Byte stream-oriented
Based on TCP, some application layer protocols have been derived to meet different needs. Different applications will default to a specific port number. The port number can also be changed according to the actual situation.
TCP Network Programming
Before starting TCP network programming, let’s first understand the socket programming model for TCP servers and clients through the following diagram:
TCP Client Network Programming
The right side of the diagram displays the simplest interface calling process of the TCP client programming:
Call socket() to create a socket object.
Call connect() to connect to the server.
Call send() to send data to the server.
Call recv() to receive data sent by the server.
Loop steps 3 and 4 until certain conditions are met or the connection ends, then call close() to close the socket and release resources.
The socket interfaces implemented in almost all programming languages are in blocking mode by default, which means that all interfaces involving network packet sending and receiving, such as connect(), send(), recv(), close(), etc., are blocking interfaces by default.
The left side of the diagram illustrating the socket programming model for TCP servers and clients shows the interface calling process for server programming:
Call socket() to create a socket object.
Call bind() to bind the local address and port.
Call listen() to listen for client connection requests.
Call accept() to accept client connection requests.
Call recv() to receive uplink data from the client.
Call send() to send data to the client.
In each client connection, loop steps 5 and 6 until certain conditions are met or the connection ends, then call close() to close the socket and release resources.
In the thread that accepts client connection requests, loop step 4 to accept more client connections.
When programming a TCP server, there are three additional interfaces compared to the client: bind(), listen(), and accept().
TCP server code:
TCP client code:
Main code:
UDP Network Programming
Before starting UDP network programming, let’s first understand the socket programming model of UDP server and client through the following diagram:
From the diagram, we can see that the UDP server also needs to call bind() to bind the local IP address and port number, which is necessary for the server.
At the same time, UDP programming is different from TCP programming in interface calls:
• Different parameters for socket():
– For TCP programming, the second parameter type is usocket.SOCK_STREAM, while for UDP it is usocket.SOCK_DGRAM.
– For TCP, the third parameter proto is usocket.IPPROTO_TCP or usocket.IPPROTO_TCP_SER, while for UDP it is usocket.IPPROTO_UDP.
• Since UDP is connectionless, the client does not need to call connect() to connect to the server.
• The data sender only needs to call sendto() to send data.
• The data receiver calls recvfrom() to receive data.
Whether sendto() can send data to the destination depends on the network environment. If the host corresponding to the destination IP address cannot be found, the data will be discarded.
Next, let’s do an experiment: write a UDP server program and a UDP client program in the module. The client periodically sends data to the server and waits for the server to send data back.
AQs
- Why does the connection to the server fail?
• The server must be a public IP address (except for connecting to the local server of the module).
• Use a TCP/UDP test tool client on the PC or mqtt.fx to connect to the server to confirm whether the connection is successful and exclude server failures.
• Do not use China Unicom SIM cards for 2G networks.
• Check the module’s signal, network registration, network attachment, and PDP activation status.
• Check if the SIM card is overdue [There is a phenomenon of overdue fees for 4G modules: unable to register 4G network but can register 2G networks].
- Does TCP have an automatic reconnection mechanism?
No. There is no automatic reconnection at the underlying layer, and the reconnection mechanism is executed at the application layer.
- How to troubleshoot DNS resolution failure?
Check whether the SIM card has successfully registered to the network and check the validity of the address, then try again.
- Why does the 4G module fail to connect to the server with a private NIC?
• Check if the APN parameters are set correctly.
• If there are modules from other manufacturers, compare and test whether the connection is normal.
• If it cannot be guaranteed that the server configuration is correct, capture packets with Wireshark on the server or install a third-party tool on the server to open a server port for comparison testing.
• For the IoT card with a fixed IP address, the domain name or IP address needs to be added to the whitelist in advance.
- What is the maximum number of sockets each module series can create at the same time?
• The EC200A series module can create up to 64 sockets at the same time.
• The ECxxxG/ECxxxU series modules can create up to 15 sockets at the same time.
• The BG95 series module can create up to 20 sockets at the same time.
- Can the 4G module be used as both a server and a client at the same time?
• For the IPv4 protocol, a module generally cannot be used as a server. The module can obtain the private IP address assigned by the operator. While for private NICs, the IP address assigned by the operator can be used for communication within the private network, so it can be used as a server.
• For the IPv6 protocol, the global link address is the public IP address, so it can be used as a server.
• The client inside the module can connect to the server inside the module, which is necessary for some application architectures.
- Why does a packet of less than 50 B consume much more data in a day?
If TCP protocol is used, a three-way handshake and four-way handshake are required to complete a data interaction. Although the raw data is not much, the actual data consumed is more than 50 B due to the necessity of adding packet headers, trailer and checksum, etc. Some operators have a requirement that each packet must be sent with a minimum size of 1 KB, and if a packet is less than 1 KB, various checksums will be added to make it to 1 KB.
- Will the connection be closed by the base station if the keepalive interval is set long?
Yes. It is generally recommended to set the keep alive interval to 2 minutes and not over 4 minutes. According to the base station policy, the connections with no data transmission for a long time will be closed. Therefore, a long keepalive interval may lead to disconnection by the base station.









Top comments (0)