基于Linux的聊天室(服务器)初成版

  实现注册、登入、下线、私聊、群聊、传文件、在线用户查询,以及管理员(root)的禁言,解禁,踢人等功能,与多客户端进行连接。

/*****************************************************
copyright (C), Nanjing University of ZiJin
File name:server.c
Author: CaoMengDe  Version:0.1    Date: 2017-01-01 14:38
Description:
Funcion List: 
*****************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define PORTNUMBER 9997


typedef struct node
{
int fd;
char name[1024];
int flagg;
struct node *next;
}Node;


Node* head;
char yourname[1024] = {0};
int init(Node** head);//初始化链表


//void menu();
void registration(int fd);//数据库用来存放用户帐号信息
int signin(int newfd);//登入判断
void* func(void* arg);//线程
int up(int fd);//上线
void down(int fd);//下线
int groupchat(int fd);//群聊
void print(int fd);//在线用户查询
int privatechat(int fd);//私聊
int Prohibition(int fd);//禁言
int Liftban(int fd);//解禁
int Kick(int fd);//踢人
void printlist(int fd);//管理员查询在线人数
int catban(int fd);//查询被禁言用户
int file(int fd);//文件传输
 
int main(int argc, char **argv)
{
int ret;
ret =init(&head);
if(-1 == ret)
{
return -1;
}
   //AF_INET:IPv4
//SOCK_STREAM:流式SOCKET,代表TCP
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == socket_fd)
{
perror("socket");
exit(1);
}

struct sockaddr_in server_addr;


    //清空addr
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORTNUMBER);
//将本机器上所有long数转化为网络字节许。
//UNADDR_ANY 可以接收任意IP地址
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (-1 == bind(socket_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)))
{
perror("bind");
exit(2);
}

//5 :最大连接数
if(-1 == listen(socket_fd, 5))
{
perror("listen");
exit(3);
}

while (1)
{
struct sockaddr_in client_addr;
   int length = sizeof(struct sockaddr);
   int newfd = accept(socket_fd, (struct sockaddr *)(&client_addr), &length);
if (-1 == newfd)
   {
   perror("accept");
   exit(4);
   }
pthread_t pid;
        pthread_create(&pid, NULL, (void*)func, (void*)newfd);

}

}






void* func(void* arg)
//线程
{
int newfd = (int)arg;
while(1)
{

char order[1024] = {0};
//sleep(1);
int ret = read(newfd, order, 1024);
if(ret == -1)
{
//perror("order");
return NULL;
}
if(strcmp(order, "*registration") == 0)
{
registration(newfd);
}
if(strcmp(order, "*signin") == 0)
{
signin(newfd);
}
if(strcmp(order, "*up") == 0)
{
up(newfd);
}
if(strcmp(order, "*privatechat") == 0)
{
privatechat(newfd);
}
if(strcmp(order, "*groupchat") == 0)
{
groupchat(newfd);
}
if(strcmp(order, "*print") == 0)
{
print(newfd);
}
if(strcmp(order, "*down") == 0)
{
down(newfd);
}
if(strcmp(order, "*file") == 0)
{
//printf("1----------------------------\n");
file(newfd);
}
if(strcmp(order, "*superuser") == 0)
{
superuser(newfd);
}
if(strcmp(order, "*Prohibition") == 0)
{
Prohibition(newfd);
}
if(strcmp(order, "*Liftban") == 0)
{
Liftban(newfd);
}
if(strcmp(order, "*Kick") == 0)
{
Kick(newfd);
}
/*if(strcmp(order, "*printlist") == 0)
{
printlist(newfd);
}*/
if(strcmp(order, "*catban") == 0)
{
catban(newfd);
}
if(strcmp(order, "*exit") == 0)
{
break;
}
memset(order, 0 ,1024);

}

    printf("conect end\n");
close(newfd);
}


int init(Node** head)
//链表的初始化
{
Node* new = (Node*)malloc(sizeof(Node));
    if(NULL == new)
    {
        return -1;
    }
    *head = new;
    (*head)->next = NULL;
return 0;
}




/*void menu()
{
int num;
printf("欢迎来到Coder的聊天室\n");
}*/




void registration(int fd)
//数据库用来存放用户帐号信息
{
int ret;
sqlite3 *db;
char* errmsg;
ret = sqlite3_open("server.db", &db);
if(ret != SQLITE_OK)
    {
        perror("open");
        exit(1);
    }
char sql_create[] = "create table if not exists stu(username text primary key, pwd text);";
    ret = sqlite3_exec(db, sql_create, NULL, NULL, &errmsg);
    if(ret != SQLITE_OK)
    {
        perror("create");
        exit(2);
    }
char buf1[1024] = {0};
char buf2[1024] = {0};
    char flag[1024] = {0};
char buf3[1024] = {0};
ret = read(fd, buf1, 1024);//帐号
if(-1 == ret)
{
perror("zhanghao");
return;
}
ret = read(fd, buf2, 1024);//密码
if(-1 == ret)
{
perror("mima");
return;
}
ret = read(fd, buf3, 1024);//确认密码
if(-1 == ret)
{
perror("2mima");
return;
}
char sql_insert[1024] = {0};
if(strcmp(buf2, buf3) != 0)
{
flag[0] = 'A';
write(fd, flag, 1024);
return;
}
sprintf(sql_insert, "insert into stu values('%s', '%s');", buf1, buf2);
ret = sqlite3_exec(db, sql_insert, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)//判断帐号是否已经存在
    {
        //perror("insert");
//printf("用户名已存在\n");
flag[0] = 'N';
write(fd, flag, 1024);
    }
else
{
//printf("执行成功!\n");
flag[0] = 'Y';
write(fd, flag, 1024);
}
ret = sqlite3_close(db);
if(ret != SQLITE_OK)
    {
        perror("close");
        exit(1);
    }
}






int signin(int newfd)
//登入判断
{
char flag[1024];
int ret;
sqlite3 *db;
char* errmsg;
int row;
int column;
char** result;
ret = sqlite3_open("server.db", &db);
if(ret != SQLITE_OK)
    {
        perror("open");
        exit(1);
    }
char buf1[1024] = {0};
char buf2[1024] = {0};
ret = read(newfd, buf1, 1024);
if(-1 == ret)
{
perror("dengruzhanghao");
return;
}
ret = read(newfd, buf2, 1024);
if(-1 == ret)
{
perror("dengrumima");
return;
}
char sql_query[1024] = {0};
sprintf(sql_query, "select * from stu where username = '%s';", buf1);
ret = sqlite3_get_table(db, sql_query, &result, &row, &column, &errmsg);
if(ret != SQLITE_OK)
{
perror("query1");
return 0;
}
if(row > 0)
{
if(strcmp(buf2, result[1*column + 1]) == 0)
{
if(strcmp("root", buf1) == 0)
{
flag[0] = 'S';
write(newfd, flag, 1024);

}
else
{
flag[0] = 'Y';
write(newfd, flag, 1024);
strcpy(yourname, buf1);
}

}
else
{
flag[0] = 'N';
write(newfd, flag, 1024);
}
}
else
{
flag[0] = 'Z';
write(newfd, flag, 1024);
}

}




int up(int fd)
//上线
{
int ret;
char str[1024] = {0};
char name[1024] = {0};
/*ret = read(fd, str, 1024);
if(-1 == ret)
{
perror("shangxian");
return;
}*/
Node* temp = head;
Node* new = (Node*)malloc(sizeof(Node));
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new;
new->next = NULL;
new->fd = fd;
new->flagg = 1;
strcpy(new->name, yourname);
memset(yourname, 0, 1024);
}






void down(int fd)
//下线
{
char t[1024] = "down_success";
Node* temp = head;
while(temp->next->fd != fd)
{
temp = temp->next;
}
Node* temp1 = temp->next;
temp->next = temp->next->next;
free(temp1);
temp1 = NULL;
write(fd, t, 1024);
}






int privatechat(int fd)
//私聊
{
int ret;
Node* temp1 = head;
while(temp1->next->fd != fd)
{
temp1 = temp1->next;
}
if(temp1->next->flagg == 1)
{
char str[1024] = "privatechat_ok";
write(fd, str, 1024);
char t[1024] = {0};
Node* temp = head;
ret = read(fd, t, 1024);
if(-1 == ret)
{
perror("privatechat");
return;
}
while(strcmp(temp->next->name, t)!= 0)
{
temp = temp->next;
if(temp->next == NULL)
{
char m[1024] = "no_find";
write(fd, m, 1024);
return 0;
}
}
int newfd2 = temp->next->fd;
while(1)
{
memset(t, 0, 1024);
ret = read(fd, t, 1024);
if(-1 == ret)
{
perror("2privatechat");
return;
}
if(strcmp(t, "exit") == 0)
{
break;
}
char buffer[1024] = {0};
sprintf(buffer, "%s:%s", temp1->next->name, t);
write(newfd2, buffer, 1024);
}
}
if(temp1->next->flagg == 0)
{
char str[1024] = "privatechat_error";
write(fd, str, 1024);
}

}


 
 
int groupchat(int fd)
//群聊
{
int ret;
Node* temp1 = head;
while(temp1->next->fd != fd)
{
temp1 = temp1->next;
}
if(temp1->next->flagg == 1)
{
char str[1024] = "groupchat_success";
write(fd, str, 1024);
while(1)
{
Node* temp = head;
char buffer[1024] = {0};
ret = read(fd, buffer, 1024);
if(-1 == ret)
{
//perror("qunliao");
return;
}
if(strcmp(buffer, "exit") == 0)
{
break;
}
while(temp->next != NULL)
{
if(temp->next->fd != fd)
{
int newfd = temp->next->fd;
char tt[1024] = {0};
sprintf(tt, "%s:%s", temp1->next->name,buffer);
write(newfd, tt, 1024);
temp = temp->next;

}
else
{
temp = temp->next;
}
}
memset(buffer, 0, 1024);
}
}
if(temp1->next->flagg == 0)
{
char str[1024] = "groupchat_error";
write(fd, str, 1024);


}
}
 
 
 void print(int fd)
 
 //在线用户查询
 {
Node* temp = head;
char buffer[1024] = {0};
while(temp->next != NULL)
{
strcpy(buffer, temp->next->name);
write(fd, buffer, 1024);
   memset(buffer, 0 ,1024);
temp = temp->next;
}

 }
 
 
int superuser(int fd)
//管理员操作
{
Node* new = (Node*)malloc(sizeof(Node));
char str[1024] = "root";
if(NULL == new)
{
perror("superuser");
return -1;
}
Node* temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new;
new->next = NULL;
strcpy(new->name, str);
new->fd = fd;
new->flagg = 1;
}


int Prohibition(int fd)
//禁言
{
int ret;
char buffer[1024] = {0};
ret = read(fd, buffer, 1024);
if(-1 == ret)
{
perror("jingyan");
return;
}
Node* temp = head;
while(strcmp(temp->next->name, buffer) != 0)
{
temp = temp->next;
if(NULL == temp->next)
{
char t[1024] = "no_find";
write(fd, t, 1024);
return;
}
}
if(temp->next->flagg == 0)
{
char s[1024] = "again_operate";
write(fd, s, 1024);
return;
}
else
{
   temp->next->flagg = 0;
}

}




int Liftban(int fd)
//解禁
{
int ret;
char buffer[1024] = {0};
ret = read(fd, buffer, 1024);
if(-1 == ret)
{
perror("jiejin");
return;
}
Node* temp = head;
while(strcmp(temp->next->name, buffer) != 0)
{
temp = temp->next;
if(NULL == temp->next)
{
char t[1024] = "no_find";
write(fd, t, 1024);
return;
}
}
if(temp->next->flagg == 1)
{
char s[1024] = "again_operate";
write(fd, s, 1024);
return;
}
else
{
   temp->next->flagg = 1;
}
}




int Kick(int fd)
//踢人
{
int ret;
Node* temp = head;
char str[1024] = {0};
ret = read(fd, str, 1024);
if(-1 == ret)
{
perror("tiren");
return;
}
while(strcmp(temp->next->name, str) != 0)
{
temp = temp->next;
if(temp->next == NULL)
{
char m[1024] = "no_find";
write(fd, m, 1024);
return 0;
}
}
int socket_fd = temp->next->fd;
char t[1024] = "kick_success";
write(socket_fd, t, 1024);
Node* temp1 = temp->next;
temp->next = temp->next->next;
free(temp1);
temp1 = NULL;
close(socket_fd);
}


/*void printlist(int fd)
//在线用户查询
{
Node* temp = head;
char buffer[1024] = {0};
while(temp->next != NULL)
{
strcpy(buffer,temp->next->name);
write(fd, buffer, 1024);
memset(buffer, 0 ,1024);
temp = temp->next;
}


}*/


int catban(int fd)
//被禁言用户查询
{
char buffer[1024] = {0};
Node* temp = head;
while(temp->next != NULL)
{
if(temp->next->flagg == 0)
{
strcpy(buffer, temp->next->name);
write(fd, buffer, 1024);
memset(buffer, 0 ,1024);
}
   temp = temp->next;
}



}


int file(int fd)
{
int ret;
Node* temp = head;
char t[1024] = "no_find";
char buffer[1024] = {0};
char file[1024] = {0};
read(fd, buffer, 1024);
while(strcmp(buffer, temp->next->name) != 0)
{
temp = temp->next;
if(NULL == temp->next)
{
write(fd, t, 1024);
return 1;
}

}

memset(buffer, 0, 1024);
int fd2 = temp->next->fd;
read(fd, file, 1024);
/*if(-1 == ret)
{
perror("wenjian");
return;
}*/
write(fd2, "file_accept", 1024);//发送让客户端收的信号
write(fd2, file, 1024);//传递的文件名字
while(ret = read(fd, buffer, 1024))
{
if(strcmp(buffer, "end_file") == 0)
{
break;
}
write(fd2, buffer, 1024);
memset(buffer, 0, 1024);
}
write(fd2, "end_file", 1024);
//printf("22-------------------------------\n");
}


你可能感兴趣的:(基于Linux的聊天室(服务器)初成版)