Network programming extranet communication

Insert picture description here

Source: WeChat Official Account "Programming Learning Base"

For beginners only, we learn network programming (such as TCP, UDP programming), we usually carry out communication tests in the local area network, sometimes we may think, we write the network data of the internal network and the network data of the external network. What is the difference? How does the data on our internal network get out of the external network?

The difference between public IP and private IP

First of all, we need to understand what are public IP and private IP ?

Public address : Inter NIC (Internet Network Information Center) is responsible. These IP addresses are assigned to organizations that have registered and applied to Inter NIC. The public IP is the only one in the world, through which you can directly access the Internet (you can directly access the Internet).

Private address (Private address) : It is a non-registered address, specifically for internal use by the organization. To put it bluntly, private IPs cannot be used to access the Internet directly.

And we usually surf the Internet through operators (Telecom, China Mobile, China Unicom Broadband, etc.). The IPs distributed through routers at home are all private IPs (LAN IPs). You may wonder, we can surf the Internet, how come it is a private IP? ?

And we usually surf the Internet through operators (Telecom, China Mobile, China Unicom Broadband, etc.). The IPs distributed through routers at home are all private IPs (LAN IPs). You may wonder, we can surf the Internet, how come it is a private IP? ?

It takes money to rent (apply) public IP . Operators bought some public IPs, then divided them out through these public IPs, and then distributed them to users one by one. This process is a bit similar. We installed the width and divided out several IPs through the router so that several people could access the Internet. Of course, the process of dividing the public IP by the operator is definitely more complicated than this. Therefore, the IP we usually use for the Internet is a private IP, and the operator who really owns the public IP (of course, we can rent a public IP ).

We can surf the Internet, how could it be a private IP?

Next, let me introduce to you what is port mapping

Port mapping is a type of NAT, which maps a port of the IP address of an external host to a machine in the internal network to provide corresponding services. When the user accesses this port of the IP, the server automatically maps the request to the machine inside the corresponding LAN.

Home routers on the market now have NAT function.

How to make the external network can access the network program (server) written by yourself

First of all, we need to apply for (rent) a public IP (with discounts for student computers) from the operator. If the public IP is 122.112.174.128, a simple chat room is running in the background on the server, and the listening port is 6666.

Then a computer host that can access the external network (ping through) can connect to this server, and then perform data interaction.

How to make the server execute in the background

The server is running in the background, by ps -A | grep sviewing the server, by kill -9 进程idkilling Service

[root@ecs-x-medium-2-linux-20200312093025 ~]# ps -A | grep s
23388 ?        00:00:00 s

The implementation of background execution is also very simple, just understand the daemon process, the summary is to call the following function

#include <unistd.h>
int daemonize()
{
    
    
    pid_t pid = fork();
    if ( pid < 0 )
    {
    
    
        return 0;
    }
    else if ( pid > 0 )
    {
    
    
        exit( 0 );
    }
    umask( 0 );
    pid_t sid = setsid();
    if ( sid < 0 )
    {
    
    
        return 0;
    }
    if ( ( chdir( "/" ) ) < 0 )
    {
    
    
        /* Log the failure */
        return 0;
    }
    close( STDIN_FILENO );
    close( STDOUT_FILENO );
    close( STDERR_FILENO );

    open( "/dev/null", O_RDONLY );
    open( "/dev/null", O_RDWR );
    open( "/dev/null", O_RDWR );
    return 1;
}

Write down in a small book, and you can also collect articles.

Examples of external network communication

After the server server is running on the server, run the client on a host that can access the external network

Insert picture description here

The server is not turned on locally, the ip address bound to the client is the public network ip address, and the port is 6666.

Finally, I changed the program of the next king a little and sent it out. I will write an epoll chat room later when I have time. The code is sent to the keyword chat room to get, and the client can be compiled and run to chat together (you need to install a virtual machine). The server code can also be obtained together

Guess you like

Origin blog.csdn.net/qq_44519484/article/details/109975338