C++/Qt: QTcpSocket won't write after reading -
i creating network client application sends requests server using qtcpsocket , expects responses in return. no higher protocol involved (http, etc.), exchange simple custom strings.
in order test, have created tcp server in python listens on socket , logs strings receives , sends back.
i can send first request ok , expected response. however, when send second request, not seem written network.
i have attached debug slots qtcpsocket
's notification signals, such byteswritten(...)
, connected()
, error()
, statechanged(...)
, etc. , see connection being established, first request sent, first response processed, number of bytes written - adds up...
only second request never seems sent :-(
after attempting send it, socket sends error(remotehostclosederror)
signal followed closingstate
, unconnectedstate
state change signals.
before go deeper this, couple of (probably basic) questions:
- do need "clear" underlying socket in way after reading ?
- is possible / probable not reading data server has sent me prevents me writing ?
- why server close connection ? or sign not right ? tried setting
lowdelay
,keepalive
socket options, didn't change anything. i've checked socket'sstate()
,isvalid()
, they're - although latter returnstrue
when unconnected... - in earlier version of application, closed , re-opened connection before sending request. worked ok. prefer keeping connection open though. not reasonable approach ? 'canonical' way to implement tcp network communication ? read/write or re-open every time ?
- does way read socket have impact on how can write ? sample code uses
readall(...)
available data; read piece piece need ,<<
qtextstream when writing...
could possibly bug in qt event loop ? have observed output in qt creator console created qdebug() << ...
gets cut short, i.e. stops. more output printed when shut down application.
this latest qt 5.4.1 on mac os x 10.8, issue occurs on windows 7.
update after first answer , comments:
the test server dead simple , taken official python socketserver.tcpserver
example:
import socketserver class mytcphandler(socketserver.streamrequesthandler): def handle(self): request = self.rfile.readline().strip() print "rx [%s]: %s" % (self.client_address[0], request) response = self.processrequest(request) print "tx [%s]: %s" % (self.client_address[0], response) self.wfile.write(response) def processrequest(self, message): if message == 'request type 01': return 'response type 01' elif message == 'request type 02': return 'response type 02' if __name__ == "__main__": server = socketserver.tcpserver(('localhost', 12345), mytcphandler) server.serve_forever()
the output is
rx [127.0.0.1]: request type 01 tx [127.0.0.1]: response type 01
also, nothing happens when re-send message after - not surprising socket closed. guess i'll have figure out why closed...
next update:
i've captured network traffic using wireshark , while network stuff doesn't tell me lot, see first request , response. right after client [ack]
nowledges response, server sends connection finish (fin)
. don't see second request anywhere.
last update:
i have posted follow-up question @ python: socketserver closes tcp connection unexpectedly.
only second request never seems sent :-(
i highly recommend running program wireshark , seeing packets getting sent , received across network. (as is, can't know sure whether bug on client side or in server, , first thing need figure out)
do need "clear" underlying socket in way after reading ?
no.
is possible / probable not reading data server has sent me prevents me writing ?
no.
why server close connection ?
it's impossible without looking @ server's code.
does or sign not right ?
again, depend on how server written.
this worked ok. prefer keeping connection open though. not reasonable approach ?
keeping connection open reasonable approach.
what 'canonical' way to implement tcp network communication ? read/write or re-open every time ?
neither canonical; depends on attempting accomplish.
does way read socket have impact on how can write ?
no.
could possibly bug in qt event loop ?
that's extremely unlikely. qt code has been used years tens of thousands of programs, bug serious have been found , fixed long ago. it's more either there bug in client, or bug in server, or mismatch between how expect api call behave , how behaves.