头文件:
#ifndef _CHATROOM1_H_
#define _CHATROOM1_H_
//************库函数
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//************宏定义
#define PORT 8888
#define SUCCESS 10000000
#define FAILURE 10000001
#define RESIGER 1
#define LOGIN 2
#define OK 3 //发送成功
#define ERROR 4
#define FIND 5
#define SAME 6
#define SEND 7 //发送消息
#define UN 8 //不在线
#define MSG 9 //收到悄悄话
#define NUM 10 //查看在线人数
#define EXIT 11
#define GROUP 12
#define ERR 13 //群聊没人在线
#define ES 14 //表情
#define PS 15 //常用语
#define VIP 16
#define TALK 17 //解禁
#define SHUTUP 18 //禁言
#define OKK 19
#define OUTLINE 20
#define OKKK 21
#define SENDFILE 22
#define UNRE 23
#define REOK 24
//**************线程
pthread_t tid = 0;
//*************结构体
struct online // 在线链表
{
int sockfd;
char name[20];
struct online* next;
};
typedef struct online* LinkList;
struct senduser // 用户信息表
{
char id[20];
char name[20];
char toname[20];
char password[20];
char code[20]; //密保
char msg[200];
char onlinenum[30][30]; //在线的人
char filename[20];
char filemsg[200];
int e_s;
int p_s;
int vip; //1是vip 0则不是
int state; //1是被禁言 0则不是
int number; //人数
int cmd;
};
#endif
客户端:
#include "chatroom1.h"
struct senduser User;
int OUT = 0;
int gettime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "%s\n",asctime (timeinfo) );
}
void SCAN_N()
{
char ch;
while((getchar())!='\n'&&ch!=EOF);
}
void show()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 欢迎来到聊天室! *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t******************************\n");
sleep(1);
}
void mainshow()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n\n");
printf("\t\t\t\t 1.登录\n");
printf("\t\t\t\t 2.注册\n");
printf("\t\t\t\t 3.忘记密码\n");
printf("\t\t\t\t 4.退出\n\n\n");
printf("\t\t\t\t******************************\n");
printf("\t\t\t\t*********输入你的选择*********\n");
}
void logging()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n\n");
printf("\t\t\t\t 正在登录中.... \n\n\n");
printf("\t\t\t\t******************************\n");
}
void sending()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n\n");
printf("\t\t\t\t 正在发送中.... \n\n\n");
printf("\t\t\t\t******************************\n");
}
//*******************注册***********************
int enroll(int sockfd)
{
int ret;
struct senduser User1;
char password1[20];
char password2[20];
User1.vip = 0;
User1.state = 0;
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n");
printf("\t\t\t\t请输入账号:\n");
printf("\t\t\t\t");
scanf("%s", User1.id);
SCAN_N();
while(1)
{
printf("\t\t\t\t请输入密码:\n");
printf("\t\t\t\t");
scanf("%s", password1);
SCAN_N();
printf("\t\t\t\t再次输入密码:\n");
printf("\t\t\t\t");
scanf("%s", password2);
SCAN_N();
if(strcmp(password1, password2) == 0)
{
printf("\t\t\t\t输入密码成功!\n");
strcpy(User1.password, password1);
break;
}
else
{
printf("\t\t\t\t两次输入不相同,请重新输入!\n");
}
}
printf("\t\t\t\t请输入昵称:\n");
printf("\t\t\t\t");
scanf("%s", User1.name);
SCAN_N();
printf("\t\t\t\t密保:请输入六位数字\n");
printf("\t\t\t\t");
scanf("%s", User1.code);
SCAN_N();
User1.cmd = RESIGER;
ret = send(sockfd, &User1, sizeof(User1), 0);
if(ret == -1)
{
perror("send");
return FAILURE;
}
ret = recv(sockfd, &User1, sizeof(User1), 0);
if(User1.cmd == UNRE)
{
system("clear");
printf("\t\t\t\t注册失败!\n");
sleep(1);
}
if(User1.cmd == REOK)
{
system("clear");
printf("\t\t\t\t注册成功!\n");
sleep(1);
}
}
//***************************登录****************
int login(int sockfd)
{
int ret;
while(1)
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t******************************\n\n");
printf("\t\t\t\t请输入您的账号:\n");
printf("\t\t\t\t");
scanf("%s", User.id);
SCAN_N();
printf("\t\t\t\t请输入密码:\n");
printf("\t\t\t\t");
scanf("%s", User.password);
SCAN_N();
User.cmd = LOGIN;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("LOGINsend");
return FAILURE;
}
memset(&User, 0, sizeof(User));
ret = recv(sockfd, &User, sizeof(User), 0);
if(User.cmd == OK)
{
return 1;
}
else if(User.cmd == SAME)
{
return 3;
}
else if(User.cmd == ERROR)
{
printf("\t\t\t\t登录错误!请重新输入正确账号和密码!\n");
sleep(1);
}
}
}
//****************************忘记密码********************
int find(int sockfd)
{
int ret;
struct senduser User1;
while(1)
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t请输入您的账号:\n");
printf("\t\t\t\t");
scanf("%s", User1.id);
SCAN_N();
printf("\t\t\t\t请输入您的密保答案:\n");
printf("\t\t\t\t");
scanf("%s", User1.code);
SCAN_N();
printf("\t\t\t\t请输入修改的密码:\n");
printf("\t\t\t\t");
scanf("%s", User1.password);
SCAN_N();
User1.cmd = FIND;
ret = send(sockfd, &User1, sizeof(User1), 0);
if(ret == -1)
{
perror("findsend");
return FAILURE;
}
ret = recv(sockfd, &User1, sizeof(User1), 0);
if(ret == -1)
{
perror("findrecv");
return FAILURE;
}
if(User1.cmd == OK)
{
printf("\t\t\t\t修改成功!\n");
sleep(1);
break;
}
else
{
sleep(1);
printf("\t\t\t\t修改失败!请输入正确的账号和密码!\n");
}
}
}
void sendps(int n, char *name)
{
switch(n)
{
case 1:
printf("\t\t\t\t %s给您发送了一句话:\n", name);
printf("\t\t\t\t How are you doing?\n");
break;
case 2:
printf("\t\t\t\t %s给您发送了一句话:\n", name);
printf("\t\t\t\t What's up?\n");
break;
case 3:
printf("\t\t\t\t %s给您发送了一句话:\n", name);
printf("\t\t\t\t May I have your name, please?\n");
break;
case 4:
printf("\t\t\t\t %s给您发送了一句话:\n", name);
printf("\t\t\t\t I'm glad to have met you.\n");
break;
case 5:
printf("\t\t\t\t %s给您发送了一句话:\n", name);
printf("\t\t\t\t Keep in touch.\n");
break;
case 6:
printf("\t\t\t\t %s给您发送了一句话:\n", name);
printf("\t\t\t\t Same to you.\n");
break;
}
}
void sendes(int n, char *name)
{
switch(n)
{
case 1:
printf("\t\t\t\t %s向您发送了一个表情:\n", name);
printf("\t\t\t\t .҉٩(*´︶`*)۶҉我跳起来就是一个么么哒\n");
break;
case 2:
printf("\t\t\t\t %s向您发送了一个表情:\n", name);
printf("\t\t\t\t .╮(︶﹏︶)鬼知道我经历了什么\n");
break;
case 3:
printf("\t\t\t\t %s向您发送了一个表情:\n", name);
printf("\t\t\t\t ٩(๑´0`๑)۶ 还我假期\n");
break;
case 4:
printf("\t\t\t\t %s向您发送了一个表情:\n", name);
printf("\t\t\t\t _(:ι」∠)_好饿,但是不想动\n");
break;
case 5:
printf("\t\t\t\t %s向您发送了一个表情:\n", name);
printf("\t\t\t\t _(:3」∠)_ 我的床需要我!\n");
break;
}
}
//***************************禁言****************************
int shutup(int sockfd)
{
int ret;
int m;
char n[20];
if(User.state == 1)
{
system("clear");
printf("\t\t\t\t***********************************\n\n");
printf("\t\t\t\t您正处于被禁言状态!是否解禁?\n");
printf("\t\t\t\t 1.霸气解禁! 2.做个安静的美男子也挺好\n");
printf("\t\t\t\t");
scanf("%d", &m);
SCAN_N();
if(m == 1)
{
if(User.vip == 1)
{
User.state = 0;
User.cmd = TALK;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendtalk");
return FAILURE;
}
printf("\t\t\t\t 解禁成功!尽情发言吧!\n");
sleep(1);
}
else
{
printf("\t\t\t\t 抱歉!您还不是我们尊贵的会员,不可以解禁噢!\n");
sleep(1);
}
}
}
system("clear");
printf("\t\t\t\t***********************************\n\n");
printf("\t\t\t\t您想要禁言哪位好友?\n");
printf("\t\t\t\t 输入好友的名字或者输入0退出\n");
printf("\t\t\t\t");
scanf("%s", n);
if(strcmp(n, "0") == 0)
{
return 0;
}
else
{
strcpy(User.toname, n);
User.cmd = SHUTUP;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendshutup");
return 0;
}
}
}
//*******************************登陆后接收的消息***********************
void *Recv(void *sockfd)
{
FILE *fp;
int ret;
struct senduser Recv = {0};
int i;
char text3[30];
char text4[100];
while(1)
{
ret = recv(*(int *)sockfd, &Recv, sizeof(Recv), 0);
if(-1 == ret)
{
perror("recv");
exit(1);
}
switch(Recv.cmd)
{
case (MSG):
system("clear");
memset(text3, 0, sizeof(text3));
memset(text4, 0, sizeof(text4));
sprintf(text3, "%s和%s的聊天记录.text", Recv.toname, Recv.name);
fp = fopen(text3, "a");
if(fp == NULL)
{
perror("fopen");
exit(1);
}
sprintf(text4, "%s:%s", Recv.name, Recv.msg);
ret = fwrite(text4, 1, sizeof(text4), fp);
if(ret == 0)
{
perror("fwrite");
exit(1);
}
fclose(fp);
printf("\t\t\t\t%s向您发送了一条悄悄话:%s\n", Recv.name, Recv.msg);
printf("\t\t\t\t输入p返回\n");
memset(&Recv.cmd,0,sizeof(Recv.cmd));
break;
case (OK):
system("clear");
printf("\t\t\t\t发送成功!\n");
memset(&Recv.cmd,0,sizeof(Recv.cmd));
break;
case (UN):
system("clear");
printf("\t\t\t\t此用户不在线!\n");
memset(&Recv.cmd,0,sizeof(Recv.cmd));
break;
case (NUM):
system("clear");
printf("\t\t\t\t现在在线的人数是:%d\n", Recv.number);
if(Recv.number == 0)
{
printf("\t\t\t\t无人在线!\n");
break;
}
printf("\t\t\t\t在线的人是:\n");
printf("\t\t\t\t");
for(i = 0; i < Recv.number; i++)
{
printf("%s ", Recv.onlinenum[i]);
}
printf("\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (GROUP):
system("clear");
printf("\t\t\t\t收到来自%s的一条群发信息:\n", Recv.name);
printf("\t\t\t\t%s", Recv.msg);
printf("\n");
printf("\t\t\t\t输入p返回\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (ERR):
system("clear");
printf("\t\t\t\t无人在线!\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (PS):
system("clear");
sendps(Recv.p_s, Recv.name);
printf("\t\t\t\t输入p返回\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (ES):
system("clear");
sendes(Recv.e_s, Recv.name);
printf("\t\t\t\t输入p返回\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (OKK):
system("clear");
printf("\t\t\t\t禁言成功!\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (SHUTUP):
User.state = 1;
system("clear");
printf("\t\t\t\t 您已经被%s禁言了!\n", Recv.name);
printf("\t\t\t\t 开通会员即可解禁!\n");
printf("\t\t\t\t输入p返回\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (OKKK):
system("clear");
printf("\t\t\t\t踢人成功!\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
printf("\t\t\t\t 输入'p'返回\n");
break;
case (SENDFILE):
fp = fopen("MyRecv.text", "a");
ret = fwrite(Recv.filemsg, 1, strlen(Recv.filemsg), fp);
if(ret == 0)
{
perror("recvfile");
break;
}
fclose(fp);
system("clear");
printf("\t\t\t\t 您成功收到来自好友%s发送的一份文件!\n", Recv.name);
printf("\t\t\t\t输入p返回\n");
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
break;
case (OUTLINE):
OUT = 1;
system("clear");
printf("\t\t\t\t 您被好友%s踢下线!\n", Recv.name);
memset(&Recv.cmd, 0, sizeof(Recv.cmd));
printf("\t\t\t\t输入'x'返回\n");
break;
}
}
}
//************************聊天主界面***************
int menushow()
{
system("clear");
printf("\t\t\t\t");
gettime();
printf("\t\t\t\t*********************************\n");
printf("\t\t\t\t \n");
printf("\t\t\t\t 小王子聊天室 \n");
printf("\t\t\t\t \n");
printf("\t\t\t\t 昵称:%s \n", User.name);
printf("\t\t\t\t \n");
printf("\t\t\t\t a.悄悄话 \n");
printf("\t\t\t\t b.群发 \n");
printf("\t\t\t\t c.查看在线人数 \n");
printf("\t\t\t\t d.查看聊天记录 \n");
printf("\t\t\t\t e.发送常用语句 \n");
printf("\t\t\t\t f.发送常用表情 \n");
printf("\t\t\t\t g.办理会员 \n");
printf("\t\t\t\t h.禁言管理 \n");
printf("\t\t\t\t i.踢人管理 \n");
printf("\t\t\t\t j.文件传输 \n");
printf("\t\t\t\t z.下线 \n");
printf("\t\t\t\t \n");
printf("\t\t\t\t*********************************\n");
}
int menu(int sockfd)
{
FILE *fp;
int j = 0;
char bs;
char msgg[100];
int ret;
char choice[10];
char name1[20];
char text1[30];
char text2[100];
while(1)
{
menushow();
printf("\t\t\t\t选择您想要的功能:\n");
printf("\t\t\t\t");
scanf("%s", choice);
SCAN_N();
switch(choice[0])
{
case 'a':
system("clear");
printf("\t\t\t\t");
gettime();
if(User.state == 1)
{
printf("\t\t\t\t您已被禁言!\n");
sleep(1);
break;
}
printf("\t\t\t\t您想要发悄悄话给哪位好友?\n");
printf("\t\t\t\t");
scanf("%s", User.toname);
SCAN_N();
printf("\t\t\t\t请输入您想要发送的消息:\n");
printf("\t\t\t\t");
scanf("%s", User.msg);
SCAN_N();
memset(text1, 0, sizeof(text1));
memset(text2, 0, sizeof(text2));
sprintf(text1, "%s和%s的聊天记录.text", User.name, User.toname);
fp = fopen(text1, "a");
if(fp == NULL)
{
perror("open");
return FAILURE;
}
sprintf(text2, "%s:%s", User.name, User.msg);
ret = fwrite(text2, 1, sizeof(text2), fp);
if(ret == 0)
{
perror("write");
return FAILURE;
}
fclose(fp);
User.cmd = SEND;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("senfmsg");
return FAILURE;
}
sending();
sleep(1);
break;
case 'b':
system("clear");
if(User.state == 1)
{
printf("\t\t\t\t您已被禁言!\n");
sleep(1);
break;
}
printf("\t\t\t\t输入您想发送的话:\n");
printf("\t\t\t\t");
scanf("%s", User.msg);
SCAN_N();
User.cmd = GROUP;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("group");
return FAILURE;
}
sending();
sleep(1);
break;
case 'c':
User.cmd = NUM;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendnum");
return FAILURE;
}
break;
case 'd':
memset(name1, 0, sizeof(name1));
memset(text1, 0, sizeof(text1));
system("clear");
printf("\t\t\t\t请问想要查看和谁的聊天记录?\n");
scanf("%s", name1);
SCAN_N();
sprintf(text1, "%s和%s的聊天记录.text", User.name, name1);
fp = fopen(text1, "r");
if(fp == NULL)
{
printf("\t\t\t\t没有与此好友的聊天记录!\n");
break;
}
while(1)
{
memset(msgg, 0, sizeof(msgg));
ret = fread(msgg, 1, sizeof(msgg), fp);
if(ret == 0)
{
break;
}
printf("%s\n", msgg);
}
sleep(3);
fclose(fp);
break;
case 'e':
User.cmd = PS;
if(User.state == 1)
{
system("clear");
printf("\t\t\t\t您已被禁言!\n");
sleep(1);
break;
}
system("clear");
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t您想要发送给谁?\n");
printf("\t\t\t\t");
scanf("%s", User.toname);
SCAN_N();
system("clear");
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t 1.How are you doing?\n");
printf("\t\t\t\t 2.What's up?\n");
printf("\t\t\t\t 3.May I have your name, please?\n");
printf("\t\t\t\t 4.I'm glad to have met you.\n");
printf("\t\t\t\t 5.Keep in touch.\n");
printf("\t\t\t\t 6.Same to you.\n");
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t选择想要发送的常用语?\n");
scanf("%d", &User.p_s);
SCAN_N();
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendps");
return FAILURE;
}
sending();
sleep(1);
break;
case 'f':
if(User.state == 1)
{
system("clear");
printf("\t\t\t\t您已被禁言!\n");
sleep(1);
break;
}
User.cmd = ES;
system("clear");
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t您想要发送给谁?\n");
printf("\t\t\t\t");
scanf("%s", User.toname);
SCAN_N();
system("clear");
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t 1..҉٩(*´︶`*)۶҉我跳起来就是一个么么哒\n");
printf("\t\t\t\t 2..╮(︶﹏︶)鬼知道我经历了什么\n");
printf("\t\t\t\t 3.٩(๑´0`๑)۶ 还我假期\n");
printf("\t\t\t\t 4._(:ι」∠)_好饿,但是不想动\n");
printf("\t\t\t\t 5._(:3」∠)_ 我的床需要我!\n");
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t 选择您想要发送的表情\n");
scanf("%d", &User.e_s);
SCAN_N();
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendes");
return FAILURE;
}
sending();
sleep(1);
break;
case 'g':
system("clear");
int m;
int n;
printf("\t\t\t\t***********************************\n\n");
if(User.vip == 1)
{
printf("\t\t\t\t 您已办理会员!\n");
sleep(1);
break;
}
if(User.vip == 0)
{
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t您还不是狂炫酷霸拽的尊贵会员!\n\n");
printf("\t\t\t\t办理了会员您将享有不同的待遇,是否开通会员?\n");
printf("\t\t\t\t 1:开通 2:残忍拒绝(T-T...)\n");
printf("\t\t\t\t");
scanf("%d", &m);
SCAN_N();
if(m == 1)
{
system("clear");
printf("\t\t\t\t***********************************\n\n");
printf("\t\t\t\t包年会员现元旦享半价优惠!\n");
printf("\t\t\t\t 请您支付60元\n");
printf("\t\t\t\t 1:支付 2:残忍拒绝(T-T...)\n");
printf("\t\t\t\t");
scanf("%d", &n);
SCAN_N();
if(n == 1)
{
User.vip = 1;
User.cmd = VIP;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendvip");
return FAILURE;
}
printf("\t\t\t\t 恭喜您开通成功!\n");
}
else printf("\t\t\t\t 会员与您擦肩而过....\n");
sleep(1);
}
else break;
}
break;
case 'h':
if(User.vip != 1)
{
system("clear");
printf("\t\t\t\t**************************\n");
printf("\t\t\t\t您还未开通会员!\n");
sleep(1);
break;
}
shutup(sockfd);
break;
case 'i':
system("clear");
if(User.vip == 0)
{
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t您没有开通会员,不可以踢人!\n");
sleep(1);
break;
}
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t您想要踢哪位可怜的好友下线?\n");
printf("\t\t\t\t");
scanf("%s", User.toname);
SCAN_N();
User.cmd = OUTLINE;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("out");
return FAILURE;
}
break;
case 'j':
system("clear");
if(User.state == 1)
{
printf("\t\t\t\t您已被禁言!请先解禁!\n");
sleep(1);
break;
}
printf("\t\t\t\t***********************************\n");
printf("\t\t\t\t您想要发送给哪位好友?\n");
scanf("%s", User.toname);
SCAN_N();
printf("\t\t\t\t您想要发送哪份文件?\n");
scanf("%s", User.filename);
SCAN_N();
fp =fopen(User.filename, "r");
if(fp == NULL)
{
printf("\t\t\t\t 此文件不存在!\n");
sleep(1);
break;
}
while(1)
{
memset(&bs, 0, sizeof(bs));
ret = fread(&bs, 1, sizeof(bs), fp);
if(ret == 0)
{
break;
}
User.filemsg[j] = bs;
j++;
}
User.filemsg[j] = '\0';
User.cmd = SENDFILE;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("sendfile");
break;
}
fclose(fp);
sending();
sleep(1);
break;
case 'z':
User.cmd = EXIT;
ret = send(sockfd, &User, sizeof(User), 0);
if(ret == -1)
{
perror("exit");
return FAILURE;
}
return 2;
case 'p':
break;
case 'x':
return 2;
default:
system("clear");
printf("\t\t\t\t错误操作!\n");
printf("\t\t\t\t请选择正确的选项!\n");
sleep(1);
break;
}
}
}
int main()
{
//初始化和连接
int sockfd, ret;
struct sockaddr_in server_addr;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("192.168.1.119");
ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(ret == -1)
{
perror("connect");
exit(1);
}
//主界面
int choice;
show();
while(1)
{
mainshow();
printf("\t\t\t\t");
scanf("%d", &choice);
SCAN_N();
switch(choice)
{
case 1:
ret = login(sockfd);
if(ret == 1)
{
printf("\t\t\t\t登陆成功!\n");
sleep(1);
logging();
sleep(1);
}
if(ret == 3)
{
printf("\t\t\t\t此账号已登录!\n");
sleep(1);
break;
}
ret = pthread_create(&tid, NULL, Recv, (void *)&sockfd);
if(ret != 0)
{
perror("createrecv");
break;
}
pthread_detach(tid);
ret = menu(sockfd);
if(ret == 2)
{
pthread_cancel(tid);
break;
}
break;
case 2:
enroll(sockfd);
break;
case 3:
find(sockfd);
break;
case 4:
close(sockfd);
exit(1);
break;
default:
system("clear");
printf("\t\t\t\t错误选项!请在1-4中选择!\n");
sleep(1);
break;
}
}
return 0;
}