qtcpsocket

qt simple tcp communication with ui projects

依然范特西╮ 提交于 2021-01-29 07:19:56
问题 I want to create a simple Tcp Communication project but I get some problems and I dont know how to solve that problem. When I try to find solution all people tell add this code (QT += network)on .pro file but in ui projects I dont have any pro file so I dont know the find the solution. //commu.h #ifndef COMMU_H #define COMMU_H #include <QtWidgets/QMainWindow> #include "ui_commu.h" #include <QtNetwork/QTcpSocket> #include <QObject> #include <QString> class commu : public QMainWindow { Q_OBJECT

Bind a QTcpSocket to a specific port

≡放荡痞女 提交于 2020-06-23 08:25:42
问题 I am connecting through a QTcpSocket to a QTcpServer . I can specify the listening port on the Server side, but the client chooses a random port for its connection. I have tried to use the method QAbstractSocket::bind but that made no difference. Here is my code: void ConnectionHandler::connectToServer() { this->socket->bind(QHostAddress::LocalHost, 2001); this->socket->connectToHost(this->ip, this->port); if (!this->socket->waitForConnected()) { this->socket->close(); this->errorMsg = this-

PyQt5: Sending and receiving messages between client and server

∥☆過路亽.° 提交于 2020-01-01 19:27:17
问题 I am trying to create a server and client to send and receive messages. My problem is to send and receive between the client and the server. client.py : from PyQt5.QtCore import QIODevice from PyQt5.QtWidgets import QApplication, QDialog from PyQt5.QtNetwork import QTcpSocket class Client(QDialog): def __init__(self): super().__init__() HOST = '127.0.0.1' PORT = 8000 self.tcpSocket = QTcpSocket(self) self.tcpSocket.connectToHost(HOST, PORT, QIODevice.ReadWrite) self.tcpSocket.readyRead

How to read complete data in QTcpSocket?

纵饮孤独 提交于 2019-12-29 07:42:09
问题 Now the server (implemented with java) will send some stream data to me, my code is like below: connect(socket, SIGNAL(readyRead()), this, SLOT(read_from_server())); in the read_from_server() : { while (socket->bytesAvailable()) { QString temp = socket->readAll(); } } but I find that even the server sent me a string with only several characters, the data is truncated, and my function is called twice, thus temp is the never complete data that I want. If server send me a longer string, my

Is the QTcpSocket (or QSslSocket) which underlies a QWebSocket — destroyed by itself when a socket error happens?

混江龙づ霸主 提交于 2019-12-23 18:17:40
问题 When a QTcpSocket (or QSslSocket ) is upgraded to a QWebSocket , the former has to be stored for the future purpose, as it requires to be ... moved to the same thread wherever QWebSocket is being moveToThread() deleteLater() whenever QWebSocket is being destroyed Failing to do 1. results in undefined behaviour and most likely a crash. While failing to do 2. results in a memory leak, which is more prominent if you are having a QWebSocketServer (QWebSocketServer - not releasing memory). I

QTcpSocket connecting results in UnknownSocketError with errorString “UnknownError”

半腔热情 提交于 2019-12-12 03:49:03
问题 Problem I am unable to get any further information regarding this error: QAbstractSocket::UnknownSocketError The QT QAbstractSocket::SocketError provides only a basic explanation that some error has occurred An unidentified error occurred. enum value = -1 Calling QTcpSocket::errorString() provides this: "Unknown error" There is one question regarding this here on SO but provides no real solution to solving the issue (and what was suggested I have done) I have absoltely no idea how to further

Pyqt ; QTcpSocket always in connectingState after server restart;

≯℡__Kan透↙ 提交于 2019-12-11 08:18:42
问题 I've got a subclass of QTcpSocket. And problem is : when i firt time connect to server - everything ok, but after socket connected i restart server (python socketServer,just close and start script again) socket disconnecting and tryin to reconnect while server is down, but when i start server again - nothing happened, socket.state() always in ConnectingState.. what is wrong ? Here example code: # -*- coding: utf-8 -*- from PyQt4.QtCore import QVariant, QTimer, pyqtSignal, QCoreApplication

How to write on multiple QTcpSockets using QThreads?

微笑、不失礼 提交于 2019-12-11 05:49:07
问题 I've been struggling for a couple of days now with this problem on QTcpSockets and QThreads. I have a QTcpServer that listens on a port and creates a new Client using the nextPendingConnection(). So now the client has a qtcpsocket which I can use for reading and writing. Let's suppose I have 100 clients connected to my server. When one of them wants to broadcast a message to everybody, my main thread (where I create the clients with nextPendingConnection() ) will have to iterate over 100

Why can't my code read the number of bytes available on a QTcpSocket? Ideas?

人盡茶涼 提交于 2019-12-11 01:59:49
问题 I have the following snippet of code in the body of a loop responsible for reading data from a QTcpSocket (nntp is a pointer to a QTcpSocket). std::vector<char> buffer; int bytesAvailable = nntp->bytesAvailable(); qDebug() << "bytesAvailable: "<<bytesAvailable; if(bytesAvailable <= 0) break; buffer.resize(bytesAvailable); bytesRead = nntp->read(&buffer[0], bytesAvailable); qDebug() << (nntp->state() == QAbstractSocket::ConnectedState); qDebug() << "bytesRead: "<<bytesRead; Intermittently,

Get remote host Ip address QTcpServer

两盒软妹~` 提交于 2019-12-10 13:20:12
问题 I'm using Qt to create TCP server using QTcpServer. Every time a client connects to server, I would like to know the remote host's IP address and port number. I tried searching documentation but couldn't find any information on this topic. I know there should be a way, I'm just not able to figure it out. Help please. 回答1: QTcpServer won't tell you the address/port directly on an incoming connection but you can get it by getting the connected QTcpSocket from QTcpServer::nextPendingConnection()