Mental difficulty understanding what is wrong...
-
I create a socket:
int intSocket = socket(AF_INET, SOCK_STREAM, 0); if ( intSocket < 0 ) { clsDebugService::exitWhenDebugQueueEmpty("Failed to create socket!"); } //Initliase and get address of localhost 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(uint16Port); char* pszIP = inet_ntoa(srvAddr.sin_addr); if ( pszIP != nullptr ) { qdbg() << "Setting up socket on ip: " << pszIP << ", port: " << uint16Port << ((strPurpose.isEmpty() == true) ? "" : strPurpose); } socklen_t tSvrAddr = sizeof(srvAddr);
The port I'm using is 8123, I then bind the address
int intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr); if ( intRC < 0 ) { clsDebugService::exitWhenDebugQueueEmpty("Cannot bind to socket!"); }
So far so good, everything is ok, listen for incoming requests:
if ( listen(intSocket, 5) < 0 ) { clsDebugService::exitWhenDebugQueueEmpty("Cannot listen to socket!"); }
Another application I'm working on running on the same system does exactly the same, where it will send requests to the first application on the same address and port, but this time it returns -1 on the bind. What I'm looking to achieve is a similar interface to a HTTP client / server where the web-server listens to port 80 and clients submit requests on the same address and port...what am I missing?
-
@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.
-
@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.
-
@SPlatten said in Mental difficulty understanding what is wrong...:
All sorted now !
great, so please don't forget to mark your post as solved!