After getting some sleep, I've written a more thorough description of my problem and how I am working with the sockets. This is all pseudo code.
SockA -> connect to local service.
SockB -> listen for connection. (localhost:9999)
SockC -> connect to remote service.
SockA -> send 'START on localhost:9999'
LSERVICE -> SockA 'OK'
LSERVICE -> connect to SockB
LSERVICE -> SockB 'INIT'
SockB -> SockC 'LIST'
SockC -> SockB '1234'
SockB -> LSERVICE '1234' (looks up what 1234 is)
LSERVICE -> SockA 'John'
LSERVICE -> SockA 'Q' (could be completely ignored and go straight to the next line)
LSERVICE -> SockA 'Public'
... more commands
I have the following setup:
lsServer QTcpServer (opens port 9999 on localhost)
rsServer QTcpSocket (connects to a remote service)
cmdRunnr QTcpSocket (connects to local service to issue commands)
I need to communicate between the 3 sockets in varying ways. I do not have control of the 'local service' program. lsServer and rsServer are non-threaded classes that read and write to each other's sockets.
For example:
connect(rsServer, SIGNAL(readyRead()), this, SLOT(readFromRemote()));
readFromRemote() {
input = rsServer->read(4096);
lsServer->write(input);
}
connect(rsServer, SIGNAL(readyRead()), this, SLOT(readFromRemote()));
readFromRemote() {
QByteArray input;
input = rsServer->read(4096);
lsServer->write(input);
}
To copy to clipboard, switch view to plain text mode
rsServer passes data that it receives to lsServer.
I use cmdRunnr to connect to a local service and use my program as a proxy of sorts to issue commands to a remote host that I'm directly connected to. The problem is, I need a way to wait until a variable is valid before continuing and I need a way to wait for input to return to the socket before continuing.
For example:
getValidList() {
QList<QByteArray> myList;
myList = getListFromRemote(cmd);
//... do stuff with myList ...
}
cmdRunnr = new CommandRunner(cmd);
cmdRunnr->start();
//... Find a way to get cmdRunnr->runnerList (QList<QByteArray>) ...
return runnerList;
}
in CommandRunner:
runnerCmd = cmd;
}
CommandRunner:run() {
mySocket.connectToHost(localhost, 4351);
//... do I connect()? ...
mySocket->write(runnerCmd);
mySocket->waitForReadyRead(5000);
input = mySocket->read(4096);
//... input should be OK ...
mySocket->waitForReadyRead(5000);
input = mySocket->read(4096);
//... input might be 'John' followed by more information or simply end at that ...
}
getValidList() {
QString cmd = "LIST";
QList<QByteArray> myList;
myList = getListFromRemote(cmd);
//... do stuff with myList ...
}
getListFromRemote(QString cmd) {
cmdRunnr = new CommandRunner(cmd);
cmdRunnr->start();
//... Find a way to get cmdRunnr->runnerList (QList<QByteArray>) ...
return runnerList;
}
in CommandRunner:
CommandRunner::CommandRunner(QObject *parent, QString cmd) : QThread { //constructor
runnerCmd = cmd;
}
CommandRunner:run() {
QByteArray input;
mySocket = new QTcpSocket();
mySocket.connectToHost(localhost, 4351);
//... do I connect()? ...
mySocket->write(runnerCmd);
mySocket->waitForReadyRead(5000);
input = mySocket->read(4096);
//... input should be OK ...
mySocket->waitForReadyRead(5000);
input = mySocket->read(4096);
//... input might be 'John' followed by more information or simply end at that ...
}
To copy to clipboard, switch view to plain text mode
The only way I know the data is finished is when the socket disconnects. There are headers I can read to determine if it's DATA or REPLY. I'm in a conundrum and am unsure of how to tackle this problem.
Bookmarks