QAbstractSocket not sending
-
Hi all -
Working on a utility that will read a file, and transfer the contents to a socket. The writes aren't working -- they're not showing up in Wireshark, and my slot that catches the bytesWritten() signal isn't executed.
I'm running from a QWidget (don't know whether that matters); here's some of the code:
QVariant one(1); m_sock->setSocketOption(QAbstractSocket::LowDelayOption, one); m_sock->connectToHost(ui->lineEditAddr->text(), (quint16) ui->lineEditPort->text().toInt(), QIODevice::ReadWrite, QAbstractSocket::IPv4Protocol); ... socketBytesWritten = sock->write((const char *) bufOut, bufLen);I've tried this with varying bufLens between 15 and ~2000. The write() call returns the same value, but nothing is getting sent.
What might I be doing wrong here? Thanks.
-
Hi,
@mzimmers said in QAbstractSocket not sending:
connectToHost
Are you sure you are connected before sending ?
Did you try to add a call to waitForConnected before sending anything ? -
Hi SGaist - just to be sure, I added this:
while (sock->waitForConnected(1000) == false) { cout << "waiting for socket connection." << endl; } cout << "socket is connected." << endl; socketBytesWritten = sock->write((const char *) bufOut, bufLen);Immediately falls through to the write() call.
I remember a few months ago, I was having another problem with a socket read, and it was because I wasn't honoring the event loop. Could I be making a similar mistake here? What's really strange is, there's another part of this program that uses the same socket, and it writes just fine.
-
What is bufOut ?
What does it contain ? -
bufOut is a char array. It holds records from an Intel Extended Hex file. All bytes are either the colon (":") or an ASCII hex character ('0', '1', etc). There are newlines ('\n') in there as well.
Have you connected to the error signals of the socket? What kind of socket is it, UDP or TCP?
-
Have you connected to the error signals of the socket? What kind of socket is it, UDP or TCP?
@kshegunov yes I have...nothing happens there. It's a TCP socket.
-
I suspect socket is not connected. To ensure that it is really connected look for 6 signal defined the socket. One of them should give hint. If it is really connected atleast it should receive the connected signal.
- How do you say data is not sent ?
- On the other side of the socket, are able to get the newConnection request ?
- Have you connected to readyRead at the other side ? Just check if some signal is not connected
- Are you doing any of these operation before even starting the event loop ?
-
Hi all -
Working on a utility that will read a file, and transfer the contents to a socket. The writes aren't working -- they're not showing up in Wireshark, and my slot that catches the bytesWritten() signal isn't executed.
I'm running from a QWidget (don't know whether that matters); here's some of the code:
QVariant one(1); m_sock->setSocketOption(QAbstractSocket::LowDelayOption, one); m_sock->connectToHost(ui->lineEditAddr->text(), (quint16) ui->lineEditPort->text().toInt(), QIODevice::ReadWrite, QAbstractSocket::IPv4Protocol); ... socketBytesWritten = sock->write((const char *) bufOut, bufLen);I've tried this with varying bufLens between 15 and ~2000. The write() call returns the same value, but nothing is getting sent.
What might I be doing wrong here? Thanks.
-
@kshegunov yes I have...nothing happens there. It's a TCP socket.
What is the socket state just before you write the data? The TCP socket can be open for writing (i.e.
isValid()returns true) even if it's in theConnectingstate. -
Hey guys - sorry it took me so long to reply; I took a break from this over the weekend.
Aha: sock is an argument passed to a non-member function. It's the same socket as m_sock. (EDIT: all functions are now member functions, and it uses m_sock as does the successful write; still not working here.)
ksheg: it's "QAbstractSocket::ConnectedState." I actually test for it twice before sending.
dheerendra: the connection is legitimate:
And again, another part of this program does successfully send on that socket. I can see it in Wireshark. It's just this send that doesn't show up on Wireshark.
EDIT: some additional information: the write() that I can see in Wireshark is called from a slot. The actual writing doesn't occur until the slot is exited, and control returns somewhere deep in Qt-land. This reinforces my suspicion that I'm doing something wrong in terms of blocking the event loop or something. I'm really lost as to what to do, though.
-
Did you check for any busy loop in your code ?
-
You mean a compute-intensive loop? Nothing like that. When I'm in autosend mode, there's a timer that can be quite brief, but that's not being used now.
It's something about my widget -- the write() doesn't complete until control passes out of it. I did a bunch of step-outs in the debugger, and it wasn't until I got to the very end of the stack that the writing actually occurred.
-
Found a solution -- calling m_sock->flush() after my write sends the stuff before control returns to the event loop. Not sure I really understand why this is needed, but it works. Thanks to all who looked.
actually the docs explains why ;-)
http://doc.qt.io/qt-5/qabstractsocket.html#flush
Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop
-
actually the docs explains why ;-)
http://doc.qt.io/qt-5/qabstractsocket.html#flush
Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop