> File Name: server.c
> Author: Yduo
> Mail: 1587202069@qq.com
> Created Time: 2016年01月20日 星期三 15时34分31秒
************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <pthread.h>
#include <sqlite3.h>
#define MAX 1024
#define LISTEN_PPLE 20
#define DATA_MAX 10
#define FILE_MAX_LEN 64
/*
*装注册信息
*/
typedef struct
{
int chse;
char name[DATA_MAX][DATA_MAX];
char pwd[DATA_MAX][DATA_MAX];
char DATA[DATA_MAX][DATA_MAX];
}login;
/*
*装在线的人
*/
typedef struct node
{
int online;
char on_name[DATA_MAX];
char on_bardian_name[DATA_MAX];
int Adm_flag; //管理员标志,是管理员时为10
int speak_flag; //禁言标志
struct node *next;
}linkedlist;
linkedlist *list;
int fd_array[20];
int flag_count_2;
int chat_room_init(const char *ip, int port); //初始化
int chat_server_accept(int socket_fd); //服务器接收
static void *handle_client_login(void *arg);
static int sqlitest_online(void *arg, char *name, char *pwd, char *DATA);
static char *login_server(void *arg, char *name, char *pwd, char *DATA);
void print(linkedlist *list, void *arg);
void personal_chat(void *arg);
linkedlist *querysock_fd(linkedlist *L, int sock_fd);
int getcount(linkedlist *list);
linkedlist *insert_online_count(linkedlist *list, char *name, char *DATA, void *arg);
int chsefunc(void *arg);
linkedlist *create_online_count(linkedlist *list);
int queryName(linkedlist *L, char *name);
linkedlist *querysock_fd(linkedlist *L, int sock_fd);
int group_chat(void *arg);
int file_transmit(void *arg);
void Whisper_chat(void *arg);
void Administrator(void *arg);
void Administrator_Appli(void *arg);
int Adm_queryName(linkedlist *L, char *name);
void Administrator_Delete(void *arg);
int Administrator_judge(linkedlist *L, char *name);
int Delete(char *name);
void Administrator_Speak(void *arg);
int Speak_queryName(linkedlist *L, char *name);
void Administrator_Speak_no(void *arg);
int no_Speak_queryName(linkedlist *L, char *name);
int Speak_queryName_cheak(linkedlist *L, char *name);
void off_line_Delete(void *arg);
sqlite3 *db;
char *ErrMsg;
/*
*定义全局为私聊做准备
*/
login *login_sock;
int main(int argc, char *argv[])
{
int ret;
pthread_t client;
int i;
char *buffer;
int login_fd;
if(3 != argc)
{
printf("Usage : ./server ip port\n");
exit(EXIT_FAILURE);
}
/*
*打开数据库
*/
ret = sqlite3_open("./sql_client_data.db", &db);
if(ret != SQLITE_OK)
{
printf("error : open\n");
}
char *create_str = "create table sql_client_data (name Test primary key, pwd Test, DATA Test);";
char *create_msg = "create table if not exists chat_msg(online integer primary key, name Test, toname Test ,messages Test,time Test);";
char *create_off = "create table if not exists offline_data(online integer primary key, name Test, toname Test,messages Test,time Test);";
ret = sqlite3_exec(db, create_str, 0, 0, &ErrMsg);
if(ret != SQLITE_OK)
{
printf("error : create %s\n", sqlite3_errmsg(db));
}
ret = sqlite3_exec(db, create_msg, 0, 0, &ErrMsg);
if(ret != SQLITE_OK) //聊天记录
{
printf("create chat_msg error:%s",sqlite3_errmsg(db));
}
ret = sqlite3_exec(db ,create_off, 0, 0, &ErrMsg);
if(ret != SQLITE_OK) //离线消息
{
printf("create chat_msg error:%s",sqlite3_errmsg(db));
}
/*
*创建一个链表
*/
create_online_count(list);
/*
*初始化操作
*/
int socket_fd = chat_room_init(argv[1], atoi(argv[2])); //把参数传入
while(1)
{
login_fd = chat_server_accept(socket_fd); //用户登录注册
if(-1 == login_fd)
{
perror("accept");
}
printf("当前用户的new_fd = %d\n", login_fd);
/*
*创建一个进程和客户通信,父进程继续监听
*/
pthread_create(&client, NULL, handle_client_login, (void *)login_fd);
}
}
/*---------------------------------------------------------------
*用于注册登录
*----------------------------------------------------------------*/
static void *handle_client_login(void *arg)
{
int ret;
int i;
int new_fd = (int)arg;
int my_fd = -1;
/*
*接收选择
*/
while(1)
{
char *buffer;
buffer = (char *)malloc(sizeof(login));
login *logindata = (login *)malloc(sizeof(login));
ret = recv(new_fd, buffer, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(new_fd);
exit(EXIT_FAILURE);
}
memcpy(logindata, buffer, sizeof(login));
int t = logindata -> chse; //用logindata -> chse的值进行判断
printf("t = %d\n", t);
if(t == 0 )
{
break;
}
else if (t == 1)
{
/*
*进行登录
*/
login_server((void *)new_fd, logindata -> name[1], logindata -> pwd[1], logindata -> DATA[1]);
break;
}
else if(t == 2)
{
/*
*创建一个数据库
*/
sqlitest_online((void *)new_fd, logindata -> name[1], logindata -> pwd[1], logindata -> DATA[1]);
break;
}
}
}
/*---------------------------------------------------------------
*初始化子程序
*----------------------------------------------------------------*/
int chat_room_init(const char *ip, int port) //初始化
{
int ret;
int socket_fd;
/*
*创建一个数据库
*/
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == socket_fd)
{
perror("socket");
exit(EXIT_FAILURE);
}
printf("建立socket成功\n");
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(struct sockaddr)); //清空
server_addr.sin_family = AF_INET; //设置网络协议
server_addr.sin_port = htons(port); //设置端口号
server_addr.sin_addr.s_addr = INADDR_ANY; //任意ip都可以通信
/*
*将新的socket与制定的ip,port绑定
*/
ret = bind(socket_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
if(-1 == ret)
{
perror("bind");
close(socket_fd);
exit(EXIT_FAILURE);
}
printf("bind成功\n");
/*
*监听它,并设置其允许最大的连接数,LISTEN_PPLE进行了宏定义
*/
ret = listen(socket_fd, LISTEN_PPLE);
if(-1 == ret)
{
perror("listen");
close(socket_fd);
exit(EXIT_FAILURE);
}
printf("listen成功\n");
return socket_fd;
}
/*---------------------------------------------------------------
*服务器接收子程序
*---------------------------------------------------------------*/
int chat_server_accept(int socket_fd)
{
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(struct sockaddr));
int addrlen = sizeof(struct sockaddr);
/*
*socket_fd接受客户端连接,并创建新的socket为new_fd,将请求连接的客户端IP、
*port保存在client_addr
*/
int new_fd = accept(socket_fd, (struct sockaddr *)&client_addr, &addrlen);
if(-1 == new_fd)
{
perror("accept");
close(socket_fd);
exit(EXIT_FAILURE);
}
printf("success connet. client_new_fd = %d\n", new_fd);
return new_fd;
}
/*----------------------------------------------------------------
*创建一个数据库
*-----------------------------------------------------------------*/
static int sqlitest_online(void *arg, char *name, char *pwd, char *DATA)
{
int ret;
int new_fd = (int)arg;
login *logindata;
logindata = (login *)malloc(sizeof(login));
memset(logindata, 0, sizeof(login));
memcpy(logindata -> name[1], name, sizeof(logindata -> name[1]));
memcpy(logindata -> pwd[1], name, sizeof(logindata -> pwd[1]));
memcpy(logindata -> DATA[1], name, sizeof(logindata -> DATA[1]));
char *buffer =(char *)malloc(sizeof(login));
/*
*这段代码为了几个一起操作需要放到主函数执行
*/
/*char *create_str = "create table sql_client_data (name Test primary key, pwd Test, DATA Test);";
ret = sqlite3_open("./sql_client_data.db", &db);
if(ret != SQLITE_OK)
{
printf("error : open\n");
}
ret = sqlite3_exec(db, create_str, 0, 0, &ErrMsg);
if(ret != SQLITE_OK)
{
printf("error : create %s\n", sqlite3_errmsg(db));
}*/
char *insert_str = "insert into sql_client_data (name, pwd, DATA) values('%s', '%s', '%s');";
char str[MAX];
printf("name : %s\n", logindata -> name[1]);
sprintf(str, insert_str, logindata -> name[1], logindata -> pwd[1], logindata -> DATA[1]);
printf("%s\n", str);
ret = sqlite3_exec(db, str, 0, 0, &ErrMsg);
printf("ret = %d\n", ret);
/*
*注册失败给客户端发送提示原因
*/
if(ret != SQLITE_OK)
{
login *logindata3;
logindata3 = (login *)malloc(sizeof(login));
memset(logindata3, 0, sizeof(login));
logindata3 -> chse = 3;
printf("logindata3 -> chse = %d\n", logindata3 -> chse);
char *buffer3 = (char *)malloc(sizeof(logindata3));
memcpy(buffer3, logindata3, sizeof(logindata3));
/*
*注册失败后发回3
*/
ret = send(new_fd, buffer3, sizeof(logindata3), 0);
printf("login error\n");
if(-1 == ret)
{
perror("send");
close(new_fd);
exit(EXIT_FAILURE);
}
}
/*
*注册成功给客户端发送提示
*/
else
{
login *logindata2;
logindata2 = (login *)malloc(sizeof(login));
memset(logindata2, 0, sizeof(login));
logindata2 -> chse = 2;
printf("logindata2 -> chse = %d\n", logindata2 -> chse);
char *buffer2 = (char *)malloc(sizeof(logindata2));
memcpy(buffer2, logindata2, sizeof(logindata2));
/*
*注册成功后发回2
*/
ret = send(new_fd, buffer2, sizeof(logindata2), 0);
printf("login success\n");
if(-1 == ret)
{
perror("send");
close(new_fd);
exit(EXIT_FAILURE);
}
}
close(new_fd);
}
/*----------------------------------------------------------------
*登录检索数据库
*-----------------------------------------------------------------*/
static char *login_server(void *arg, char *name, char *pwd, char *DATA)
{
int ret;
int flag_count_1;
int i;
int new_fd = (int)arg;
sqlite3_stmt *stmt;
char str[100];
char str_n[100];
char str_b[100];
login *logindata;
logindata = (login *)malloc(sizeof(login));
memset(logindata, 0, sizeof(login));
memcpy(logindata -> name[1], name, sizeof(logindata -> name[1]));
memcpy(logindata -> pwd[1], name, sizeof(logindata -> pwd[1]));
memcpy(logindata -> DATA[1], name, sizeof(logindata -> DATA[1]));
char *buffer =(char *)malloc(sizeof(login));
/*
*打开表
*/
/*
ret = sqlite3_open("./sql_client_data.db", &db);
if(ret != SQLITE_OK)
{
printf("error : open\n");
}*/
/*
*count函数统计
*/
char *select_str = "select count(*) from sql_client_data where name = '%s';";
sprintf(str, select_str, logindata -> name[1]);
printf("str = %s\n", str);
/*
*准备执行
*/
ret = sqlite3_prepare(db, str, -1, &stmt, 0);
if(ret != SQLITE_OK)
{
login *logindata7;
logindata7 = (login *)malloc(sizeof(login));
memset(logindata7, 0, sizeof(login));
logindata7 -> chse = 6;
printf("logindata7 -> chse = %d\n", logindata7 -> chse);
char *sendbuf = (char *)malloc(sizeof(logindata7));
memcpy(sendbuf, logindata7, sizeof(logindata7));
/*
*错误后发回6
*/
ret = send(new_fd, sendbuf, sizeof(logindata7), 0);
printf("error :count\n");
if(-1 == ret)
{
perror("send");
close(new_fd);
exit(EXIT_FAILURE);
}
close(new_fd);
free(sendbuf);
}
printf("sqlite3_prepare\n");
ret = sqlite3_step(stmt);
printf("sqlite3_step\n");
char *n_Count = (char *)sqlite3_column_text(stmt, 0);
/*
*名字错误
*/
if(0 == strcmp(n_Count, "0"))
{
login *logindata5;
logindata5 = (login *)malloc(sizeof(login));
memset(logindata5, 0, sizeof(login));
logindata5 -> chse = 4;
printf("logindata5 -> chse = %d\n", logindata5 -> chse);
char *buffer5 = (char *)malloc(sizeof(logindata5));
memcpy(buffer5, logindata5, sizeof(logindata5));
/*
*名字错误后发回4
*/
ret = send(new_fd, buffer5, sizeof(logindata5), 0);
printf("This client don't login\n");
if(-1 == ret)
{
perror("send");
close(new_fd);
exit(EXIT_FAILURE);
}
free(buffer5);
}
/*
*名字对
*/
else if(1 == strcmp(n_Count, "0"))
{
printf("查密码\n");
char *select_str_p = "select count(*) from sql_client_data where pwd = '%s';";
sprintf(str, select_str_p, logindata -> pwd[1]);
printf("str = %s\n", str);
ret = sqlite3_prepare(db, str, -1, &stmt, 0);
if(ret != SQLITE_OK)
{
printf("error :count\n");
}
ret = sqlite3_step(stmt);
char *p_Count = (char *)sqlite3_column_text(stmt, 0);
if(1 == strcmp(p_Count, "0"))
{
login *logindata1;
logindata1 = (login *)malloc(sizeof(login));
memset(logindata1, 0, sizeof(login));
logindata1 -> chse = 1;
printf("logindata1 -> chse = %d\n", logindata1 -> chse);
char *buffer = (char *)malloc(sizeof(logindata1));
memcpy(buffer, logindata1, sizeof(logindata1));
/*
*登录成功后发回1
*/
ret = send(new_fd, buffer, sizeof(logindata1), 0);
printf("login success\n");
if(-1 == ret)
{
perror("send");
close(new_fd);
exit(EXIT_FAILURE);
}
/*
*把xinxi复制
*/
sprintf(str_n, "%s", logindata -> name[1]);
/*
*把名字装进去,做为名片
*/
login_sock = (login *)malloc(sizeof(login));
sprintf(login_sock -> name[1], "%s", logindata -> name[1]);
printf("login_sock ->name[1] = %s\n", login_sock ->name[1]);
sprintf(str_b, "%s", logindata -> DATA[1]);
printf("str_n = %s\n", str_n);
printf("str_b = %s\n", str_b);
list = insert_online_count(list, str_n, str_b, (void *)new_fd);
print(list, (void *)new_fd);
chsefunc((void *)new_fd);
}
else if(0 == strcmp(p_Count, "0"))
{
printf("new_fd = %d\n", new_fd);
login *logindata6;
logindata6 = (login *)malloc(sizeof(login));
memset(logindata6, 0, sizeof(login));
logindata6 -> chse = 5;
printf("logindata6 -> chse = %d\n", logindata6 -> chse);
char *buffer6 = (char *)malloc(sizeof(logindata6));
memcpy(buffer6, logindata6, sizeof(logindata6));
/*
*密码错误后发回5
*/
ret = send(new_fd, buffer6, sizeof(logindata6), 0);
printf("pwd is error\n");
if(-1 == ret)
{
perror("send");
close(new_fd);
exit(EXIT_FAILURE);
}
}
}
}
/*
*创建一个空表
*/
linkedlist *create_online_count(linkedlist *list)
{
list = (linkedlist *)malloc(sizeof(linkedlist));
list = NULL;
return list;
}
/*
*统计个数
*/
int getcount(linkedlist *list)
{
int count = 0;
while(NULL != list)
{
printf("正在计数list ->name %s\n", list -> on_name);
count ++ ;
list = list -> next;
}
return count;
}
/*
*插入
*/
linkedlist *insert_online_count(linkedlist *list, char *name, char *DATA, void *arg)
{
int sock_fd =(int)arg;
linkedlist *head, *online_people;
head = list;
online_people = (linkedlist *)malloc(sizeof(linkedlist));
if(head == NULL)
{
head = online_people;
online_people -> next = NULL;
}
else
{
online_people -> next = head;
head = online_people;
}
online_people -> online = sock_fd;
printf("online_people -> online = %d\n", online_people -> online);
sprintf(online_people -> on_name, "%s", name);
printf("online_people -> on_name = %s\n", online_people -> on_name);
sprintf(online_people -> on_bardian_name, "%s", DATA);
printf("online_people -> on_bardian_name = %s\n", online_people -> on_bardian_name);
return head;
}
/*
*打印发送函数
*/
void print(linkedlist *list, void *arg)
{
int ret;
int login_fd = (int)arg;
while(list != NULL)
{
int t = getcount(list);
login *logindata;
logindata = (login *)malloc(sizeof(login));
logindata -> chse = t;
printf("当前在线人数的个数%d\n", logindata -> chse);
memcpy(logindata -> name[1], list -> on_name, sizeof(list -> on_name));
/*
*在线人的名字
*/
printf("%s\n", logindata -> name[1]);
char *buffer = (char *)malloc(sizeof(login));
memcpy(buffer, logindata, sizeof(login));
memset(logindata, 0, sizeof(login));
ret = send(login_fd, buffer, sizeof(login), 0);
list = list -> next;
free(buffer);
}
}
/*
*查名字
*/
int queryName(linkedlist *L, char *name)
{
while ( L != NULL)
{
if (!strcmp(L -> on_name, name))
{
return L -> online;
}
else
{
L = L -> next;
}
}
}
/*
*判断是否为管理员
*/
int Administrator_judge(linkedlist *L, char *name)
{
while ( L != NULL)
{
if (!strcmp(L -> on_name, name))
{
return L -> Adm_flag;
}
else
{
L = L -> next;
}
}
}
/*
*管理员申请操作存储
*/
int Adm_queryName(linkedlist *L, char *name)
{
while ( L != NULL)
{
if (!strcmp(L -> on_name, name))
{
return L -> Adm_flag = 10;
}
else
{
L = L -> next;
}
}
}
/*
*禁言操作
*/
int Speak_queryName(linkedlist *L, char *name)
{
while ( L != NULL)
{
if (!strcmp(L -> on_name, name))
{
return L -> speak_flag = 10;
}
else
{
L = L -> next;
}
}
}
/*
*查看是否被禁言
*/
int Speak_queryName_cheak(linkedlist *L, char *name)
{
while ( L != NULL)
{
if (!strcmp(L -> on_name, name))
{
return L -> speak_flag;
}
else
{
L = L -> next;
}
}
}
/*
*解除禁言操作
*/
int no_Speak_queryName(linkedlist *L, char *name)
{
while ( L != NULL)
{
if (!strcmp(L -> on_name, name))
{
return L -> speak_flag = 5;
}
else
{
L = L -> next;
}
}
}
/*
*功能选择
*/
int chsefunc(void *arg)
{
while(1)
{
printf("选择\n");
int ret;
int new_fd = (int)arg;
char buffer[1024];
memset(buffer, 0, 1024);
//buffer = (char *)malloc(sizeof(linkedlist));
ret = recv(new_fd, buffer, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(new_fd);
exit(EXIT_FAILURE);
}
login *loginchse;
loginchse = (login *)malloc(sizeof(login));
memset(loginchse, 0, sizeof(login));
memcpy(loginchse, buffer, sizeof(login));
printf("loginchse -> chse = %d\n", loginchse -> chse);
switch(loginchse -> chse)
{
/*
*私聊
*/
case 6:
{
personal_chat((void *)new_fd);
break;
}
/*
*群聊
*/
case 7:
{
group_chat((void *)new_fd);
break;
}
/*
*悄悄话
*/
case 8:
{
Whisper_chat((void *)new_fd);
break;
}
/*
*文件传送
*/
case 9:
{
file_transmit((void *)new_fd);
break;
}
/*
*管理员
*/
case 10:
{
Administrator((void *)new_fd);
printf("Test A\n");
break;
}
case 11:
{
off_line_Delete((void *)new_fd);
break;
}
}
if(0 == loginchse -> chse)
{
close(new_fd);
break;
}
printf("Test B\n");
}
}
void Administrator(void *arg)
{
printf("管理员\n");
int ret;
int new_fd = (int)arg;
char buffer[1024];
memset(buffer, 0, 1024);
printf("Test1\n");
ret = recv(new_fd, buffer, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(new_fd);
exit(EXIT_FAILURE);
}
printf("Test2\n");
login *loginchse;
loginchse = (login *)malloc(sizeof(login));
memset(loginchse, 0, sizeof(login));
memcpy(loginchse, buffer, sizeof(login));
printf("loginchse -> chse = %d\n", loginchse -> chse);
switch(loginchse -> chse)
{
case 1:
{
printf("申请\n");
Administrator_Appli((void *)new_fd);
printf("完成\n");
break;
}
case 2:
{
printf("T人n");
Administrator_Delete((void *)new_fd);
break;
}
case 3:
{
Administrator_Speak((void *)new_fd);
break;
}
case 4:
{
Administrator_Speak_no((void *)new_fd);
break;
}
printf("Test1\n");
}
printf("Test2\n");
}
/*
*解除禁言
*/
void Administrator_Speak_no(void *arg)
{
printf("解除禁言\n");
int ret;
int my_fd = (int)arg;
int fd;
char buf[1024];
memset(buf, 0, 1024);
printf("Test1\n");
ret = recv(my_fd, buf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
printf("Test2\n");
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, buf, sizeof(login));
printf("my_fd = %d\n", my_fd);
fd = Administrator_judge(list, loginsend -> name[2]);
printf("fd = %d\n", fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
if(10 == fd)
{
printf("Test1\n");
int S_fd = no_Speak_queryName(list, loginsend -> name[1]);
printf("s_fd = %d\n", S_fd);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
if(5 == S_fd)
{
int target_fd = queryName(list, loginsend -> name[1]);
send_login -> chse = 18;
printf("send_login -> chse = %d\n", send_login -> chse);
sprintf(send_login -> name[1], "%s", loginsend -> name[2]);
printf("send_login -> name[1] = %s\n", send_login -> name[1]);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(target_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(target_fd);
exit(EXIT_FAILURE);
}
}
else
{
send_login -> chse = 19;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
else
{
/*
*用于客户端判断
*/
send_login -> chse = 13;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
/*
*禁言
*/
void Administrator_Speak(void *arg)
{
printf("禁言\n");
int ret;
int my_fd = (int)arg;
int fd;
char buf[1024];
memset(buf, 0, 1024);
printf("Test1\n");
ret = recv(my_fd, buf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
printf("Test2\n");
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, buf, sizeof(login));
printf("my_fd = %d\n", my_fd);
fd = Administrator_judge(list, loginsend -> name[2]);
printf("fd = %d\n", fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
if(10 == fd)
{
printf("Test1\n");
int S_fd = Speak_queryName(list, loginsend -> name[1]);
printf("s_fd = %d\n", S_fd);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
if(10 == S_fd)
{
int target_fd = queryName(list, loginsend -> name[1]);
send_login -> chse = 16;
printf("send_login -> chse = %d\n", send_login -> chse);
sprintf(send_login -> name[1], "%s", loginsend -> name[2]);
printf("send_login -> name[1] = %s\n", send_login -> name[1]);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(target_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(target_fd);
exit(EXIT_FAILURE);
}
}
else
{
send_login -> chse = 17;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
else
{
/*
*用于客户端判断
*/
send_login -> chse = 13;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
/*
*T人
*/
void Administrator_Delete(void *arg)
{
int ret;
int my_fd = (int)arg;
int fd;
char buf[1024];
memset(buf, 0, 1024);
printf("Test1\n");
ret = recv(my_fd, buf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
printf("Test2\n");
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, buf, sizeof(login));
printf("my_fd = %d\n", my_fd);
fd = Administrator_judge(list, loginsend -> name[2]);
printf("fd = %d\n", fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
if(10 == fd)
{
printf("Test1\n");
/*
*用于客户端判断
*/
int target_fd = queryName(list, loginsend -> name[1]);
send_login -> chse = 12;
printf("send_login -> chse = %d\n", send_login -> chse);
sprintf(send_login -> name[1], "%s", loginsend -> name[2]);
printf("send_login -> name[1] = %s\n", send_login -> name[1]);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(target_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(target_fd);
exit(EXIT_FAILURE);
}
int D_fd = Delete(loginsend -> name[1]);
printf("D_FD = %d\n", D_fd);
if(0 == D_fd)
{
/*
*用于客户端判断
*/
send_login -> chse = 14;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("D_fdtest5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("my_fd = %d\n", my_fd);
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
else
{
/*
*用于客户端判断
*/
send_login -> chse = 15;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
else
{
/*
*用于客户端判断
*/
send_login -> chse = 13;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
void off_line_Delete(void *arg)
{
int ret;
int my_fd = (int)arg;
int fd;
char buf[1024];
memset(buf, 0, 1024);
printf("Test1\n");
ret = recv(my_fd, buf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
printf("Test2\n");
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, buf, sizeof(login));
printf("my_fd = %d\n", my_fd);
printf("loginsend -> name = %s\n", loginsend -> name[2]);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
printf("Test1\n");
/*
*用于客户端判断
*/
send_login -> chse = 21;
printf("send_login -> chse = %d\n", send_login -> chse);
sprintf(send_login -> name[1], "%s", loginsend -> name[2]);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
printf("loginsend -> name[1] = %s\n", send_login -> name[1]);
int D_fd = Delete(send_login -> name[1]);
printf("test7\n");
if(0 == D_fd)
{
printf("test7\n");
close(my_fd);
}
printf("D_fd = %d\n", D_fd);
}
int Delete(char *name)
{
linkedlist *p = list -> next, *p1 = list -> next;
while((strcmp(p -> on_name, name) != 0 ) && (p -> next != NULL))
{
p1 = p;
p = p -> next;
}
if(strcmp(name, p -> on_name) == 0) //输出删除信息
{
if ((p == list -> next) && (p -> next != NULL))
{
list -> next = p -> next;
}
else if((p == list -> next) && (p -> next == NULL))
{
list -> next = p -> next;
return 0;
}
else
{
p1 -> next = p -> next;
}
}
else
{
printf("没有这个人\n");
return 1;
}
}
/*
*管理员申请
*/
void Administrator_Appli(void *arg)
{
int ret;
int my_fd = (int)arg;
int fd;
char buf[1024];
memset(buf, 0, 1024);
ret = recv(my_fd, buf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, buf, sizeof(login));
printf("my_fd = %d\n", my_fd);
fd = Adm_queryName(list, loginsend -> name[2]);
printf("管理员fd = %d\n", fd);
/*
*用于客户端判断
*/
login *send_login = (login *)malloc(sizeof(login));
send_login -> chse = 11;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf_s[1024];// = (char *)malloc(sizeof(login));
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
/*
*私聊
*/
void personal_chat(void *arg)
{
int ret;
int my_fd = (int)arg;
int target_fd;
char sendbuf[1024];
memset(sendbuf, 0, 1024);
ret = recv(my_fd, sendbuf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, sendbuf, sizeof(login));
printf("my_fd = %d\n", my_fd);
target_fd = queryName(list, loginsend -> name[1]);
ret = Speak_queryName_cheak(list, loginsend -> name[2]);
printf("ret = %d\n", ret);
if(10 == ret)
{
printf("my_fd = %d\n", my_fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
/*
*用于客户端判断
*/
send_login -> chse = 20;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf[1024];// = (char *)malloc(sizeof(login));
memset(buf, 0, 1024);
memcpy(buf, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
else
{
printf("target_fd = %d\n", target_fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
sprintf(send_login -> DATA[1], "%s", loginsend ->DATA[1]);
printf("send_login -> DATA[1] = %s\n", send_login -> DATA[1]);
/*
*用于客户端判断
*/
send_login -> chse = 7;
printf("send_login -> chse = %d\n", send_login -> chse);
sprintf(send_login -> name[1], "%s", loginsend -> name[2]);
printf("send_login -> name[1] = %s\n", send_login -> name[1]);
char buf[1024];// = (char *)malloc(sizeof(login));
memset(buf, 0, 1024);
memcpy(buf, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(target_fd, buf, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
/*
*悄悄话
*/
void Whisper_chat(void *arg)
{
int ret;
int my_fd = (int)arg;
int target_fd;
char sendbuf[1024];
memset(sendbuf, 0, 1024);
ret = recv(my_fd, sendbuf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
login *loginsend = (login *)malloc(sizeof(login));
memset(loginsend, 0, sizeof(login));
memcpy(loginsend, sendbuf, sizeof(login));
printf("my_fd = %d\n", my_fd);
target_fd = queryName(list, loginsend -> name[1]);
ret = Speak_queryName_cheak(list, loginsend -> name[2]);
if(10 == ret)
{
printf("my_fd = %d\n", my_fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
/*
*用于客户端判断
*/
send_login -> chse = 20;
printf("send_login -> chse = %d\n", send_login -> chse);
char buf[1024];// = (char *)malloc(sizeof(login));
memset(buf, 0, 1024);
memcpy(buf, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
else
{
printf("target_fd = %d\n", target_fd);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
sprintf(send_login -> DATA[1], "%s", loginsend ->DATA[1]);
printf("send_login -> DATA[1] = %s\n", send_login -> DATA[1]);
/*
*用于客户端判断
*/
send_login -> chse = 10;
printf("send_login -> chse = %d\n", send_login -> chse);
sprintf(send_login -> name[1], "%s", loginsend -> name[2]);
printf("send_login -> name[1] = %s\n", send_login -> name[1]);
char buf[1024];// = (char *)malloc(sizeof(login));
memset(buf, 0, 1024);
memcpy(buf, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(target_fd, buf, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
}
/*
*群组聊天
*/
int group_chat(void *arg)
{
printf("test1\n");
int ret;
int my_fd = (int)arg;
int other_fd;
printf("test2\n");
char groupbuf[1024]; //= (char *)malloc(sizeof(login));
memset(groupbuf, 0, 1024);
printf("test3\n");
ret = recv(my_fd, groupbuf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(my_fd);
exit(EXIT_FAILURE);
}
printf("test4\n");
login *group_login = (login *)malloc(sizeof(login));
memset(group_login, 0, sizeof(login));
memcpy(group_login, groupbuf, sizeof(login));
ret = Speak_queryName_cheak(list, group_login -> name[2]);
printf("ret = %d\n", ret);
if(10 == ret)
{
group_login -> chse = 20;
printf("group_login -> chse = %d\n", group_login -> chse);
char buf[1024];// = (char *)malloc(sizeof(login));
memset(buf, 0, 1024);
memcpy(buf, group_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
ret = send(my_fd, buf, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(my_fd);
exit(EXIT_FAILURE);
}
}
else
{
group_login -> chse = 8;
printf("group_login -> name[1] = %s\n", group_login -> name[1]);
char buffer[1024];
memset(buffer, 0, 1024);
memcpy(buffer, group_login, sizeof(login));
login *logindata;
logindata = (login *)malloc(sizeof(login));
memset(logindata, 0, sizeof(login));
int i = getcount(list);
printf("i = %d\n", i);
int j;
linkedlist *p;
p = list;
printf("list ->name %s\n", p -> on_name);
printf("group_login ->name[1] %s\n", group_login ->name[1]);
while(NULL != p)
{
printf("正在计数list ->name %s\n", p -> on_name);
if(strcmp(p -> on_name, group_login ->name[1]))
{
printf("send\n");
other_fd = queryName(p, p -> on_name);
ret = send(other_fd, buffer, sizeof(login), 0);
if(ret == -1)
{
close(other_fd);
}
}
p = p -> next;
}
p = list -> next;
}
}
/*----------------------------------------------------------------------------
*文件传送
------------------------------------------------------------------------------*/
int file_transmit(void *arg)
{
FILE *file = NULL;
int my_fd = (int)arg;
int len;
char filename[FILE_MAX_LEN + 1];
char buffer[MAX];
login *File_data;
File_data = (login *)malloc(sizeof(login));
printf("接收文件\n");
len = recv(my_fd, buffer, sizeof(login), 0);
memcpy(File_data, buffer, sizeof(login));
printf("File_data -> name[1] %s\n", File_data -> name[1]); //发送的对象
int target_fd = queryName(list, File_data -> name[1]);
login *send_login = (login *)malloc(sizeof(login));
memset(send_login, 0, sizeof(login));
sprintf(send_login -> DATA[1], "%s", File_data ->DATA[1]); //文件名
printf("send_login -> DATA[1] = %s\n", send_login -> DATA[1]);
sprintf(send_login -> DATA[2], "%s", File_data -> DATA[2]); //文件的内容
printf("send_login -> DATA[2] = %s\n", send_login -> DATA[2]);
/*
*用于客户端判断
*/
send_login -> chse = 9;
printf("send_login -> chse = %d\n", send_login -> chse); //通知消息
sprintf(send_login -> name[1], "%s", File_data -> name[2]); //发文件人的姓名
printf("send_login -> name[1] = %s\n", send_login -> name[1]);
char buf_s[1024];
memset(buf_s, 0, 1024);
memcpy(buf_s, send_login, sizeof(login));
/*
*向目标发送消息
*/
printf("test5\n");
int ret = send(target_fd, buf_s, sizeof(login), 0);
printf("test6\n");
if(-1 == ret)
{
perror("send");
close(target_fd);
exit(EXIT_FAILURE);
}
}
/*************************************************************************
> File Name: client.c
> Author: Yduo
> Mail: 1587202069@qq.com
> Created Time: 2016年01月20日 星期三 15时34分52秒
************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 1024
#define DATA_MAX 10
#define FILE_MAX_LEN 64
typedef struct
{
int chse;
char name[DATA_MAX][DATA_MAX];
char pwd[DATA_MAX][DATA_MAX];
char DATA[DATA_MAX][DATA_MAX];
}login;
/*
*装在线的人
*/
typedef struct node
{
char on_name[DATA_MAX];
char on_bardian_name[DATA_MAX];
struct node *next;
}linkedlist;
int chat_count_client(const char *ip, int port); //客户端连接函数
static void *readfunc(void *arg);
int func(void *arg, char *name);
int choose_func(int online_flag, void *arg);
int chat_send(void *arg, char *name);
linkedlist *create_online_count(char *name, char *DATA);
int display(void *arg);
int chat_group(void *arg, char *name);
static void *readfunc_2(void *arg);
int recv_fife(void *arg);
int send_file(void *arg, char *name);
int Administrator(void *arg, char *name);
int Whisper_send(void *arg, char *name);
int Administrator_send(void *arg, char *name);
int Administrator_Appli_send(void *arg, char *name);
int off_line_send(void *arg, char *name);
/*
*定义全局标志位
*/
static int flag_online_1 = 0;
int main(int argc, char *argv[])
{
int ret;
int choose;
if(3 != argc)
{
printf("Usage : ./client ip port\n");
exit(EXIT_FAILURE);
}
/*----------------------------------------------------------------
*界面信息
*----------------------------------------------------------------*/
printf("*********************************************************\n");
printf("* *\n");
printf("* 欢迎使用 *\n");
printf("* *\n");
printf("*********************************************************\n");
sleep(2);
printf("*********************************************************\n");
printf("* *\n");
printf("* 1、登录 2、注册 3、退出 *\n");
printf("* *\n");
printf("*********************************************************\n");
while(1)
{
/*------------------------------------------------------------
*连接服务器并发送请求
*-------------------------------------------------------------*/
scanf("%d", &choose); //接收选择
printf("%d\n", choose);
int chat_fd = chat_count_client(argv[1], atoi(argv[2]));
//pthread_create(&client, NULL, readfunc, (void *)chat_fd);
/*
*退出
*/
if(3 == choose)
{
break;
}
switch(choose)
{
case 1:
{
/*
*登录
*/
printf("请输入以下信息:\n");
/*
*告诉服务器你的选择
*/
login *chat_data;
chat_data = (login *)malloc(sizeof(login));
memset(chat_data, 0, sizeof(login));
chat_data -> chse = choose;
printf("chat_data -> chse = %d\n", chat_data -> chse);
printf("**********name***********\n");
scanf("%s", chat_data -> name[1]);
printf("**********pwd***********\n");
scanf("%s", chat_data -> pwd[1]);
char *buffer = (char *)malloc(sizeof(login));
memcpy(buffer, chat_data, sizeof(login));
/*
*向login_fd服务端发送数据
*/
ret = send(chat_fd, buffer, sizeof(login), 0);
if(-1 == ret)
{
perror("send");
close(chat_fd);
exit(EXIT_FAILURE);
}
char *buff;
buff = (char *)malloc(sizeof(login));
login *logindata = (login *)malloc(sizeof(login));
ret = recv(chat_fd, buff, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(chat_fd);
exit(EXIT_FAILURE);
}
memcpy(logindata, buff, sizeof(login));
/*
*登录成功后服务器发回1,在logindata -> chse里面
*/
printf("logindata -> chse %d\n", logindata -> chse);
if(1 == logindata -> chse)
{
func((void *)chat_fd, chat_data -> name[1]);
}
/*
*登录失败后服务器发回4,在logindata -> chse里面
*/
else if(4 == logindata -> chse )
{
printf("This client don't login\n");
printf("*********************************************************\n");
printf("* *\n");
printf("* 1、登录 2、注册 3、退出 *\n");
printf("* *\n");
printf("*********************************************************\n");
break;
}
/*
*密码错误服务器发回5,在logindata -> chse里面
*/
else if(5 == logindata -> chse )
{
printf("pwd is error\n");
printf("*********************************************************\n");
printf("* *\n");
printf("* 1、登录 2、注册 3、退出 *\n");
printf("* *\n");
printf("*********************************************************\n");
break;
}
/*
*登陆错误服务器发回6,在logindata -> chse里面
*/
else if(6 == logindata -> chse )
{
printf("longin error\n");
printf("*********************************************************\n");
printf("* *\n");
printf("* 1、登录 2、注册 3、退出 *\n");
printf("* *\n");
printf("*********************************************************\n");
break;
}
free(buff);
buff = NULL;
sleep(2);
}
case 2:
{ /*
*注册
*/
printf("请输入以下信息:\n");
/*
*告诉服务器你的选择
*/
login *chat_data;
chat_data = (login *)malloc(sizeof(login));
chat_data -> chse = choose;
printf("chat_data -> chse = %d\n", chat_data -> chse);
printf("**********name***********\n");
scanf("%s", chat_data -> name[1]);
printf("chat_data -> name[1] = %s\n", chat_data -> name[1]);
printf("**********pwd***********\n");
scanf("%s", chat_data -> pwd[1]);
printf("chat_data -> pwd[1] = %s\n", chat_data -> pwd[1]);
printf("*******DATA********\n");
scanf("%s", chat_data -> DATA[1]);
printf("chat_data -> DATA[1] = %s\n", chat_data -> DATA[1]);
char *buffer = (char *)malloc(sizeof(login));
memcpy(buffer, chat_data, sizeof(login));
/*
*向socket_fd服务端发送数据
*/
ret = send(chat_fd, buffer, sizeof(login), 0);
if(-1 == ret)
{
perror("send");
close(chat_fd);
exit(EXIT_FAILURE);
}
char *buf;
buf = (char *)malloc(sizeof(login));
ret = recv(chat_fd, buf, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(chat_fd);
exit(EXIT_FAILURE);
}
login *loginda = (login *)malloc(sizeof(login));
memcpy(loginda, buf, sizeof(login));
/*
*注册后服务器发回1,在logindata -> chse里面
*/
printf("loginda -> chse %d\n", loginda -> chse);
if(2 == loginda -> chse)
{
printf("login success\n");
printf("*************************************\n");
printf("* *\n");
printf("* 1、登录 3、退出 *\n");
printf("* *\n");
printf("*************************************\n");
break;
}
/*
*没有注册服务器发回4,在logindata -> chse里面
*/
else if(3 == loginda -> chse )
{
printf("login error\n");
printf("*********************************************************\n");
printf("* *\n");
printf("* 1、登录 2、注册 3、退出 *\n");
printf("* *\n");
printf("*********************************************************\n");
break;
}
free(buffer);
buffer = NULL;
free(buf);
buf = NULL;
sleep(1);
break;
}
}
}
}
/*-----------------------------------------------------------------
*接收服务器的信息并打印
*------------------------------------------------------------------*/
static void *readfunc(void *arg)
{
int ret;
int socket_fd = (int)arg;
while(1)
{
char buffer[1024];
login *chat_data;
memset(buffer, 0, 1024);
//buffer = (char *)malloc(sizeof(login));
chat_data = (login *)malloc(sizeof(login));
ret = recv(socket_fd, buffer, sizeof(login), 0);
printf("socket_fd = %d\n", socket_fd);
if(-1 == ret)
{
perror("recv");
close(socket_fd);
exit(EXIT_FAILURE);
}
memcpy(chat_data, buffer, sizeof(login));
if(7 == chat_data -> chse )
{
printf("name :%s\tnews :%s\n", chat_data -> name[1], chat_data -> DATA[1]);
}
else if(8 == chat_data -> chse )
{
printf("name :%s\tnews :%s\n", chat_data -> name[1], chat_data -> DATA[1]);
}
else if(9 == chat_data -> chse)
{
printf("%s传来文件%s\n", chat_data -> name[1], chat_data -> DATA[1]);
FILE *file = fopen(chat_data -> DATA[1], "wr");
if(file == NULL)
{
printf("create file %s failure", chat_data -> DATA[1]);
perror("create:");
exit(-3);
}
//printf("recv file %s\n", chat_data -> DATA[2]);
int len = fwrite(chat_data -> DATA[2], sizeof(chat_data -> DATA[2]), sizeof(chat_data -> DATA[2]), file);
//printf("len = %d\n", len);
fclose(file);
printf("recv file %s success\n", chat_data -> DATA[1]);
}
else if(10 == chat_data -> chse)
{
printf("news :%s\n", chat_data -> DATA[1]);
}
else if(11 == chat_data -> chse)
{
printf("申请管理员成功\n");
}
else if(12 == chat_data -> chse)
{
printf("您被管理员< %s >T除\n", chat_data -> name[1]);
goto EXIT_MAIN;
}
else if(13 == chat_data -> chse)
{
printf("您不是管理员\n");
}
else if(14 == chat_data -> chse)
{
printf("T除失败\n");
}
else if(15 == chat_data -> chse)
{
printf("T除成功\n");
}
else if(16 == chat_data -> chse)
{
printf("您被管理员< %s >禁言\n", chat_data -> name[1]);
}
else if(17 == chat_data -> chse)
{
printf("禁言失败\n");
}
else if(18 == chat_data -> chse)
{
printf("您被管理员< %s >解除禁言\n", chat_data -> name[1]);
}
else if(19 == chat_data -> chse)
{
printf("解除禁言失败\n");
}
else if(20 == chat_data -> chse)
{
printf("您在禁言状态\n");
}
else if(21 == chat_data -> chse)
{
printf("下线成功\n");
goto EXIT_MAIN;
}
}
EXIT_MAIN:
flag_online_1 = 1;
}
/*-----------------------------------------------------------------
*客户端连接函数
*------------------------------------------------------------------*/
int chat_count_client(const char *ip, int port)
{
int ret;
int socket_fd;
/*
*建立一个socket
*/
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == socket_fd)
{
perror("socket");
close(socket_fd);
exit(EXIT_FAILURE);
}
printf("建立socket成功\n");
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(struct sockaddr)); //清空
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(port);;
client_addr.sin_addr.s_addr = INADDR_ANY;
/*
*发送连接请求
*/
ret = connect(socket_fd, (struct sockaddr *)&client_addr, sizeof(struct sockaddr));
if(-1 == ret)
{
perror("connect");
close(socket_fd);
exit(EXIT_FAILURE);
}
printf("connect成功\n");
return socket_fd;
}
int func(void *arg, char *name)
{
pthread_t client;
int chat_fd = (int)arg;
pthread_create(&client, NULL, readfunc, (void *)chat_fd);
display((void *)chat_fd);
int flag_online;
while(1)
{
if(flag_online_1 == 0)
{
printf("**************************\n");
printf("*Yd *\n");
printf("* 6、私 聊 *\n");
printf("* 7、群 聊 *\n");
printf("* 8、悄 悄 话 *\n");
printf("* 9、发送文件 *\n");
printf("* 10、管 理 员 *\n");
printf("* 11、下 线 *\n");
printf("* *\n");
printf("**************************\n");
scanf("%d", &flag_online);
switch(flag_online)
{
case 6:
{
choose_func(flag_online, (void *)chat_fd);
chat_send((void *)chat_fd, name);
break;
}
case 7:
{
choose_func(flag_online, (void *)chat_fd);
chat_group((void *)chat_fd, name);
break;
}
case 8:
{
choose_func(flag_online, (void *)chat_fd);
Whisper_send((void *)chat_fd, name);
break;
}
case 9:
{
choose_func(flag_online, (void *)chat_fd);
send_file((void *)chat_fd, name);
break;
}
case 10:
{
choose_func(flag_online, (void *)chat_fd);
Administrator((void *)chat_fd, name);
break;
}
case 11:
{
choose_func(flag_online, (void *)chat_fd);
off_line_send((void *)chat_fd, name);
break;
}
}
}
else if(flag_online_1 == 1)
{
flag_online_1 = 0;
close(chat_fd);
printf("**************************\n");
printf("* 请输入Ctrl + c退出 *\n");
printf("**************************\n");
goto WW;
}
}
WW:
return;
}
/*
*选择
*/
int choose_func(int online_flag, void *arg)
{
int ret;
int chat_fd = (int)arg;
login *chat_data;
chat_data = (login *)malloc(sizeof(login));
memset(chat_data, 0, sizeof(login));
chat_data -> chse = online_flag;
printf("chat_data -> chse = %d\n", chat_data -> chse);
//向socket_fd服务端发送数据
char buffer[1024]; //= (char *)malloc(sizeof(login));
memset(buffer, 0, 1024);
memcpy(buffer, chat_data, sizeof(login));
ret = send(chat_fd, buffer, sizeof(login), 0); //向socket_fd服务端发送数据
printf("chat_fd = %d\n", chat_fd);
if(-1 == ret)
{
perror("send");
close(chat_fd);
exit(EXIT_FAILURE);
}
}
/*
*打印
*/
int display(void *arg)
{
int ret;
int new_fd = (int)arg;
char *bufferdis;
bufferdis = (char *)malloc(sizeof(login));
login *logindisplay = (login *)malloc(sizeof(login));
ret = recv(new_fd, bufferdis, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(new_fd);
exit(EXIT_FAILURE);
}
memcpy(logindisplay, bufferdis, sizeof(login));//sizeof后面不能是指正
int play = logindisplay -> chse;
printf("当前在线人数:%d\n", play);
printf("\t%s\n", logindisplay -> name[1]);
int i = 2;
for((i < play); i <= play; i ++)
{
ret = recv(new_fd, bufferdis, sizeof(login), 0);
if(-1 == ret)
{
perror("recv");
close(new_fd);
exit(EXIT_FAILURE);
}
memcpy(logindisplay, bufferdis, sizeof(login));//sizeof后面不能是指正
printf("\t%s\n", logindisplay -> name[1]);
}
printf("\n");
}
/*
*私聊发送
*/
int chat_send(void *arg, char *name)
{
int ret;
int chatfd = (int)arg;
login *chatdata1;
chatdata1 = (login *)malloc(sizeof(login));
//memset(chatdata1, 0, sizeof(login));
sprintf(chatdata1 -> name[2], "%s", name);
printf("chatdata1 -> name[2] = %s\n", chatdata1 -> name[2]);
printf("******name*******\n");
scanf("%s", chatdata1 -> name[1]);
printf("******news*******\n");
scanf("%s", chatdata1 -> DATA[1]);
char buffer7[1024];
memset(buffer7, 0, 1024);
memcpy(buffer7, chatdata1, sizeof(login));
ret = send(chatfd, buffer7, sizeof(login), 0); //向chatfd服务端发送数据
printf("chatfd = %d\n", chatfd);
if(-1 == ret)
{
perror("send");
close(chatfd);
exit(EXIT_FAILURE);
}
sleep(1);
}
/*
*悄悄话
*/
int Whisper_send(void *arg, char *name)
{
int ret;
int chatfd = (int)arg;
login *chatdata1;
chatdata1 = (login *)malloc(sizeof(login));
//memset(chatdata1, 0, sizeof(login));
sprintf(chatdata1 -> name[2], "%s", name);
printf("chatdata1 -> name[2] = %s\n", chatdata1 -> name[2]);
printf("******name*******\n");
scanf("%s", chatdata1 -> name[1]);
printf("******news*******\n");
scanf("%s", chatdata1 -> DATA[1]);
char buffer7[1024];
memset(buffer7, 0, 1024);
memcpy(buffer7, chatdata1, sizeof(login));
ret = send(chatfd, buffer7, sizeof(login), 0); //向chatfd服务端发送数据
printf("chatfd = %d\n", chatfd);
if(-1 == ret)
{
perror("send");
close(chatfd);
exit(EXIT_FAILURE);
}
sleep(1);
}
/*
*发送(群组)
*/
int chat_group(void *arg, char *name)
{
int ret;
int group_fd = (int)arg;
login *group_data;
group_data = (login *)malloc(sizeof(login));
//memset(group_data, 0, sizeof(login));
sprintf(group_data -> name[1], "%s", name);
printf("********news********\n");
scanf("%s", group_data -> DATA[1]);
printf("********end*********\n");
char buffer8[1024];
memset(buffer8, 0, 1024);
memcpy(buffer8, group_data, sizeof(login));
ret = send(group_fd, buffer8, sizeof(login), 0); //向socket_fd服务端发送数据
printf("group_fd = %d\n", group_fd);
if(-1 == ret)
{
perror("send");
close(group_fd);
exit(EXIT_FAILURE);
}
sleep(1);
}
/*----------------------------------------------------------------------------
*接收文件
------------------------------------------------------------------------------*/
int recv_fife(void *arg)
{
FILE *file = NULL;
int my_fd = (int)arg;
int total = 0;
int len;
char buf[MAX];
char filename[FILE_MAX_LEN + 1];
while(total < FILE_MAX_LEN)
{
len = recv(my_fd, filename + total, (FILE_MAX_LEN - total), 0);
if(len <= 0)
{
break;
}
total += len;
}
if(total != FILE_MAX_LEN)
{
perror("failure file name");
exit(-3);
}
printf("recv file %s\n", filename);
file = fopen(filename, "wb");
if(file == NULL)
{
printf("create file %s failure", filename);
perror("create:");
exit(-3);
}
printf("recv begin\n");
total = 0;
while(1)
{
len = recv(my_fd, buf, sizeof(buf), 0);
if(len == -1)
{
break;
}
total += len;
fwrite(buf, 1, len, file);
}
fclose(file);
printf("recv file %s success total lenght %d\n", filename, total);
close(my_fd);
}
/*----------------------------------------------------------------------
*发送文件
-----------------------------------------------------------------------*/
int send_file(void *arg, char *name)
{
char buf[MAX];
memset(buf, 0, sizeof(buf));
char filename[FILE_MAX_LEN + 1];
int sock = (int)arg;
int len;
login *File_data;
File_data = (login *)malloc(sizeof(login));
memset(File_data, 0, sizeof(login));
sprintf(File_data -> name[2], "%s", name);
printf("File_data -> name[2] = %s\n", File_data -> name[2]);
printf("******name*******\n");
scanf("%s", File_data -> name[1]);
printf("****FILE_NAME****\n");
scanf("%s", filename);
printf("%s\n", filename);
sprintf(File_data -> DATA[1], filename, sizeof(filename));
//printf("File_data -> DATA[1] %s\n", File_data -> DATA[1]);
FILE *file = fopen(filename, "r");
len = fread(File_data -> DATA[2], sizeof(File_data -> DATA[2]), sizeof(login), file); //文件的内容
// printf("server read %s, len %d\n", filename, len);
// printf("File_data -> DATA[2] %s\n", File_data -> DATA[2]);
memcpy(buf, File_data, sizeof(login));
if(send(sock, buf, sizeof(login), 0))
{
perror("send file:");
close(sock);
}
printf("文件发送完成\n");
fclose(file);
sleep(1);
}
int Administrator(void *arg, char *name)
{
int Adm_fd = (int)arg;
int ch;
printf("*------------------------------*\n");
printf("*1、申请 2、T人 3、禁言 4、解禁*\n");
printf("*------------------------------*\n");
scanf("%d", &ch);
printf("%d\n", ch);
choose_func(ch, (void *)Adm_fd);
switch(ch)
{
case 1:
{
Administrator_Appli_send((void *)Adm_fd, name);
break;
}
case 2:
{
Administrator_send((void *)Adm_fd, name);
break;
}
case 3:
{
Administrator_send((void *)Adm_fd, name);
break;
}
case 4:
{
Administrator_send((void *)Adm_fd, name);
break;
}
}
}
/*
*管理员操作
*/
int Administrator_send(void *arg, char *name)
{
int ret;
int chatfd = (int)arg;
login *chatdata1;
chatdata1 = (login *)malloc(sizeof(login));
//memset(chatdata1, 0, sizeof(login));
sprintf(chatdata1 -> name[2], "%s", name);
printf("chatdata1 -> name[2] = %s\n", chatdata1 -> name[2]);
printf("******name*******\n");
scanf("%s", chatdata1 -> name[1]);
char buffer7[1024];
memset(buffer7, 0, 1024);
memcpy(buffer7, chatdata1, sizeof(login));
ret = send(chatfd, buffer7, sizeof(login), 0); //向chatfd服务端发送数据
printf("chatfd = %d\n", chatfd);
if(-1 == ret)
{
perror("send");
close(chatfd);
exit(EXIT_FAILURE);
}
sleep(1);
}
/*
*管理员申请
*/
int Administrator_Appli_send(void *arg, char *name)
{
int ret;
int chatfd = (int)arg;
login *chatdata1;
chatdata1 = (login *)malloc(sizeof(login));
//memset(chatdata1, 0, sizeof(login));
sprintf(chatdata1 -> name[2], "%s", name);
printf("chatdata1 -> name[2] = %s\n", chatdata1 -> name[2]);
char buffer7[1024];
memset(buffer7, 0, 1024);
memcpy(buffer7, chatdata1, sizeof(login));
ret = send(chatfd, buffer7, sizeof(login), 0); //向chatfd服务端发送数据
printf("chatfd = %d\n", chatfd);
if(-1 == ret)
{
perror("send");
close(chatfd);
exit(EXIT_FAILURE);
}
sleep(1);
}
/*
*下线
*/
int off_line_send(void *arg, char *name)
{
int ret;
int chatfd = (int)arg;
login *chatdata1;
chatdata1 = (login *)malloc(sizeof(login));
//memset(chatdata1, 0, sizeof(login));
sprintf(chatdata1 -> name[2], "%s", name);
printf("chatdata1 -> name[2] = %s\n", chatdata1 -> name[2]);
char buffer7[1024];
memset(buffer7, 0, 1024);
memcpy(buffer7, chatdata1, sizeof(login));
ret = send(chatfd, buffer7, sizeof(login), 0); //向chatfd服务端发送数据
printf("chatfd = %d\n", chatfd);
if(-1 == ret)
{
perror("send");
close(chatfd);
exit(EXIT_FAILURE);
}
sleep(1);
}