服务器端:
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"C:\\Program Files\\MySQL\\MySQL Server 5.7\\lib\\libmysql.lib")
struct Library
{
char bookName[100];
char writerBook[100];
char bookId[100];
char flag[100];
char studentName[100];
char studentNumber[100];
};
void addNewBook(SOCKET & sClient,MYSQL & mycont)
{
using std::cout;
using std::endl;
char sql_insert[2048];
while (1) //实现循环添加
{
char buffer[1024];
Library book;
recv(sClient, buffer, sizeof(buffer), 0);
if (buffer[0] == 'q')
{
cout << "quit" << endl;
break;
}
memcpy(&book, buffer, sizeof(Library)); //将接受的内容转换为结构体形式
sprintf(sql_insert, "insert into mytable(bookname,writerbook,bookid,flag) values(%s,%s,%s,'IN')", book.bookName, book.writerBook,book.bookId);
mysql_query(&mycont, sql_insert);
}
}
void seeAllBooks(SOCKET & sClient, MYSQL & mycont)
{
using std::cout;
using std::endl;
MYSQL_RES * result;
MYSQL_ROW sql_col = 0;
mysql_query(&mycont,"select * from mytable"); //访问数据库
result = mysql_store_result(&mycont); //存储上面命令的结果
if (result)
{
Library book;
while (sql_col = mysql_fetch_row(result)) //将每一列的结果保存
{
memcpy(book.bookName, sql_col[0], sizeof(book.bookName));
memcpy(book.writerBook, sql_col[1], sizeof(book.writerBook));
memcpy(book.bookId, sql_col[2], sizeof(book.bookId));
memcpy(book.flag, sql_col[3], sizeof(book.flag));
send(sClient, (char *)&book, sizeof(Library), 0); //先将要发送的结构体内容整合为字符串形式再发送
int t = 1000000;
while (t > 0)
{
t--;
}
}
}
send(sClient, "q", sizeof("q"), 0);
}
void borrowBook(SOCKET & sClient, MYSQL & mycont)
{
using std::cout;
char buffer[1024];
char mysearch[100];
Library book;
MYSQL_RES * result;
MYSQL_ROW sql_col = 0;
recv(sClient, buffer, sizeof(buffer), 0);
memcpy(&book, buffer, sizeof(Library));
sprintf(mysearch, "select * from mytable where bookname = %s and bookid = %s", book.bookName, book.bookId); //sprintf实现含变量的命令调用
mysql_query(&mycont, mysearch);
if (result = mysql_store_result(&mycont))
{
sql_col = mysql_fetch_row(result);
memcpy(book.bookName, sql_col[0], sizeof(book.bookName));
memcpy(book.writerBook, sql_col[1], sizeof(book.writerBook));
memcpy(book.bookId, sql_col[2], sizeof(book.bookId));
memcpy(book.flag, sql_col[3], sizeof(book.flag));
send(sClient, (char *)&book, sizeof(Library), 0);
recv(sClient, buffer, sizeof(buffer), 0);
memcpy(&book, buffer, sizeof(Library));
char para[1024];
sprintf(para,"update mytable set flag = 'OUT',studentName = %s, studentNumber = %s where bookname = %s and bookid = %s", book.studentName, book.studentNumber, book.bookName,book.bookId);
mysql_query(&mycont, para);
}
else
{
send(sClient, "q", sizeof("q"), 0);
}
}
void backBook(SOCKET & sClient, MYSQL & mycont)
{
using std::cout;
using std::endl;
char buffer[1024];
Library book;
char mysearch[1024];
MYSQL_RES * res;
recv(sClient, buffer, sizeof(buffer), 0);
memcpy(&book, buffer, sizeof(Library));
char para[1024];
sprintf(para, "update mytable set flag = 'IN',studentName = 'NULL',studentNumber = 'NULL' where bookid = %s and studentname = %s",book.bookId, book.studentName);
mysql_query(&mycont, para);
Sleep(1);
}
void seeBackBook(SOCKET & sClient, MYSQL & mycont)
{
using std::cout;
using std::endl;
char buffer[1024];
Library book;
char mysearch[1024];
MYSQL_RES * res;
MYSQL_ROW sql_col;
sprintf(mysearch,"select * from mytable where flag = 'OUT'");
mysql_query(&mycont, mysearch);
res = mysql_store_result(&mycont);
if (res)
{
Library book;
while (sql_col = mysql_fetch_row(res))
{
memcpy(book.bookName, sql_col[0], sizeof(book.bookName));
memcpy(book.writerBook, sql_col[1], sizeof(book.writerBook));
memcpy(book.bookId, sql_col[2], sizeof(book.bookId));
memcpy(book.flag, sql_col[3], sizeof(book.flag));
memcpy(book.studentName, sql_col[4], sizeof(book.studentName));
memcpy(book.studentNumber, sql_col[5], sizeof(book.studentNumber));
send(sClient, (char *)&book, sizeof(Library), 0);
int t = 1000000;
while (t > 0)
{
t--;
}
}
}
send(sClient, "q", sizeof("q"), 0); //终止条件
}
void deleteBook(SOCKET & sClient, MYSQL & mycont)
{
using std::cout;
using std::endl;
char buffer[1024];
Library book;
char mysearch[1024];
recv(sClient, buffer, sizeof(buffer), 0);
memcpy(&book, buffer, sizeof(Library));
sprintf(mysearch, "delete from mytable where bookname = %s and bookid = %s", book.bookName, book.bookId);
mysql_query(&mycont, mysearch);
}
int functions(SOCKET & sClient)
{
using std::cout;
using std::cin;
using std::endl;
int retVal;
const int BUF_SIZE = 64;
char buf[BUF_SIZE];
char sendBuf[BUF_SIZE];
const char user[] = "root";
const char pswd[] = "12345678";
const char host[] = "localhost";
const char database[] = "xiwenbase";
unsigned int port = 3306;
MYSQL mycont;
MYSQL_RES * result;
MYSQL_ROW sql_row;
int res;
mysql_init(&mycont);
if (mysql_real_connect(&mycont, host, user, pswd, database, port, NULL, 0))
{
mysql_query(&mycont, "SET NAMES GBK");
while (1)
{
ZeroMemory(buf, BUF_SIZE);
retVal = recv(sClient, buf, BUF_SIZE, 0);
if (SOCKET_ERROR == retVal)
{
cout << "对方已下线" << endl;
closesocket(sClient);
WSACleanup();
return -1;
}
char c;
c = buf[0];
if (buf[0] == '0')
{
cout << "发送为空!" << endl;
}
switch (c)
{
case '1':addNewBook(sClient,mycont); break;
case '2':seeAllBooks(sClient, mycont); break;
case '3':borrowBook(sClient, mycont); break;
case '4':backBook(sClient, mycont); break;
case '5':seeBackBook(sClient, mycont); break;
case '6':deleteBook(sClient, mycont); break;
}
}
}
}
int main()
{
using std::cout;
using std::cin;
using std::endl;
using std::thread;
int retVal;
SOCKET sServer;
SOCKET sClient;
WSAData wsd;
SOCKADDR_IN servaddr;
SOCKADDR_IN cliaddr;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
cout << "startup error!" << endl;
return -1;
}
sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sServer == INVALID_SOCKET)
{
cout << "socket failed!" << endl;
WSACleanup();
return -1;
}
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons((short)4999);
servaddr.sin_addr.s_addr = INADDR_ANY;
retVal = bind(sServer, (LPSOCKADDR)&servaddr, sizeof(servaddr));
if (SOCKET_ERROR == retVal)
{
cout << "bind error!" << endl;
closesocket(sServer);
WSACleanup();
return -1;
}
retVal = listen(sServer, 1024);
if (SOCKET_ERROR == retVal)
{
cout << "listen error!" << endl;
closesocket(sServer);
WSACleanup();
return -1;
}
while (1)
{
int cliaddrlen = sizeof(cliaddr);
sClient = accept(sServer, (sockaddr FAR*)&cliaddr, &cliaddrlen);
if (INVALID_SOCKET == sClient)
{
//cout << "accept error!" << endl;
closesocket(sServer);
WSACleanup();
return -1;
}
thread t1(functions,sClient);
t1.detach();
}
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return 0;
}
客户机端:
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
using namespace std;
struct Library
{
char bookName[100];
char writerBook[100];
char bookId[100];
char flag[100];
char studentName[100];
char studentNumber[100];
};
void show(SOCKET & sServet)
{
Library book;
cout << " ———————————————————" << endl;
cout << " | 欢迎使用西文图书室管理系统! |" << endl;
cout << " | 1、添加新书" << '\t' << "2、查看书库 |" << endl;
cout << " | 3、借阅图书" << '\t' << "4、归还图书 |" << endl;
cout << " | 5、借书信息" << '\t' << "6、下架废书 |" << endl;
cout << " ———————————————————" << endl;
char c;
cout << "请输入操作数字:" << endl;
cin >> c;
switch (c)
{
case '1':
{
send(sServet, "1", strlen("1"), 0);
while (1)
{
cout << "请输入要添加的书目名称、作者、书目编号(q退出)" << endl;
cin >> book.bookName;
if (book.bookName[0] == 'q')
{
send(sServet, "q", sizeof("q"), 0);
break;
}
cin>> book.writerBook >> book.bookId;
memcpy(book.bookName, book.bookName, sizeof(book.bookName));
memcpy(book.writerBook, book.writerBook, sizeof(book.writerBook));
memcpy(book.bookId, book.bookId, sizeof(book.bookId));
send(sServet, (char *)&book, sizeof(Library), 0);
}
break;
}
case '2':
{
char buffer[1024];
send(sServet, "2", strlen("2"), 0);
cout << left << setw(6) << "书名" << setw(6) << "作者" << setw(10) << "书目编号" << setw(6) << "借阅状态" << endl;
while (1)
{
Library book;
ZeroMemory(buffer, 1024);
recv(sServet, buffer, sizeof(buffer), 0);
if (buffer[0] == 'q')
{
cout << "显示完毕!" << endl;
break;
}
memcpy(&book, buffer, sizeof(Library));
cout << left << setw(6) << book.bookName << setw(6) << book.writerBook << setw(10) << book.bookId << setw(6) << book.flag << endl;
}
break;
}
case '3':
{
char buffer[1024];
Library book;
send(sServet, "3", strlen("3"), 0);
cout << "请输入你要借阅的书籍的名称和书目编号:" << endl;
cin >> book.bookName >> book.bookId;
memcpy(book.bookName, book.bookName, sizeof(book.bookName));
memcpy(book.bookId, book.bookId, sizeof(book.bookId));
send(sServet, (char *)&book, sizeof(Library), 0);
cout << "1" << endl;
recv(sServet, buffer, sizeof(buffer), 0);
if (buffer[0] == 'q')
{
cout << "查询失败!" << endl;
break;
}
memcpy(&book, buffer, sizeof(Library));
cout << left << setw(6) << "书名" << setw(6) << "作者" << setw(10) << "书目编号" << setw(6) <<"借阅状态"<< endl;
cout << left << setw(6) << book.bookName << setw(6) << book.writerBook << setw(10) << book.bookId << setw(6) << book.flag<> book.studentName >> book.studentNumber;
memcpy(book.studentName, book.studentName, sizeof(book.studentName));
memcpy(book.studentNumber, book.studentNumber, sizeof(book.studentNumber));
send(sServet, (char *)&book, sizeof(Library), 0);
cout << "借阅成功!" << endl;
}
break;
}
case '4':
{
char buffer[1024];
send(sServet, "4", strlen("4"), 0);
Library book;
cout << "请输入你要归还的图书编号和本人姓名:" << endl;
cin >> book.bookId >> book.studentName;
memcpy(book.bookId, book.bookId, sizeof(book.bookId));
memcpy(book.studentName, book.studentName, sizeof(book.studentName));
send(sServet, (char *)&book, sizeof(Library), 0);
cout << "还书成功!欢迎下次使用!" << endl;
break;
}
case '5':
{
char buffer[1024];
send(sServet, "5", strlen("5"), 0);
cout << left << setw(6) << "书名" << setw(6) << "作者" << setw(10) << "书目编号" << setw(10) << "借阅状态" << setw(10) << "借书人"<> book.bookName >> book.bookId;
memcpy(book.bookName, book.bookName, sizeof(book.bookName));
memcpy(book.bookId, book.bookId, sizeof(book.bookId));
send(sServet, (char *)&book, sizeof(Library), 0);
cout << "下架成功!" << endl;
break;
}
default:cout << "谢谢使用!" << endl;exit(0);
}
}
int main()
{
const int BUF_SIZE = 64;
WSADATA wsd;
SOCKET sServer;
SOCKADDR_IN servaddr;
char buf[BUF_SIZE];
char recvBuf[BUF_SIZE];
int retVal;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
cout << "WSAStartup error!" << endl;
return -1;
}
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sServer)
{
cout << "socket error!" << endl;
WSACleanup();
return -1;
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons((short)4999);
int servaddrlen = sizeof(servaddr);
retVal = connect(sServer, (LPSOCKADDR)&servaddr, servaddrlen);
if (SOCKET_ERROR == retVal)
{
cout << "connect error!" << endl;
WSACleanup();
closesocket(sServer);
return -1;
}
while (1)
{
show(sServer);
}
closesocket(sServer);
WSACleanup();
return 0;
}