C/C++ 网络聊天室 客户端(qt,windows) 服务器(Linux)
Qt的学习成果,抛砖引玉,博君一笑//I/O复用epoll模型
//服务器代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#defineMAXEVENTS 128
//变量声明;
int userSock; //套接字数组;
char userId; //id数组;
int maxfd,listenfd; //最大套接字,监听套接字;
//函数声明;
//套接字初始化;
int IniServer(int port);
//获取xml字符串;
bool GetXmlBuffer(const char *xml,const char *label,char *val);
bool GetXmlBuffer(const char *xml,const char *label,int *val);
bool GetXmlBuffer(const char *xml,const char *label,double *val);
//消息处理;
void dealLogin(int eventfd,char*buffer);
void dealOut(int eventfd,char *buffer);
void dealMail(int evetfd,char *buffer);
//主函数;
int main(int argc,char* argv[])
{
if(argc !=2 )
{
printf("请指定端口号,例如:./xxx 5081");
return -1;
}
listenfd = IniServer(atoi(argv));
if(listenfd <= 0) {printf("初始化失败\n");return -1;}
memset(userSock,0,sizeof(userSock));
for(int i = 0;i < 1024;i++) memset(userId,0,sizeof(userId));
userSock = listenfd;
maxfd = listenfd;
int epollfd = epoll_create(1);
struct epoll_event add;
add.data.fd = listenfd;
add.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,listenfd,&add);
while(1)
{
struct epoll_event events;
int infds = epoll_wait(epollfd,events,MAXEVENTS,-1);
if(infds < 0) {printf("epoll_wait failed\n");return -1;}
if(infds == 0) {printf("超时\n");continue;}
for(int i = 0;i < infds;i++)
{
if(events.data.fd == listenfd && (events.events&EPOLLIN)) //监听事件;
{
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);
int clientfd = accept(listenfd,(struct sockaddr*)&clientaddr,&len);
if(clientfd <= 0) {printf("与客户端连接失败\n");continue;}
memset(&add,0,sizeof(add));
add.data.fd = clientfd;
add.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,clientfd,&add);
ntohl(clientaddr.sin_addr.s_addr);
printf("客户端%s连接到服务器\n",inet_ntoa(clientaddr.sin_addr));
userSock = clientfd;
maxfd = clientfd>maxfd?clientfd:maxfd;
}
else if(events.events&EPOLLIN) //读事件;
{
char buffer;
memset(buffer,0,sizeof(buffer));
int iret = recv(events.data.fd,buffer,sizeof(buffer),0);
if(iret <= 0)
{
add.data.fd = events.data.fd;
add.events = events.events;
epoll_ctl(epollfd,EPOLL_CTL_DEL,events.data.fd,&add);
close(events.data.fd);
printf("socket(%d)已断开连接\n",events.data.fd);
}
else
{
printf("接收:%s\n",buffer);
char val = {0};
if(GetXmlBuffer(buffer,"login",val))
{
dealLogin(events.data.fd,buffer);
continue;
}
else if(GetXmlBuffer(buffer,"out",val))
{
dealOut(events.data.fd,buffer);
continue;
}
else if(GetXmlBuffer(buffer,"mail",val))
{
dealMail(events.data.fd,buffer);
continue;
}
//printf("接收:%s\n",buffer);
//send(events.data.fd,buffer,sizeof(buffer),0);
}
}
}
}
}
//函数定义;
int IniServer(int port)
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd <= 0) return -1;
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0) return -1;
if(listen(sockfd,5)!=0) return -1;
return sockfd;
}
bool GetXmlBuffer(const char *xml,const char *label,char *val)
{
char *start,*end;
char label_start,label_end;
memset(label_start,0,sizeof(label_start));
memset(label_start,0,sizeof(label_end));
int label_len = strlen(label);
sprintf(label_start,"<%s>",label);
sprintf(label_end,"</%s>",label);
start = (char*)strstr(xml,label_start),end = (char*)strstr(xml,label_end);
if(start == NULL || end == NULL) return false;
strncpy(val,start+label_len+2,end-start-label_len-2);
return true;
}
bool GetXmlBuffer(const char *xml,const char *label,int *val)
{
char strval;
if(GetXmlBuffer(xml,label,strval) == false) return false;
*val = atoi(strval);
return true;
}
bool GetXmlBuffer(const char *xml,const char *label,double *val)
{
char strval;
if(GetXmlBuffer(xml,label,strval) == false) return false;
*val = atof(strval);
return true;
}
void dealLogin(int eventfd,char *buffer)
{
char tmp = {0};
for(int i = 0;i <= maxfd;i++)
{
if(userSock == 0) continue;
if(userSock == listenfd) continue;
send(userSock,buffer,strlen(buffer),0);
}
for(int i = 0;i <= maxfd;i++)
{
if(userSock == 0) continue;
if(userSock == listenfd) continue;
if(userSock == eventfd) continue;
memset(tmp,0,sizeof(tmp));
sprintf(tmp,"<login>%s</login>",userId]);
send(eventfd,tmp,strlen(tmp),0);
usleep(90000);
}
memset(tmp,0,sizeof(tmp));
GetXmlBuffer(buffer,"login",userId);
}
void dealOut(int eventfd,char *buffer)
{
for(int i = 0;i <= maxfd;i++)
{
if(userSock == 0) continue;
if(userSock == listenfd) continue;
if(userSock == eventfd) continue;
send(userSock,buffer,strlen(buffer),0);
}
for(int i = maxfd;i >= 0;i--)
{
if(userSock == 0) continue;
maxfd = userSock;
break;
}
userSock = 0;
memset(userId,0,sizeof(userId));
}
void dealMail(int eventfd,char *buffer)
{
char val = {0};
GetXmlBuffer(buffer,"mail",val);
char tmp = {0};
sprintf(tmp,"<mail>---------%s---------\n%s\n----------------------</mail>",\
userId,val);
for(int i = 0;i <= maxfd;i++)
{
if(userSock == 0) continue;
if(userSock == listenfd) continue;
send(userSock,tmp,strlen(tmp),0);
}
}
本帖最后由 弦歌雅意 于 2021-3-5 18:02 编辑
客户端代码(启动界面)
****************************************
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include "talk.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
talk t;
private slots:
void on_startbtn_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
******************************************
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QHostAddress>
#include <QTextEdit>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("启动程序");
connect(&t,&talk::Mysignal,
[=]()
{
this->show();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startbtn_clicked()
{
QString ipstr;
qint16 port;
t.id = ui->idline->text();
ipstr = ui->ipline->text();
port = ui->portline->text().toInt();
t.sockfd->connectToHost(QHostAddress(ipstr),port);
this->hide();
t.show();
} 本帖最后由 弦歌雅意 于 2021-3-5 18:03 编辑
客户端聊天界面
*************************************
头文件
#ifndef TALK_H
#define TALK_H
#include <QWidget>
#include <QTcpSocket>
#include <QString>
namespace Ui {
class talk;
}
class talk : public QWidget
{
Q_OBJECT
public:
explicit talk(QWidget *parent = nullptr);
~talk();
QTcpSocket *sockfd;
QString id;
signals:
void Mysignal();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::talk *ui;
};
#endif // TALK_H
************************************
源文件
#include "talk.h"
#include "ui_talk.h"
#include <QTcpSocket>
#include <QString>
#include <QDebug>
#include <QKeyEvent>
#include <QTextEdit>
//函数声明;
bool GetXmlBuffer(QString &recvStr,QString label);
talk::talk(QWidget *parent) :
QWidget(parent),
ui(new Ui::talk)
{
ui->setupUi(this);
this->setWindowTitle("聊天板块");
sockfd = NULL;
sockfd = new QTcpSocket(this);
connect(sockfd,&QTcpSocket::connected,
[=]()
{
ui->readtext->append("和服务器建立连接");
QString str = "<login>" + id + "</login>";
sockfd->write(str.toUtf8().data());
});
connect(sockfd,&QTcpSocket::readyRead,
[=]()
{
QByteArray array = sockfd->readAll();
QString str = QString(array);
if(GetXmlBuffer(str,"login"))
{
ui->idlist->addItem(str);
}
else if(GetXmlBuffer(str,"out"))
{
for(int i = 0;i < ui->idlist->count();i++)
{
if(str == ui->idlist->item(i)->text())
{
ui->idlist->takeItem(i);
}
}
}
else if(GetXmlBuffer(str,"mail"))
{
ui->readtext->append(str);
}
});
}
talk::~talk()
{
delete ui;
}
void talk::on_pushButton_clicked()
{
QString str = ui->writetext->toPlainText();
str = "<mail>" + str + "</mail>";
sockfd->write(str.toUtf8().data());
//ui->readtext->append(str);
ui->writetext->setText("");
}
void talk::on_pushButton_2_clicked()
{
QString str = "<out>" + id + "</out>";
sockfd->write(str.toUtf8().data());
sockfd->disconnectFromHost();
ui->readtext->append("已与服务器断开连接");
this->ui->readtext->setText("");
this->ui->writetext->setText("");
this->ui->idlist->clear();
this->hide();
emit Mysignal();
}
bool GetXmlBuffer(QString &recvStr,QString label)
{
QString start = "<" + label + ">";
QString end = "</" + label + ">";
int startPos = recvStr.indexOf(start);
if(startPos == -1) return false;
recvStr = recvStr.mid(start.length(),recvStr.length()-start.length()-end.length());
return true;
}
本帖最后由 弦歌雅意 于 2021-3-5 14:59 编辑
运行效果如下
本帖最后由 弦歌雅意 于 2021-3-5 15:13 编辑
我是采用了 epoll模型 来写服务器,并没有使用 多线程/多进程 的方式,所以服务器虽然是在linux运行的,但是和Windows差别不大,改了头文件,再稍微修改一下代码就能在Windows上运行
欢迎各位鱼c的大神提出建议,也欢迎初学的萌新找我交流
以后也许会做一篇详细的教程,包括 基础的socket套接字,以及io复用三种模型(select,poll,epoll)
另外,由于聊天室功能是一点一点完善的,走一步看一步,所以会有一些莫名明其妙的地方,因为修改的不够干净,请各位体谅。
最后贴一下ui设计
{:10_256:}{:10_256:}{:10_256:} 强啊
页:
[1]