main ()
{
/***** TO OTHER PROCESS *******************************************************/
/****** FROM OTHER PROCESS ****************************************************/
connect(m_tcpKeysFromServer, SIGNAL(newConnection()), this, SLOT(acceptConnectionFromKeysServer()));
serverListenFromKeysServer();
}
/************** CODE USED TO TRANSMITT A MESSAGGE **********************************************************************/
// this function send a message to other process
void ecmqMfgClient::sendToKeysServer()
{
m_keysServerTcpSocket->connectToHost(m_Params.IpAddressKeysServer, m_Params.PortToKeysServer);
connect(m_keysServerTcpSocket, SIGNAL(disconnected()),
this, SLOT(checkDisconnect()));
//1.
out << (quint16)0;
//2. port of client
out << m_Params.PortFromKeysServer;
//3.
out << cmd;
//4. type of server request
out << type;
//5. type of server request
out << fileName;
//6. length_payload
quint16 length_payload = 0;
length_payload = (quint16)(block.size() + sizeof(quint16));
out << length_payload;
m_keysServerTcpSocket->write(block);
m_keysServerTcpSocket->disconnectFromHost();
m_keysServerTcpSocket->waitForDisconnected(10000);
}
void ecmqMfgClient::checkDisconnect()
{
m_keysServerTcpSocket->flush();
//m_keysServerTcpSocket->abort();
m_keysServerTcpSocket->close();
qDebug("checkDisconnect() OK!!");
}
/************** CODE USED TO RECEIVE A MESSAGE **************************************************************************/
void ecmqMfgClient::acceptConnectionFromKeysServer()
{
m_tcpKeysFromServerConnection = m_tcpKeysFromServer->nextPendingConnection();
connect(m_tcpKeysFromServerConnection, SIGNAL(readyRead()),
this, SLOT(receiveFromKeysServer()));
connect(m_tcpKeysFromServerConnection,
SIGNAL(error
(QAbstractSocket::SocketError)),
this,
SLOT(displayErrorFromKeyServer
(m_tcpKeysFromServerConnection,
QAbstractSocket::SocketError)));
}
void ecmqMfgClient::receiveFromKeysServer()
{
qint16 bytesAvail;
bytesAvail= m_tcpKeysFromServerConnection->bytesAvailable();
if (bytesAvail > 0)
{
// QDataStream in(this);
//.1
quint16 cmd;
pin >> cmd;
//.2
quint16 type;
pin >> type;
//.3
pin >> keyCode;
//.4
quint16 length_payload;// = 0;
pin >> length_payload;
QString msg
= tr
(">>receiveFromKeysServer<< ") + tr
(" > bytesAvail ") + QString("%5").
arg(bytesAvail
) + tr
(" < ") + tr
(" > -- cmd ") + QString("%5").
arg(cmd
) + tr
(" < ") + tr
(" > -- type ") + QString("%5").
arg(type
) + tr
(" < ") + tr (" > -- keyCode ") + keyCode + tr (" < ")
+ tr
(" > -- length_payload ") + QString("%5").
arg(length_payload
) + tr
(" < ") + tr("\n");
m_tcpKeysFromServerConnection->flush();
serverListenFromKeysServer();
}
void ecmqMfgClient::serverListenFromKeysServer()
{
if (!m_tcpKeysFromServer->isListening() &&
!m_tcpKeysFromServer
->listen
(QHostAddress(m_localIPAddress
), m_Params.
PortFromKeysServer)) {
newLabelSvrName = tr("Unable to start KEYS server: check IP address!");
m_netInfo->setLabelFromKeysServer(newLabelSvrName);
const QString msg
= tr
("Unable to start KEYS server at ") + m_localIPAddress
+ " : " + QString("%5").
arg(m_Params.
PortFromKeysServer) + tr
(" on ") + QDateTime::currentDateTime().
toString(Qt
::ISODate) + tr("\n");
}
else
{
// if tcpServer listen to the provided ipAddr and ipPort, the newConnection
// signal is emitted every time a client tries to connect
newLabelSvrName = tr("Listening from ")
+ m_Params.
IpAddressKeysServer +" @ " + QString("%5").
arg(m_Params.
PortFromKeysServer) + " from KEYS srv";
}
}
main ()
{
/***** TO OTHER PROCESS *******************************************************/
m_keysServerTcpSocket = new(QTcpSocket);
/****** FROM OTHER PROCESS ****************************************************/
m_tcpKeysFromServer = new QTcpServer(this);
connect(m_tcpKeysFromServer, SIGNAL(newConnection()), this, SLOT(acceptConnectionFromKeysServer()));
serverListenFromKeysServer();
}
/************** CODE USED TO TRANSMITT A MESSAGGE **********************************************************************/
// this function send a message to other process
void ecmqMfgClient::sendToKeysServer()
{
m_keysServerTcpSocket->connectToHost(m_Params.IpAddressKeysServer, m_Params.PortToKeysServer);
connect(m_keysServerTcpSocket, SIGNAL(disconnected()),
this, SLOT(checkDisconnect()));
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);
//1.
out << (quint16)0;
//2. port of client
out << m_Params.PortFromKeysServer;
//3.
out << cmd;
//4. type of server request
out << type;
//5. type of server request
out << fileName;
//6. length_payload
quint16 length_payload = 0;
length_payload = (quint16)(block.size() + sizeof(quint16));
out << length_payload;
m_keysServerTcpSocket->write(block);
m_keysServerTcpSocket->disconnectFromHost();
m_keysServerTcpSocket->waitForDisconnected(10000);
}
void ecmqMfgClient::checkDisconnect()
{
m_keysServerTcpSocket->flush();
//m_keysServerTcpSocket->abort();
m_keysServerTcpSocket->close();
qDebug("checkDisconnect() OK!!");
}
/************** CODE USED TO RECEIVE A MESSAGE **************************************************************************/
void ecmqMfgClient::acceptConnectionFromKeysServer()
{
m_tcpKeysFromServerConnection = m_tcpKeysFromServer->nextPendingConnection();
connect(m_tcpKeysFromServerConnection, SIGNAL(readyRead()),
this, SLOT(receiveFromKeysServer()));
connect(m_tcpKeysFromServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayErrorFromKeyServer(m_tcpKeysFromServerConnection, QAbstractSocket::SocketError)));
}
void ecmqMfgClient::receiveFromKeysServer()
{
QString msg;
qint16 bytesAvail;
bytesAvail= m_tcpKeysFromServerConnection->bytesAvailable();
if (bytesAvail > 0)
{
// QDataStream in(this);
QDataStream pin(m_tcpKeysFromServerConnection);
pin.setVersion(QDataStream::Qt_4_1);
//.1
quint16 cmd;
pin >> cmd;
//.2
quint16 type;
pin >> type;
//.3
QString keyCode;
pin >> keyCode;
//.4
quint16 length_payload;// = 0;
pin >> length_payload;
QString msg = tr(">>receiveFromKeysServer<< ")
+ tr (" > bytesAvail ") + QString("%5").arg(bytesAvail) + tr (" < ")
+ tr (" > -- cmd ") + QString("%5").arg(cmd) + tr (" < ")
+ tr (" > -- type ") + QString("%5").arg(type) + tr (" < ")
+ tr (" > -- keyCode ") + keyCode + tr (" < ")
+ tr (" > -- length_payload ") + QString("%5").arg(length_payload) + tr (" < ")
+ tr("\n");
m_tcpKeysFromServerConnection->flush();
serverListenFromKeysServer();
}
void ecmqMfgClient::serverListenFromKeysServer()
{
QString newLabelSvrName;
QPalette palette;
if (!m_tcpKeysFromServer->isListening() &&
!m_tcpKeysFromServer->listen(QHostAddress(m_localIPAddress), m_Params.PortFromKeysServer))
{
newLabelSvrName = tr("Unable to start KEYS server: check IP address!");
m_netInfo->setLabelFromKeysServer(newLabelSvrName);
const QString msg = tr("Unable to start KEYS server at ")
+ m_localIPAddress + " : " + QString("%5").arg(m_Params.PortFromKeysServer)
+ tr(" on ") + QDateTime::currentDateTime().toString(Qt::ISODate)
+ tr("\n");
}
else
{
// if tcpServer listen to the provided ipAddr and ipPort, the newConnection
// signal is emitted every time a client tries to connect
newLabelSvrName = tr("Listening from ")
+ m_Params.IpAddressKeysServer +" @ " + QString("%5").arg(m_Params.PortFromKeysServer) + " from KEYS srv";
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks