Concepts and models of network programming!

I. Introduction:

For the basic knowledge of network programming, everyone must be familiar with it, and there are also a lot of information on the Internet! But most of the materials are scattered; so this topic is also to record your own study notes, and at the same time, it is also a systematic study of network programming, not only in theory, but more importantly, in practice!

Second, the essence of network programming:

The essence of network programming is to use the interface functions provided by the operating system to program, and then make the application program have the ability to send and receive network data !

insert image description here
In the above operating system diagram, there are two large modules, the kernel and the application:

  • Let's take a look at the kernel first: the kernel in the operating system is mainly used to manage hardware, and one of the hardware is the network card! To network and send and receive network data, we must rely on this hardware network card.

  • And after our application is executed, if it wants to send and receive network data, how should it do it? It must use the network interface provided by the kernel to indirectly control the network card through the kernel to realize the sending and receiving of network data!

3. The core concept of network programming:

  • Protocol: Pre-defined data rules for data communication; to enable the two parties to communicate normally, there must be a rule, and this rule is a protocol, and this protocol is defined by the programmer according to a certain standard!

  • Address: An integer value used in network communications to identify a device . For example, our common IP address: 192.168.1.100

  • Port number: The value specified by the device for sending and receiving data, used to identify the specific connection, can be understood as the data channel used for network communication in the device! For example, we send data to port 80!

  • Role: server: device waiting for connection; client: device that initiates connection

  • Little-endian system: A system using little-endian mode, that is, the low byte of the data is placed in the low address of the memory. For example, there is a 4-byte int type integer value:

0x12345678

How is this integer value stored in memory? This depends on what system it is distributed to. For example, if it is stored in a little-endian system, it should be stored like this:
insert image description here

  • Big-endian system: A system that adopts big-endian mode, that is, the low byte of data is placed in the high address of the memory. Let’s take the example just now as an example:

insert image description here

  • Network byte order: The network byte order adopts the big-endian mode, so a byte conversion is required in the little-endian system. Otherwise, when the data sent by device a reaches device b, the data may be corrupted. Change, then this change is the problem caused by the byte order, so a byte order conversion is required!

Fourth, the actual code case:

Let's now realize that a client sends a request to the server, and after the server receives the request, it will return something to the client: for example, the request shown in the picture below:

insert image description here
Before writing the code case, first draw an overall framework process for everyone:

  • Prepare to connect to the network (socket)
  • Connect to a remote device (connect)
  • Send and receive data (send/recv)
  • Close the connection (close)

insert image description here
A preliminary study on related network programming interfaces:

#include <sys/types.h>
#include <sys/socket.h>
function prototype Functional description
int socket(int domain, int type, int protocal); Create socket, prepare for network connection
int connect(int sock, struct sockaddr *addr, socklen_t len); Connect to the 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); Accept data sent back from the remote device
int close(int fd); destroy socket

The following is an example of a small demo case, sending a request to the Baidu server, and then the server returns the corresponding information:

#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(int argc, char *argv[])
{
    
    
	int sock =0;
	int len = 0;
	int r = 0;
	char buffer[128] = {
    
    0};
	struct sockaddr_in addr = {
    
    0};
	char *tosend = "GET /index.html HTTP/1.1\nHOST: www.baidu.com\nUser-Agent: TEST\nConnection: close\n\n";

    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("39.156.66.10");
    addr.sin_port = htons(80);

    if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
    
    
         printf("connect 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, buffer, sizeof(buffer),0);
       if(r>0)
       {
    
    
       	  len += r;
       }
       for(i=0;i<r;i++)
       {
    
    
      		printf("%c",buffer[i]);
       }
     }while(r >0)
    printf("\n");
    printf("recv bytes = %d\n",len);
    close(sock);
    
    return 0;
}

The final effect is as follows:

insert image description here

Well, the basic concepts of network programming in this issue are shared here, and we will continue to share in the next issue!

Guess you like

Origin blog.csdn.net/Dada_ping/article/details/127351113