Swift Server Practice

Meqt

In Vapor, if you want your server program accessible to outside network, you set your hostname with the following code:

1
webapp.http.server.configuration.hostname = "0.0.0.0"

with special thanks to chatGPT’s answer:
This code is setting the hostname of an HTTP server in the Vapor framework. The hostname is being set to the “0.0.0.0” IP address, which is a special address known as the “wildcard” or “unspecified” address.

When an HTTP server is bound to this address, it means that the server will accept incoming connections on any IP address associated with the machine. This can be useful when you want your server to be accessible from any network interface on the machine, or when you don’t know the specific IP address you want to bind to in advance.

In this case, the hostname is being set in the “configuration” object of the HTTP server, which is an instance of the HTTPServerConfig struct. The hostname property of the struct is being set to the “0.0.0.0” IP address, which will cause the server to listen for incoming connections on all available network interfaces.

Network Information

Besides, I list some useful commands here:

1
netstat -antp

The netstat command is a utility used to display network statistics and information about the system’s network connections. The options used in the command netstat -antp are as follows:

  • -a: This option displays all active connections, including both incoming and outgoing connections.
  • -n: This option displays IP addresses and port numbers in numerical form, rather than resolving them to hostnames and service names.
  • -t: This option displays only TCP connections.
  • -p: This option displays the name of the process associated with each connection.

When run with these options, netstat -antp will display a list of all active TCP connections on the system, including both incoming and outgoing connections. The list will include the local and remote IP addresses and port numbers, as well as the name of the process associated with each connection.

This information can be useful for diagnosing network-related issues, such as determining which process is using a particular port or monitoring network activity.

Special Config of Aliyun

How to make a port on Aliyun Server accessible to all network interfaces

To make a port on an Aliyun Server accessible to all network interfaces, you need to configure the firewall and bind the port to the “0.0.0.0” IP address. Here’s an overview of the steps you need to follow:

  1. Open the firewall port: In the Aliyun Console, go to the “Security Group” page and add a new rule to allow incoming traffic on the desired port. This will ensure that the port is accessible from any network.
  2. Bind the port to “0.0.0.0”: In the configuration of the service that is using the port (for example, an HTTP server), you will need to bind the port to the “0.0.0.0” IP address. The exact method of doing this will depend on the software you are using, but it will typically involve setting a hostname or IP address in the server configuration.

Once you have made these changes, you should be able to access the port from any network interface on the Aliyun Server. However, it is important to note that you should only open the firewall port and bind the port to “0.0.0.0” if you need to allow incoming connections from any network. If you only need to allow incoming connections from specific networks, you should restrict access to the port by configuring the firewall appropriately.

MySQL

you need to give root user a password, or vapor would keep giving your a 111 error code. So you need to add the following magic incantation

1
2
3
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd';
flush privileges;
exit;

mysql statements I have been used quite frequently.

1
2
3
4
show databases;
use <database_name>;
show tables;
drop table <table name>;
  • Post title:Swift Server Practice
  • Post author:Meqt
  • Create time:2023-02-09 09:01:52
  • Post link:https://meqtmac.github.io/2023/02/09/swift-Server/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.