在线英英词典
项目功能描述
用户注册和登录验证
服务器端将用户信息和历史记录保存在数据库中。客户端输入用户名和密码,服务器端在数据库中查找、匹配,返回结果
单词在线翻译
根据客户端输入的单词在字典文件中搜索
历史记录查询
项目分析
代码实现
gcc client.c -o client
./client 192.168.232.128 1234
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#define N 32
#define R 1 // user - register
#define L 2 // user - login
#define Q 3 // user - query
#define H 4 // user - history
// 定义通信双方的信息结构体
typedef struct
{
int type;
char name[N];
char data[256];
} MSG;
int do_register(int sockfd, MSG *msg)
{
msg->type = R;
printf("Input name:");
scanf("%s", msg->name);
getchar();
printf("Input passwd:");
scanf("%s", msg->data);
if (send(sockfd, msg, sizeof(MSG), 0) < 0)
{
printf("fail to send.\n");
return -1;
}
if (recv(sockfd, msg, sizeof(MSG), 0) < 0)
{
printf("Fail to recv.\n");
return -1;
}
// ok ! or usr alread exist.
printf("%s\n", msg->data);
return 0;
}
int do_login(int sockfd, MSG *msg)
{
msg->type = L;
printf("Input name:");
scanf("%s", msg->name);
getchar();
printf("Input passwd:");
scanf(