Question: How to do network programming?
The essence of network programming
Use the interface functions provided by the operating system to enable the application program to have the ability to send and receive network data
The network interface is a function provided by the operating system at the code level
- Network programming is just a fancy way of playing a series of system functions
The application uses the networking capabilities of the operating system through the network interface
- Network programming is domain-specific (network) programming in C
Protocol: Predefined data rules for data communication Address: Integer value used to identify the device in network communication ("xxx.xxx.xxx.xxx" 4-byte integer value can be represented, just to make it easier to remember, convert as a string)
The port number
- The value assigned by the device to send and receive data to indicate a specific connection
- It can be understood as: the data channel used for network communication in the device
- For example, a computer often has only one IP and has multiple applications, at this time, the port is used to distinguish
Role
- Server: Waiting for a connected device
- Client: It is the evil quilt who initiated the connection
ps : 一台设备可同时作为客户端和服务端
Network Knowledge Charging Station
Is a web address an IP address? What is the URL and what is the domain name?
- The URL is not an IP address, but refers to the address of network information resources (such as the address of a specific web page), namely: URL
- A domain name is the name of an IP address, and multiple domain names can point to the same IP address
- Domain Name → DNS → IP Address
Does the protocol have to be incomprehensible binary data?
- A protocol is a convention, ie: pre-defined rules
- Protocols can be based on textual or binary definitions
Little-endian system: a system that adopts little-endian mode, that is, the low byte of data is placed in the low address of memory
0x12345678 int 4字节
低地址 👉 |78|56|34|12| 👈 高地址
Big-endian system: a system that adopts big-endian mode, that is, the low byte of data is placed in the high address of memory
0x12345678 int 4字节
低地址 👉 |12|34|56|78| 👈 高地址
Network byte order: The byte order of the URL adopts the big endian mode , so byte conversion needs to be done in the little endian system
network programming mode
1. 准备连接
2. 连接远程设备
3. 收发数据
4. 关闭丽娜姐
A Preliminary Study of Network Programming Interface
#include <sts/types.h>
#include <sys/socket.h>
function prototype | Function description |
int socket(int domain, int type, int protocal); | Create a socket in preparation for a network connection |
int connect(int socket, struct sockaddr *addr, socklen_t len); | Connect to a remote device at the specified address |
ssize_t send(int fd, const void *buf, size_t n, int flags); | Send data to remote device |
ssize_t recv(int fd, void *buf, size_t n, int flags); | Receive data from remote devices |
int close(int fd); | close the connection, destroy the socket |
Programming experiment: first experience of network programming
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
int sock = 0;
struct sockaddr_in addr = {0};
char *tosend = "GET /index.html HTTP/1.1\nHOST: www.dt4sw.com\nUser-Agent: TEST\nConnection: close\n\n";
int len = 0;
char buf[128] = {0};
int r = 0;
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("socket error\n");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("47.99.217.175");
addr.sin_port = htons(80);
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
printf("connected error\n");
return -1;
}
printf("connect success\n");
len = send(sock, tosend, strlen(tosend), 0);
printf("send bytes = %d\n", len);
len = 0;
do {
int i = 0;
r = recv(sock, buf, sizeof(buf), 0);
if (r > 0) {
len += r;
}
for (i=0; i<r; ++i) {
printf("%c", buf[i]);
}
} while (r > 0);
printf("\n");
printf("recv bytes = %d\n", len);
return 0;
}
output:
./a.out
connect success
send bytes = 81
HTTP/1.1 200 OK
Content-Type: text/html
......
......
<script src="./static/js/app.js"></script>
</body>
</html>
recv bytes = 22382
Think: What is the role of the network program written this time?
Answer: Client
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。