-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ab1741
commit 2a8c730
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef CHATROOM_CLIENT_H | ||
#define CHATROOM_CLIENT_H | ||
|
||
#include <string> | ||
#include "Common.h" | ||
|
||
using namespace std; | ||
|
||
// 客户端类,用来连接服务器发送和接收消息 | ||
class Client { | ||
|
||
public: | ||
// 无参数构造函数 | ||
Client(); | ||
|
||
// 连接服务器 | ||
void Connect(); | ||
|
||
// 断开连接 | ||
void Close(); | ||
|
||
// 启动客户端 | ||
void Start(); | ||
|
||
private: | ||
|
||
// 当前连接服务器端创建的socket | ||
int sock; | ||
|
||
// 当前进程ID | ||
int pid; | ||
|
||
// epoll_create创建后的返回值 | ||
int epfd; | ||
|
||
// 创建管道,其中fd[0]用于父进程读,fd[1]用于子进程写 | ||
int pipe_fd[2]; | ||
|
||
// 表示客户端是否正常工作 | ||
bool isClientwork; | ||
|
||
// 聊天信息缓冲区 | ||
char message[BUF_SIZE]; | ||
|
||
//用户连接的服务器 IP + port | ||
struct sockaddr_in serverAddr; | ||
}; | ||
|
||
|
||
|
||
#endif //CHATROOM_CLIENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef CHATROOM_COMMON_H | ||
#define CHATROOM_COMMON_H | ||
|
||
#include <iostream> | ||
#include <list> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <sys/epoll.h> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// 默认服务器端IP地址 | ||
#define SERVER_IP "127.0.0.1" | ||
|
||
// 服务器端口号 | ||
#define SERVER_PORT 8888 | ||
|
||
// int epoll_create(int size)中的size | ||
// 为epoll支持的最大句柄数 | ||
#define EPOLL_SIZE 5000 | ||
|
||
// 缓冲区大小65535 | ||
#define BUF_SIZE 0xFFFF | ||
|
||
// 新用户登录后的欢迎信息 | ||
#define SERVER_WELCOME "Welcome you join to the chat room! Your chat ID is: Client #%d" | ||
|
||
// 其他用户收到消息的前缀 | ||
#define SERVER_MESSAGE "ClientID %d say >> %s" | ||
|
||
// 退出系统 | ||
#define EXIT "EXIT" | ||
|
||
// 提醒你是聊天室中唯一的客户 | ||
#define CAUTION "There is only one int the char room!" | ||
|
||
|
||
// 注册新的fd到epollfd中 | ||
// 参数enable_et表示是否启用ET模式,如果为True则启用,否则使用LT模式 | ||
static void addfd( int epollfd, int fd, bool enable_et ) | ||
{ | ||
struct epoll_event ev; | ||
ev.data.fd = fd; | ||
ev.events = EPOLLIN; | ||
if( enable_et ) | ||
ev.events = EPOLLIN | EPOLLET; | ||
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); | ||
// 设置socket为nonblocking模式 | ||
// 执行完就转向下一条指令,不管函数有没有返回。 | ||
fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0)| O_NONBLOCK); | ||
printf("fd added to epoll!\n\n"); | ||
} | ||
|
||
#endif // CHATROOM_COMMON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef CHATROOM_SERVER_H | ||
#define CHATROOM_SERVER_H | ||
|
||
#include <string> | ||
|
||
#include "Common.h" | ||
|
||
using namespace std; | ||
|
||
// 服务端类,用来处理客户端请求 | ||
class Server { | ||
|
||
public: | ||
// 无参数构造函数 | ||
Server(); | ||
|
||
// 初始化服务器端设置 | ||
void Init(); | ||
|
||
// 关闭服务 | ||
void Close(); | ||
|
||
// 启动服务端 | ||
void Start(); | ||
|
||
private: | ||
// 广播消息给所有客户端 | ||
int SendBroadcastMessage(int clientfd); | ||
|
||
// 服务器端serverAddr信息 | ||
struct sockaddr_in serverAddr; | ||
|
||
//创建监听的socket | ||
int listener; | ||
|
||
// epoll_create创建后的返回值 | ||
int epfd; | ||
|
||
// 客户端列表 | ||
list<int> clients_list; | ||
}; | ||
|
||
|
||
|
||
#endif //CHATROOM_SERVER_H |