基于tcp和qt的简单聊天室搭建
2016-08-10 18:48
405 查看
使用Qt库中的 <QTcpServer> 和<QTcpSocket>类实现局域网络下的聊天室。
分为服务端和客户端;
服务端接收来自各个客户端的信息,并发送到所有客户端;
客户端用于用户登陆及聊天。
客户端:
使用<QTcpSocket>类即可;
tcp_client.h
namespace Ui {
class tcp_cilent;
}
class tcp_cilent : public QWidget
{
Q_OBJECT
public:
explicit tcp_cilent(QWidget *parent = 0);
~tcp_cilent();
private slots:
void on_pushButtonconnect_clicked();
void on_send_clicked();
void slotConnected();
void slotDisconnected();
void slotError(QAbstractSocket::SocketError);
void slotStateChanged(QAbstractSocket::SocketState);
void slotReadData();
//bool isconnect;
private:
Ui::tcp_cilent *ui;
QTcpSocket*tcpsocket;
};tcp_client.cpp
服务器端
#ifndef MYTCPSOCKET_H
#define MYTCPSOCKET_H
#include <QObject>
#include<QTcpSocket>
class MyTcpSocket : public QTcpSocket
{
Q_OBJECT
public:
MyTcpSocket(QObject*parent=0);
~MyTcpSocket();
signals:
void disconnected(MyTcpSocket*);
void updateMsg(QByteArray);
private slots:
void slotDisconnected();
void slotReadData();
};
#endif // MYTCPSOCKET_H
分为服务端和客户端;
服务端接收来自各个客户端的信息,并发送到所有客户端;
客户端用于用户登陆及聊天。
客户端:
使用<QTcpSocket>类即可;
tcp_client.h
namespace Ui {
class tcp_cilent;
}
class tcp_cilent : public QWidget
{
Q_OBJECT
public:
explicit tcp_cilent(QWidget *parent = 0);
~tcp_cilent();
private slots:
void on_pushButtonconnect_clicked();
void on_send_clicked();
void slotConnected();
void slotDisconnected();
void slotError(QAbstractSocket::SocketError);
void slotStateChanged(QAbstractSocket::SocketState);
void slotReadData();
//bool isconnect;
private:
Ui::tcp_cilent *ui;
QTcpSocket*tcpsocket;
};tcp_client.cpp
tcp_cilent::tcp_cilent(QWidget *parent): QWidget(parent), ui(new Ui::tcp_cilent) { ui->setupUi(this); ui->send->setDisabled(true); ui->lineEditsend->setDisabled(true); //isconnect=false; } tcp_cilent::~tcp_cilent() { delete ui; } void tcp_cilent::on_pushButtonconnect_clicked() { tcpsocket=new QTcpSocket(this); connect(tcpsocket,SIGNAL(connected()),this,SLOT(slotConnected())); connect(tcpsocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected())); connect(tcpsocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slotError(QAbstractSocket::SocketError))); connect(tcpsocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(slotStateChanged(QAbstractSocket::SocketState))); connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(slotReadData())); QHostAddress host(ui->LineEdithost->text()); tcpsocket->connectToHost(host,ui->lineEditport->text().toShort()); //isconnect=true; // ui->pushButtonconnect->setText("close"); /* else { isconnect=false; ui->pushButtonconnect->setText("connect"); }*/ } void tcp_cilent::slotReadData() { while(tcpsocket->bytesAvailable()>0) { QByteArray ba; ba.resize(tcpsocket->bytesAvailable()); tcpsocket->read(ba.data(),ba.size()); ui->listWidget->addItem(QString(ba)); ui->listWidget->scrollToBottom(); } } void tcp_cilent::slotConnected() { //isconnect=true; ui->send->setDisabled(false); ui->lineEditsend->setDisabled(false); } void tcp_cilent::slotDisconnected() { //isconnect=false; qDebug()<<"connecttion lost\n"; ui->send->setDisabled(true); ui->lineEditsend->setDisabled(true); } void tcp_cilent::slotError(QAbstractSocket::SocketError err) { qDebug()<<"error"<<err; } void tcp_cilent::slotStateChanged(QAbstractSocket::SocketState s) { qDebug()<<"state"<<s; } void tcp_cilent::on_send_clicked() { tcpsocket->write(ui->user->text().toUtf8()+": "+ui->lineEditsend->text().toUtf8()); ui->lineEditsend->clear(); }
服务器端
#ifndef MYTCPSOCKET_H
#define MYTCPSOCKET_H
#include <QObject>
#include<QTcpSocket>
class MyTcpSocket : public QTcpSocket
{
Q_OBJECT
public:
MyTcpSocket(QObject*parent=0);
~MyTcpSocket();
signals:
void disconnected(MyTcpSocket*);
void updateMsg(QByteArray);
private slots:
void slotDisconnected();
void slotReadData();
};
#endif // MYTCPSOCKET_H
相关文章推荐
- Qt---基于TCP聊天室
- java基于socket tcp的简单聊天室
- fedora14 基于Qt的TCP发送文本信息 聊天室 开发记录
- PyQt5基于QTcpSocket的简单聊天室
- 翻译QT基于TCP聊天室server
- Android搭建简单的socket服务器——基于TCP
- 基于Qt tcp的聊天室
- 使用Silverlight4与Wcf4的Net.tcp实现的简单聊天室
- 基于TCP的简单FTP程序
- 基于TCP模式的socket编程 简单封装
- linux下基于TCP简单的socket通信程序
- 搭建flex + blazeds 最简单的messaging例子:简易聊天室
- 【socket编程】一个简单的基于TCP的客户/服务端例子(vs2008)
- 用QT搭建简单的播放器外壳
- 基于WebSocet简单聊天室(NodeJS + node-websocket-server)
- Qt 简单的 tcp socket 通信流程
- 以异步的方式操作TCP/IP套接字——以异步方式实现简单的聊天室
- 使用Silverlight4与Wcf4的Net.tcp实现的简单聊天室
- 以异步的方式操作TCP/IP套接字——以异步方式实现简单的聊天室