Chapter 13 TCP/IP and network programming
TCP/IP protocol
TCP/IP is the foundation of the Internet. TCP stands for transmission control protocol. IP stands for internet protocol. There are currently two versions of IP, IPv4 and IPv6. IPv4 uses a 32-bit address and IPv6 uses a 128 bit address. This section focuses on IPv4, which is still the most used IP version at present. The organizational structure of TCP/IP is divided into several levels, commonly known as TCP/IP stack.
At the top level are applications that use TCP/IP. Applications such as ssh for logging in to the remote host, mail for exchanging e-mail, http for Web pages need reliable data transmission. Typically, such applications use TCP at the transport layer. On the other hand, some applications, such as the ping command for querying other hosts, do not require reliability. Such applications can use UDP at the transport layer to improve efficiency.
Host: a host is a computer or device that supports TCP/IP protocol. Each host is identified by a 32-bit IP address.
IP address: the IP address is divided into two parts: NetworkID field and HostID field. According to the division, IP addresses are divided into A~E categories.
For example, a class B IP address is divided into a 16 bit network number, where the first two bits are 10, and then a 16 bit host number field. Packets destined for IP addresses are first sent to routers with the same network number. The router will forward the packet to a specific host in the network through the host number. Each host has a local host name localhost, and the default IP address is 127.0.0.1.
A router is a special IP host that receives and forwards packets. If so, an IP packet may pass through many routers or jump to a destination.
UDP
UDP is the user datagram protocol.
UDP runs over IP and is used to send / receive datagrams. Similar to IP, UDP does not guarantee reliability, but it is fast and efficient. It can be used in situations where reliability is not important.
ping uses UDP protocol.
TCP
TCP (transmission control protocol) is a connection oriented protocol used to send / receive data streams. TCP can also run over IP, but it ensures reliable data transmission. Generally, UDP is similar to USPS sending mail, while TCP is similar to telephone connection.
Ports and Applications
Application = (host IP, protocol, port number)
Data flow in TCP/IP network
The data of the application layer is passed to the transport layer, which adds a TCP or UDP header to the data to identify the transport protocol used. The combined data is transferred to the IP network layer, and an IP header containing IP address is added to identify the sending and receiving hosts. Then, the combined data is transmitted to the network cone layer. The network link layer divides the data into multiple frames and adds the addresses of the sending and receiving networks for transmission between physical networks: the mapping from IP address to network address is performed by address resolution protocol (ARP). At the receiving end, the data encoding process is the opposite. Each layer unpacks the received data by stripping the data header, reassembles the data and transmits the data to the upper layer. The original data of the application on the sending host will eventually be transferred to the corresponding application on the receiving host.
Network programming
Server client computing model: most network programming tasks are based on the server client computing model.
Socket:
struct sockaddr_in ( sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; ); struct in_addr { uint32_t s_addr;
In the socket address structure,
- Sin of TCP/IP network_ Family is always set to AF_INET.
- sm_port contains the port numbers in network byte order.
- sin_addr is the host IP address in network byte order.
Lint socket (int domain, int type, int protocol)
int udp_sock = socket(AF_INET, SOCK_DGRAM, 0);
A socket for sending / receiving UDP datagrams will be created.
int tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
A connection oriented TCP socket for sending / receiving data streams will be created.
Practice and problem solving
Programming exercise: use C language to realize simple UDP transmission.
This practice can realize UDP data transmission between client and server through C language.
The code is as follows:
client:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> #define MAXBUF 256 int main(int argc, char const *argv[]) { int s = 0; int n = 0; int reuse = 1; int port = 1987; struct sockaddr_in srv; char buf[MAXBUF] = {0}; /*Analytical parameters*/ if (argc != 2) { printf("Usage:%s ServerIP\n", argv[0]); return -1; } bzero(&srv, sizeof(srv)); srv.sin_family = PF_INET; srv.sin_addr.s_addr = inet_addr(argv[1]); srv.sin_port = htons(port); /*Create UDP socket word*/ s = socket(AF_INET, SOCK_DGRAM, 0); if(s<0){ perror("socket"); return -1; } while(1){ memset(buf, 0, MAXBUF); /*Read user input into buf*/ fgets(buf, MAXBUF, stdin); /*Send data to the server through the socket word s*/ if ((n = sendto(s, buf, strlen(buf), 0, (struct sockaddr *) &srv, sizeof(struct sockaddr))) < 0) { perror("sendto"); return -1; }else{ printf("send to %s(port=%d) len %d:%s\n", argv[1], port, n, buf); } } }
Server side:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> #define MAXBUF 256 int main(int argc, char const *argv[]) { int s = 0; int n = 0; int reuse = 1; int cli_len = sizeof(struct sockaddr); int port = 1987; char buf[MAXBUF] = {0}; struct sockaddr_in addr, cli; /*Initialize local listening port information*/ bzero(&addr, sizeof(addr)); addr.sin_family = PF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); /*Create UDP socket word*/ s = socket(AF_INET, SOCK_DGRAM, 0); if (s<0) { perror("socket"); return -1; } /*Allow port multiplexing*/ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); /*Bind specified port*/ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return -1; } while(1){ memset(buf, 0, MAXBUF); /*Read data from socket word s*/ n = recvfrom(s, buf, MAXBUF, 0, (struct sockaddr *)&cli, &cli_len); if(n<0){ perror("recvfrom"); return -1; }else{ printf("receive msg from %s(port=%d) len %d: %s\n",inet_ntoa(cli.sin_addr), port, n, buf); } } return 0; }
Practice screenshot:
Code link
The code includes some previous code in the code cloud. Link: https://gitee.com/Ressurection20191320/code/tree/master/IS