最終效果圖(更新A3501對應的數值並記錄更新時間)
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysqlclient-dev
MySQL需要提前安裝,因爲MySQL文件(m.c)需要包含頭文件(#include <mysql/mysql.h>
),需要用到一些封裝的函數
TCP套接字服務器( server.c 文件)
#include "head.h"
#include <mysql/mysql.h>
#include "m.h"
int main(int argc,char **argv)
{
//創建tcp套接字
int fd=socket(AF_INET,SOCK_STREAM,0);
//準備地址結構體ipv4
struct sockaddr_in addr;
socklen_t len=sizeof(addr);
bzero(&addr,len);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(atoi(argv[1]));
//綁定地址到指定的通信端點
if(bind(fd,(struct sockaddr *)&addr,len)==0)
{
printf("\n綁定成功\n");
//設定監聽狀態
//注意:只有處於監聽狀態的tcp套接字,才能接受対端的鏈接請求
//參數2規定的是該通信端點能(同時)接受連接請求的數量
//不是連接請求的總數
if(listen(fd,2)==0)
{
printf("設置監聽成功\n");
}
}
else
{
printf("端口綁定失敗\n");
close(fd);
printf("服務器已經斷開\n");
return 0;
}
//靜靜等待對端的連接
struct sockaddr_in peeraddr;
len=sizeof(peeraddr);
bzero(&peeraddr,len);
int connfd=accept(fd,(struct sockaddr *)&peeraddr,&len);
printf("連接成功,對端地址:%s:%hu\n",
inet_ntoa(peeraddr.sin_addr),
ntohs(peeraddr.sin_port));
char msg[10];
char ID_CARD_NAME[10];
char ch;
//循環等待數據接收
while(1)
{
bzero(msg,10);//數組清零
bzero(ID_CARD_NAME,10);//數組清零
if((read(connfd,msg,10)!=0)&&(msg[3]=='A'))
{
sprintf(ID_CARD_NAME,"%c%c%c%c%c",msg[3],msg[4],msg[5],msg[6],msg[7]);
printf("ID=%s\n",ID_CARD_NAME);
mysql_link();//連接MYSQL
print_table("resident");//查看錶中內容
update_data("credit=credit+1",ID_CARD_NAME);
update_data("latest_time=current_timestamp",ID_CARD_NAME);
print_table("resident");//查看錶中內容
continue;
}
}
mysql_closed();//關閉MySQL
close(connfd);
close(fd);
printf("服務器已經斷開\n");
return 0;
}
head.h 文件
#ifndef _HEAD_H_
#define _HEAD_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <sys/soundcard.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <time.h>
#include <syslog.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <errno.h>
#endif
MYSQL數據庫語句(m.c)
1.連接數據庫(mysql_link)
2.關閉數據庫(mysql_close)
3.打印 mydb 數據庫的表(show_tables)
4.C語言打印表格(print_table)
5.更新數據庫(update_date)
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mysql/mysql.h"
#include "m.h"
void mysql_link(void)//連接數據庫
{
MYSQL *mysql_ctr;
mysql_ctr = mysql_init(NULL);//初始化
mysql_ctr = mysql_real_connect(mysql_ctr, IP_addr, user, password, database, 0 ,NULL, 0);
if (mysql_ctr)
{
printf("\nMYSQL連接成功\n");
}
else
{
printf("\nMYSQL連接失敗\n");
}
}
void mysql_closed(void)//關閉數據庫
{
MYSQL *mysql_ctr;
mysql_close(mysql_ctr);
printf("MYSQL關閉成功\n");
}
void show_tables(void)//打印在mydb數據庫的表
{
static int t=0;
MYSQL *mysql_ctr;
static MYSQL_RES *res; // 返回查詢結果
static MYSQL_ROW row; // 返回行數據
t = mysql_query(mysql_ctr, "show databases");
if (t)
{
printf("Failed to query: %s\n", mysql_error(mysql_ctr));
}
res = mysql_store_result(mysql_ctr);
printf("\n***********************************************\n");
while ((row = mysql_fetch_row(res)))
{
for(t = 0; t < mysql_num_fields(res); t++)
{
printf("%s\t", row[t]);
}
printf("\n***********************************************\n");
}
mysql_free_result(res);
}
void print_table(char table_name[20])//C語言打印表格
{
static int i,j;
MYSQL *mysql_ctr;
char *cmd = "select * from ";
static char query[50];
static MYSQL_RES *res; // 返回查詢結果
static MYSQL_ROW row; // 返回行數據
memset(query, '\0', sizeof(query));//初始化query,清零
sprintf(query, "%s%s", cmd, table_name);
i=mysql_query(mysql_ctr, query);
if (i)
{
printf("Failed to query\n");
}
else
{
//printf("\nQuery successfully!\n");
printf("\n%s",table_name);
res = mysql_store_result(mysql_ctr);
j=mysql_num_fields(res);
printf("\n********************************************\n");
while (row = mysql_fetch_row(res)) //依次保存table每行的內容成數組
{
for(i = 0; i < j; i++)
{
printf("%s\t", row[i]);
}
printf("\n********************************************\n");
}
}
mysql_free_result(res);//釋放結果內存
}
void update_data(char handle[50], char *ID_name)//更新數據
{
MYSQL *mysql_ctr;
static int k;
static char query[100];
static char *cmd = "update resident";
sprintf(query, "%s set %s where ID=\"%s\"", cmd, handle, ID_name); //轉義\"
//printf("%s\n", query);
k = mysql_real_query(mysql_ctr, query, strlen(query));
if (k)
{
printf("Failed to query: %s\n", mysql_error(mysql_ctr));
}
printf("\nUpdate data sucessfully!\n");
}
m.h
#ifndef _M_H
#define _M_H
#include <mysql/mysql.h>
#define IP_addr "localhost"
#define user "admin"
#define password "admin"
#define database "mydb"
volatile extern MYSQL *mysql_ctr;
void mysql_link(void);
void mysql_closed(void);
void print_table(char table_name[20]);
void show_tables(void);
void update_data(char handle[50], char ID_name[10]);
#endif
終端GCC編譯
gcc server.c m.c -o server -lmysqlclient
編譯通過就OK,然後./server 50002
指定通信端口號運行即可