//command.h
#ifndef SERVER_REMOTE_COMMAND_H
#define SERVER_REMOTE_COMMAND_H
#include
#define GROUP_SEND '1'
#define SINGLE_SEND '2'
#define REGISER '3'
#define LOGIN '4'
typedef struct st
{
int fd; //client fd
char *ip; //client ip
int port; //client port
char name[20]; //client name
char password[20]; //client password
pthread_t tid; //client thread id
struct st *next; //next node
}client_info;
#endif
#ifndef SERVER_REMOTE_LIST_H
#define SERVER_REMOTE_LIST_H
typedef struct st client_info; // 让编译器知道这个结构体的存在
void initList(client_info **head);
void addList(client_info *head, const client_info *node);
void removeList(client_info *head, const client_info *node);
void printList(client_info *head);
#endif
//main.c
#include "command.h"
#include
#include
#include
#include
#include
#include
#include
#include "list.h"
#define BUF_SIZE 100
client_info *head;//client info list
FILE* chatHistory;
void fileWrite(FILE* chatHistory, char* msg)
{
fprintf(chatHistory, "%s", msg);
fflush(chatHistory); // 刷新缓冲区
}
pthread_mutex_t mutex;// 互斥锁
// 处理信息
void* handleClient(void*arg)
{
client_info clientInfo = *(client_info*)arg; // 清零
char buf[BUF_SIZE];
while(1)
{
bzero(buf, BUF_SIZE); //清零 memset()
// read 接收信息
int len = recv(clientInfo.fd, buf, BUF_SIZE, 0);
#ifndef DEBUG_MY
printf("recv: %s\n", buf);
#endif
if(len == -1)
{
perror("recv");
pthread_exit(NULL);
}
if(len == 0) // 接收为0==EOF ,客户端断开连接
{
#ifndef DEBUG_MY
printf("client %s closed\n", clientInfo.ip); //定位错误
#endif
// 移除
pthread_mutex_lock(&mutex);
removeList(head, &clientInfo);
pthread_mutex_unlock(&mutex);
close(clientInfo.fd);
pthread_exit(NULL); // 退出线程
}
if(buf[0]==GROUP_SEND)//群发消息
{
pthread_mutex_lock(&mutex);
#ifndef DEBUG_MY
printf("client send %s\n", buf+1); //定位错误
#endif
char temp_buf[BUF_SIZE]={0};
time_t timep;
time(&timep);
sprintf(temp_buf, "%s:%s:%s", clientInfo.name, buf+1,ctime(&timep));
#ifndef DEBUG_MY
printf("temp_buf %s\n", temp_buf); //定位错误
#endif
// 发送信息格式为:name:msg:time
client_info *temp=head->next;
while(temp!=NULL)
{
send(temp->fd, temp_buf, BUF_SIZE, 0);
// 排除自己
temp=temp->next;
}
// 写入聊天记录
strcat(temp_buf, "群发消息\n");
fileWrite(chatHistory, temp_buf);
pthread_mutex_unlock(&mutex);
}
else if(buf[0]==SINGLE_SEND)//单发消息
{
char *p = buf + 1; //接受buf[0]后面的数据
// strtok()函数用于将字符串分割成一个个片段
// 规定单发消息格式为:2:ip:msg
// strtok()函数第一次调用时,第一个参数为字符串,第二个参数为分隔符
// 之后每次调用,第一个参数为NULL,第二个参数为分隔符
char *ip = strtok(p, ":");
char *msg = strtok(NULL, ":");
#ifndef DEBUG_MY
printf("ip:%s msg:%s", ip,msg); //定位错误
#endif
pthread_mutex_lock(&mutex);
client_info *temp = head->next;
while(temp->next!=NULL)
{
if (strcmp(temp->ip, ip) == 0)
{
char temp_buf[BUF_SIZE];
sprintf(temp_buf, "%s:%s", clientInfo.ip, msg);
write(temp->fd, temp_buf, strlen(temp_buf));
// 写入聊天记录
strcat(temp_buf, "单发消息\n");
fileWrite(chatHistory, temp_buf);
break;
}
temp=temp->next;
}
pthread_mutex_unlock(&mutex);
}
}
}
int main()
{
pthread_mutex_init(&mutex, NULL);
initList(&head);
chatHistory= fopen("chatHistory.txt","a+");
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sock_fd == -1)
{
perror("socket");
exit(-1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(9000);
server_addr.sin_addr.s_addr =INADDR_ANY;
int opt = 1;
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(bind(sock_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1)
{
perror("bind");
exit(-1);
}
if(listen(sock_fd, 10) == -1)
{
perror("listen");
exit(-1);
}
while(1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_fd = accept(sock_fd, (struct sockaddr*)&client_addr, &len);
if(client_fd == -1)
{
perror("accept");
exit(-1);
}
// 读取账号密码
char msg[BUF_SIZE];
memset(msg,0, BUF_SIZE);
int len_read = recv(client_fd, msg, BUF_SIZE, 0);
if(len_read == -1)
{
perror("recv");
exit(-1);
}
// 处理连接信息
char *p = msg + 1;
char *username = strtok(p, ":");
char *password = strtok(NULL, ":");
if(msg[0]==REGISER) // 注册
{
//添加到文件
FILE* fp = fopen("user.txt", "a+");
if(fp == NULL)
{
perror("fopen");
exit(-1);
}
char un[20], pwd[20];
int exit=0;
while (fscanf(fp, "%s%s", un, pwd) != EOF)
{
if(strcmp(username,un)==0&&strcmp(password,pwd)==0)
{
exit=1; //已存在
break;
}
}
if(exit==1)
{
// 发送注册失败
char *send_msg= "fail";
write(client_fd, send_msg, strlen(send_msg));
continue;
}
else if(exit==0)
{
// 写入文件
fprintf(fp, "%s %s\n", username, password);
fclose(fp);
// 发送注册成功
char *send_msg= "success";
write(client_fd, send_msg, strlen(send_msg));
}
}
if(msg[0]==LOGIN) //登录
{
//验证
// 打开文件
FILE* fp = fopen("user.txt", "r");
if(fp == NULL)
{
perror("fopen");
exit(-1);
}
// 读取文件
char un[20];
char pwd[20];
while (fscanf(fp, "%s%s", un, pwd) != EOF)
{
if(strcmp(username, un) == 0 && strcmp(password, pwd) == 0)
{
char* send_msg = "success";
write(client_fd, send_msg, strlen(send_msg));
break;
}
}
fclose(fp);
char* send_msg = "fail";
write(client_fd, send_msg, strlen(send_msg));
continue;
}
client_info clientInfo;
strcpy(clientInfo.name, username);
strcpy(clientInfo.password, password);
clientInfo.port = ntohs(client_addr.sin_port); //get client port
clientInfo.fd = client_fd;
clientInfo.ip = inet_ntoa(client_addr.sin_addr); //get client ip
//创建线程
pthread_t pid;
int ret=pthread_create(&pid, NULL, handleClient, (void*)&clientInfo);
if(ret!=0)
{
perror("pthread_create");
exit(-1);
}
clientInfo.tid = pid;
pthread_mutex_lock(&mutex);
addList(head, &clientInfo);
pthread_mutex_unlock(&mutex);
}
}
//user.txt
xiaobai 2222
xiaoxiao 1234
xiaoli 12345
xiaolo 1234
xiaowi 123
jjjj 2222
dddd 1111
ffff 3333
eeee 2222
fffff 33333
rrrr 2222
qwer 1234
12345 123456
123 1234
112233 445566
22333 444444
0000 2222
pppp 2222
//chatHistory.txt
群发消息群发消息群发消息群发消息12345:qqqqqqqqqqqqq:Wed Aug 9 08:55:36 2023
群发消息
0000:qqqqqqqqqqqqqqqqqqq:Wed Aug 9 09:14:57 2023
群发消息
0000:qqqqqqqqqqqqqqqq:Wed Aug 9 09:15:54 2023
群发消息
0000:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq:Wed Aug 9 09:17:16 2023
群发消息
0000:baibaibia:Wed Aug 9 09:17:27 2023
群发消息
pppp:qqqqqqqqqqqqqqqqqqqqqq:Wed Aug 9 17:02:51 2023
群发消息
pppp:llllllllllllllllllllllll:Wed Aug 9 17:03:20 2023
群发消息
pppp:llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
pppp:llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll1llllllllllllllllllllllll:Wed Aug 9 17:05:13 2023
群发消息
//客户机:
//command.h
#ifndef THREAD_COMMAND_H
#define THREAD_COMMAND_H#define GROUP_SEND '1'
#define SINGLE_SEND '2'
#define REGISER '3'
#define LOGIN '4'#define BUF_SIZE 1024 // 缓冲区大小
#endif//menuFunctions.h
#ifndef THREAD_MENUFUNCTION_H
#define THREAD_MENUFUNCTION_Hvoid menuForLoginAndRegister(int fd);
void menuForSend(int fd);#endif
//menuFunctions.c
#include "menuFunctions.h"
#include
#include
#include
#include
#include "command.h"
#includevoid menuForLoginAndRegister(int fd)
{
printf("1.注册\n");
printf("2.登录\n");
printf("3.退出\n");
printf("请输入你的选择: ");
char choice;
scanf("%c", &choice);
getchar();
switch(choice)
{
case '1':
{
char name[20]={0};
char password[20]={0};
printf("请输入用户名: ");
scanf("%s", name);
getchar();
printf("请输入密码: ");
scanf("%s", password);
getchar();
char buf[BUF_SIZE]={0};//缓冲区
snprintf(buf, sizeof(buf), "%c:%s:%s", REGISER, name, password);
send(fd, buf, strlen(buf), 0);
sleep(3); //等待服务器返回注册结果
read(fd, buf, sizeof(buf));
if(buf[0] == 's') //注册成功
{
printf("注册成功\n");
printf("1.聊天\n");
printf("2.退出\n");
printf("请输入你的选择: \n");
char choice1;
scanf("%c",&choice1);
getchar();
switch (choice1)
{
case '1' :
{
menuForSend(fd);
break;
}
case '2':
{
close(fd);
exit(0);
}
default:
{
printf("输入错误\n");
break;
}
}
}
else if(buf[0] == 'f')
{
printf("注册失败\n");
exit(0); }
break;
}
case '2':
{
char name[20];
char password[20];
printf("请输入用户名: ");
scanf("%s", name);
getchar();
printf("请输入密码: ");
scanf("%s", password);
getchar();
char buf[BUF_SIZE];
snprintf(buf,sizeof(buf), "%c:%s:%s", LOGIN, name, password);
send(fd, buf, strlen(buf), 0);
sleep(4); //等待服务器返回登录结果
read(fd, buf, sizeof(buf));
if(buf[0] == 's') //登录成功
{
printf("登录成功\n");
menuForSend(fd);
}
else
{
printf("登录失败\n");
}
break;
}
case '3':
{
close(fd);
exit(0);
}
default:
{
printf("输入错误\n");
exit(0);
break;
}
}
}void menuForSend(int fd)
{
while(1)
{
printf("1.群发\n");
printf("2.私聊\n");
printf("3.退出\n");
printf("请输入你的选择: ");
char choice;
scanf("%c", &choice);
getchar();
switch (choice)
{
case '1':
{
char buf[BUF_SIZE]={0};
char temp[500];
printf("请输入要发送的内容: ");
scanf("%s", temp);
getchar();
snprintf(buf, sizeof(buf), "%c%s", GROUP_SEND, temp);
send(fd, buf, strlen(buf), 0);
break;
}
case '2':
{
char buf[BUF_SIZE] = {0};
char temp[500];
printf("请输入要发送的内容: ");
scanf("%s", temp);
getchar();
printf("请输入要发送的对象的ip ");
char ip[20] = {0};
scanf("%s", ip);
getchar();
snprintf(buf, sizeof(buf), "%c:%s:%s", SINGLE_SEND,ip, temp);
send(fd, buf, strlen(buf), 0);
break;
}
case '3':
{
close(fd);
exit(0);
}
default:
{
printf("输入错误\n");
break;
}
}
}
}#include "command.h"
#include
#include
#include
#include
#include
#include
#include
#include "menuFunctions.h"int main()
{
int fd;
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd==-1)
{
perror("socket failed");
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(9000);
addr.sin_addr.s_addr=inet_addr("192.168.2.160");
if(connect(fd,(struct sockaddr*)&addr,sizeof(addr))==-1)
{
perror("connect failed");
exit(-1);
}
pid_t pid=fork();
if(pid==-1)
{
perror("fork failed");
exit(-1);
}
else if(pid==0)
{
sleep(15);
while(1)
{
char buf[BUF_SIZE];
int len=read(fd,buf,sizeof(buf));
if(len==-1)
{
perror("read failed");
exit(-1);
}
else if(len==0)
{
printf("服务器已关闭\n");
exit(0);
}
else
{
printf("%s\n",buf);
}
}
}
else
{
menuForLoginAndRegister(fd);
wait(NULL);
}}