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