From 098d039040b1cb087e9a1600506c792be3671f6f Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Mon, 25 Nov 2019 20:32:39 +0800 Subject: [PATCH] feat: QTcpServer and QTcpSocket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TCP通信之QTcpServer和QTcpSocket,服务器和客户端通讯 --- QtTcpEx/ExTcpClient/ExTcpClient.cpp | 21 +++++++++++++++++++++ QtTcpEx/ExTcpClient/ExTcpClient.h | 21 +++++++++++++++++++++ QtTcpEx/ExTcpServer/ExTcpServer.cpp | 24 ++++++++++++++++++++++-- QtTcpEx/ExTcpServer/ExTcpServer.h | 21 +++++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.cpp b/QtTcpEx/ExTcpClient/ExTcpClient.cpp index e37f70f..30b0a4a 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.cpp +++ b/QtTcpEx/ExTcpClient/ExTcpClient.cpp @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * github: https://github.com/touwoyimuli + * blogs: https://touwoyimuli.github.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "ExTcpClient.h" #include "ui_ExTcpClient.h" diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.h b/QtTcpEx/ExTcpClient/ExTcpClient.h index 9f543e7..4eddf4a 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.h +++ b/QtTcpEx/ExTcpClient/ExTcpClient.h @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * github: https://github.com/touwoyimuli + * blogs: https://touwoyimuli.github.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #ifndef EXTCPCLIENT_H #define EXTCPCLIENT_H diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.cpp b/QtTcpEx/ExTcpServer/ExTcpServer.cpp index 111ca76..69178d3 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.cpp +++ b/QtTcpEx/ExTcpServer/ExTcpServer.cpp @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * github: https://github.com/touwoyimuli + * blogs: https://touwoyimuli.github.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "ExTcpServer.h" #include "ui_ExTcpServer.h" @@ -131,10 +152,9 @@ void ExTcpServer::onNewConnection() m_tcpSocket = m_tcpServer->nextPendingConnection(); //创建 socket connect(m_tcpSocket, SIGNAL(connected()), this, SLOT(onClientConnected())); - onClientConnected(); //本函数有写 connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(onClientDisonnected())); connect(m_tcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChange(QAbstractSocket::SocketState))); - onSocketStateChange(m_tcpSocket->state()); //本函数也有写 后面尝试删除此两行, 看看或不会有异常 + onSocketStateChange(m_tcpSocket->state()); connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead())); } diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.h b/QtTcpEx/ExTcpServer/ExTcpServer.h index 20835c4..b84f5d5 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.h +++ b/QtTcpEx/ExTcpServer/ExTcpServer.h @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * github: https://github.com/touwoyimuli + * blogs: https://touwoyimuli.github.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #ifndef EXTCPSERVER_H #define EXTCPSERVER_H