After mastering everything, start this article.
1. Adjust the directory structure
In order to facilitate compilation, we now adjust the code structure of the previous article as follows.
root@ubuntu:/mnt/hgfs/code/chat# tree .
.
├── chat_client
│ ├── include
│ ├── Makefile
│ ├── obj
│ │ └── Makefile
│ └── src
│ ├── client.c
│ └── Makefile
├── chat.h
├── chat_server
│ ├── bin
│ │ └── server
│ ├── data
│ ├── include
│ ├── Makefile
│ ├── obj
│ │ └── server.o
│ └── src
│ ├── Makefile
│ └── server.c
└── gcc.sh
10 directories, 15 files
The final file directory with added data is as follows:
peng@ubuntu:/mnt/hgfs/code/chat-sqlite$ tree .
.
├── chat_client
│ ├── include
│ ├── Makefile
│ ├── obj
│ │ └── Makefile
│ └── src
│ ├── client.c
│ └── Makefile
├── chat.h
├── chat_server
│ ├── data
│ ├── include
│ │ └── data.h
│ ├── Makefile
│ ├── obj
│ │ └── Makefile
│ └── src
│ ├── data.c
│ ├── Makefile
│ └── server.c
├── clean.sh
├── gcc.sh
├── user.db
└── 解压密码.txt
9 directories, 17 files
clean.sh is used to clear temporary files gcc.sh is used to compile the entire project
The server code is placed in the chat_server directory; the client code is placed in the chat_client directory;
Database related codes are placed under chat_server/data.
chat.h is a header file used by all clients and servers, so it is placed in the root directory.
After adding functions later, the new header files and C files can be added to the include and src directories of the corresponding project directory respectively.
2. Design database tables
All client information we previously maintained was in a global array, and there was no saving function. Now we need to save all client information into the database.
Database storage directory
chat_server/data
Database name:
user.db
Table name to store user information:
user
The format of table user is as follows:
name | Attributes | illustrate |
---|---|---|
name | TEXT PRIMARY KEY | Username, cannot be repeated |
passwd | TEXT NOT NULL | password |
fd | INT NOT NULL | Socket descriptor: -1 means not online, >0 means online |
director | INT NOT NULL | Whether the user name is registered: -1 not registered, 1 registered |
3. Statements and functions of main functional operations
The most important thing for database operations is statements. The implementation statements corresponding to different functions are explained below.
1 Create table user
CREATE TABLE IF NOT EXISTS user(name TEXT PRIMARY KEY NOT NULL,passwd TEXT NOT NULL,fd INT NOT NULL,regist INT NOT NULL);
2 Add a user
After the client sends a registration request, the server registers the user information into the database.
The database operation statements are as follows:
insert into user values('一口Linux', '123456',-1, 1)
The function function is as follows:
int db_add_user(char name[],char passwd[])
功能:
增加一个用户,执行该函数前需要先判断该用户名是否存在
参数:
name:用户名
passwd:密码
返回值:
-1:失败
1:成功
3 Determine whether the user is online
After the client sends the login command, the server uses this function to determine whether the user has logged in successfully.
The database operation statements are as follows:
select fd from user where name='嵌入式Linux'
The function function is as follows:
int db_user_if_online(char *name,char *passwd)
功能:
判断用户是否在线,该函数主要根据fd的值来判断用户是否在线
参数:
name:用户名
passwd:密码
返回值:
1:在线
-1:不在线
-2:用户不存在
4 Determine whether a username is registered
The user sends a registration command, and the server needs to determine whether the user name has already been registered.
The database operation statements are as follows:
select regist from user where name='嵌入式Linux'
The function function is as follows:
int db_user_if_reg(char *name)
功能:
判断某个用户名是否注册过
参数:
name:用户名
返回值:
1:注册过
-1:没有注册过
5 Determine whether the username and password are correct
The user sends a login command and needs to determine whether the username and password are correct.
The database operation statements are as follows:
select * from user where name='嵌入式Linux' and passwd='123456'
The function function is as follows:
int db_user_pwd_corrct(char *name,char* passwd)
功能:
判断客户端发送的用户名密码是否正确
参数:
name:用户名
passwd:密码
返回值:
1:正确
-1:用户名或者密码不正确
6 Users go online and offline
After the user successfully logs in, or sends a logout application, or is abnormally disconnected, the status of the database needs to be updated.
The database operation statements are as follows:
UPDATE user set fd=-1 where name='嵌入式Linux'
The value of fd is the socket descriptor. The offline setting is -1, and the upper line setting is the corresponding value >0.
The function function is as follows:
int db_user_on_off(int fd,char *name,unsigned int on_off)
功能:
更新数据库中用户的fd字段
参数:
fd:套接字描述符
name:用户名
on_off:上线还是下线
返回值:
1:正确
-1:失败
7. Display online users
After the user sends the command to display online users, the server searches for all online users from the database and sends the names to the client in a loop.
int db_list_online_user(int fd)
4. Operation results
compile
./gcc.sh
1. Server startup
./server 9999
The port number is set to 9999
2. Client registration
Client start
./client 127.0.0.1 9999
Select 1 to register and enter your username and password.
3. User login
Enter option 2 and enter the username and password you just registered. If they are inconsistent, an error will be prompted.
login successful:
4. Register and log in several other users
Register and log in new users 111, 222, 333
5. Public chat
Select option 3 to enter the public chat. User yikou says hello to all users!
It can be seen that all users have received the message.
6. Private chat
User yikou sends a message to user 111:
As can be seen from the figure below, only user 111 received the information, and no other users received the information.
7. Display online users
8. View final database information
5. Code description
In order to facilitate readers to learn the difference between adding a database and removing data,
Use git to maintain versions.
To switch to the version without a database, execute the following command.
git reset --hard 597330ae0a183c9db8f68b7c9f60df94f8965778
To switch back to the version with a database, execute the following command:
git reset --hard 10bfbfaf2d09ae895313273c960ecfd84663f9fd
After using the database, data storage and management are more convenient, data types are easier to expand, and logical relationships are clearer.
To get the complete code, visit https://gitee.com/wx_98fa5ee790/chat directly