qt_Qtcp

本文介绍了QTcpSocket和QTcpServer在QT框架中的应用,包括它们的信号处理,如acceptError和newConnection,并提供了Tcp聊天室服务器的构建实例和完整代码下载链接。
摘要由CSDN通过智能技术生成

QTcpSocket QTcpServer

在这里插入图片描述

函数 作用
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) 监听
void setMaxPendingConnections(int numConnections) 可以挂起的连接数量
QHostAddress serverAddress() const 地址
quint16 serverPort() const 端口

signalls:
void acceptError(QAbstractSocket::SocketError socketError)
void newConnection()

先来个小例子
在这里插入图片描述

/*
* 大致的过程如下:
* m_tcpserver=new QTcpServer(this);
* m_tcpserver->listen(QHostAddress::Any,30142);//监听所有ip的端口
*connect(m_tcpserver,SIGNAL(newConnection()),this,SLOT(newConnect())); 
*  m_tcpsocket = m_tcpserver->nextPendingConnection(); //得到每个连进来的socket
* connect(m_tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage())); 
*  m_tcpsocket->write(strMesg.toUtf8()); //发送
*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QPushButton>
#include<QTcpServer>
#include<QTcpSocket>
#include<QTextBrowser>
#include<QTextEdit>
class MainWindow : public QMainWindow
{
   
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void startTcpserver();
    void sendMessage();
    void newConnect();
    void readMessage();
private:
    QPushButton *start_bt,*send_bt;
    QTextEdit *input_edit;
    QTextBrowser *text_browser;
    QTcpServer *m_tcpserver;
    QTcpSocket *m_tcpsocket;

};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include<QDateTime>
#include<QAction>
#include<QHostAddress>
#include<QHBoxLayout>
#include<QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
   
    QWidget *wid=new QWidget;
    this->setCentralWidget(wid);
    QHBoxLayout *buttom_layout=new QHBoxLayout;
    QVBoxLayout *main_layout=new QVBoxLayout(wid);
    start_bt=new QPushButton("Start");
    send_bt=new QPushButton("Send");
    input_edit=new QTextEdit();
    text_browser=new QTextBrowser();
    buttom_layout->addStretch();
    buttom_layout->addWidget(start_bt);
    buttom_layout->addStretch();
    buttom_layout->addWidget(send_bt);
    buttom_layout->addStretch();
    buttom_layout->setSpacing(100);
    text_browser->setFixedHeight(300);
    main_layout->addWidget(text_browser);
    main_layout->addSpacing(30);
    input_edit->setFixedHeight(70);
    main_layout->addWidget(input_edit);
    main_layout->addSpacing(30);
    main_layout->addLayout(buttom_layout);
    main_layout->addStretch();
    main_layout->setContentsMargins(30,30,30,30);
    this->resize(500,500);
    this->setWindowTitle("My QQ Server");
    send_bt->setShortcut(Qt::Key_Enter);

    //=============以上为界面设置===========
    m_tcpserver=new QTcpServer(this);
    m_tcpsocket = new QTcpSocket;
    connect(start_bt,SIGNAL(clicked(bool)),this,SLOT(startTcpserver()));
    connect(send_bt,SIGNAL(clicked(bool)),this,SLOT(sendMessage()));
    this->show();


}

MainWindow::~MainWindow()
{
   

}
void  MainWindow::startTcpserver()
{
   
    m_tcpserver->listen(QHostAddress::Any,30142);//监听所有ip的端口30142
    qDebug()<<"start server";
    //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
    connect(m_tcpserver,SIGNAL(newConnection()),this,SLOT(newConnect()));
}
void MainWindow::newConnect()
{
   
    m_tcpsocket = m_tcpserver->nextPendingConnection(); //得到每个连进来的socket
    qDebug()<<"new Connect";
    text_browser->append(QString("%0").arg(m_tcpsocket->peerAddress().toIPv4Address())+" is connected");
    connect(m_tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽
}
void MainWindow::readMessage()
{
   
    QByteArray qba= m_tcpsocket->readAll(); //读取
    qDebug()<<qba;

    QString ss=QVariant(qba).toString();
    text_browser->append(QDateTime::currentDateTime().time().toString()+"\n"+ss);
}
void MainWindow::sendMessage()
{
   
    QString strMesg= input_edit->toPlainText();
    qDebug()<<strMesg;
    input_edit->clear
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值