Solved Mental difficulty understanding what is wrong...
-
@jsulm, the return from bind is -1, always -1 when it fails. The only reason I haven't used QTcpSocket is familiarity. I will take a look now.
-
@SPlatten Sorry, I actually mean to check errno.
-
@jsulm , errno is 0x3c (60), which according to is a timeout...
-
@SPlatten Why do you call bind() in the second application? The second application should connect(), not bind().
-
@jsulm , thank you, how do I call connect for the socket, the option connects appearing are for signals and slots.
-
This post is deleted! -
@jsulm , I realised that, but how do I get Qt to use that, I've modified the code to:
if ( blnIsModule == true ) { intRC = connect(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr); } else { intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr); }
It won't compile. If I "Find references under cursor" for bind I get the header:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/socket.h
For connect there are loads, but the above isn't one of them.
-
@SPlatten You can put this networking code into a class which does not inherit from QObject.
Or simply use networking functionality from Qt. -
@jsulm said in Mental difficulty understanding what is wrong...:
You can put this networking code into a class which does not inherit from QObject.
Or learn C++ and use '::connect(...)' to access the non-namespaced function :-P
-
I'm making a real meal of this and so far not doing very well. The main application creates a socket:
int intSocket = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in srvAddr; bzero((char*)&srvAddr, sizeof(srvAddr)); srvAddr.sin_family = AF_INET; srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); srvAddr.sin_port = htons(8123); socklen_t tSvrAddr = sizeof(srvAddr); int intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
intRC is 0, which is successful. I then launch a child process:
QString strName = strFullPath.mid(intLastSep + 1) ,strPath = strFullPath.mid(0, intLastSep + 1); clsMainWnd::mspobjProcess->setArguments(slstArgs); clsMainWnd::mspobjProcess->setWorkingDirectory(strPath); clsMainWnd::mspobjProcess->setProgram(strName); clsMainWnd::mspobjProcess->startDetached(&int64PID);
The child process is launched and I can see the process ID with:
ps -A | grep "mdFileIO"
The child process is supposed to send a heartbeat, when it starts it creates a socket:
int intSocket = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in srvAddr; bzero((char*)&srvAddr, sizeof(srvAddr)); srvAddr.sin_family = AF_INET; srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); srvAddr.sin_port = htons(8123); socklen_t tSvrAddr = sizeof(srvAddr); int intRC = ::connect(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
intRC is 0 which is successful. I then install a thread that issues a heartbeat message using the socket connected to above:
QJsonObject objHeartbeat; objHeartbeat.insert(clsModHelper::mscszModule, clsModHelper::msszTitle); QJsonDocument objDoc(objHeartbeat); QByteArray bytArr(objDoc.toJson(QJsonDocument::Compact)); tWritten = write(intSocket, bytArr, bytArr.size());
tWritten is > 0, so sent successfully, but I do not see the heartbeat message in the main application where the receiving handler is:
while( mpThread != nullptr ) { struct sockaddr_in cliAddr; socklen_t tCliLen = sizeof(cliAddr); int intNewSocket = accept(mintSocket, (struct sockaddr*)&cliAddr, &tCliLen); if ( intNewSocket < 0 ) { continue; } ... }
No accept is processed, so it appears although the connect is successful in the client, the main application isn't processing the accept.
-
yeah, you're not using the BSD sockets interface properly. I'd also suggest using Qt sockets API since 1) it's higher level, and 2) you're asking in a Qt specific forum.
-
All sorted now ! I haven't posted the answer, because it was a lot of source to write.
-
@SPlatten said in Mental difficulty understanding what is wrong...:
All sorted now !
great, so please don't forget to mark your post as solved!