I realize this is not the best way to structure the code. I should use returns instead of nesting the if's more. I've been in kind of a hurry.
client side:
out << ui.command_lineEdit->text();
cout << "sending manual command: " << ui.command_lineEdit->text().toAscii().data() << endl;
bool result = udpSocket.waitForConnected( 3000 );
if ( result != false ) {
qint64 writtenSize = udpSocket.write( datagram ); // send command
if ( writtenSize != -1 ) {
result = udpSocket.waitForReadyRead( 3000 );
if ( result != false ) {
cout << "found a response." << endl;
while ( udpSocket.hasPendingDatagrams() ) {
QByteArray datagram
(command_socket_device.
bytesAvailable(),
'0' );
command_socket_device.readDatagram(datagram.data(), datagram.size());
in >> the_return;
this->statusBar()->showMessage( the_return );
}
} else {
cout << "No response." << endl;
}
}
}
QUdpSocket udpSocket;
QByteArray datagram;
QDataStream out( &datagram, QIODevice::ReadWrite );
out.setVersion( QDataStream::Qt_4_5 );
out << ui.command_lineEdit->text();
udpSocket.connectToHost( QHostAddress::LocalHost, 5825 );
cout << "sending manual command: " << ui.command_lineEdit->text().toAscii().data() << endl;
bool result = udpSocket.waitForConnected( 3000 );
if ( result != false ) {
qint64 writtenSize = udpSocket.write( datagram ); // send command
if ( writtenSize != -1 ) {
result = udpSocket.waitForReadyRead( 3000 );
if ( result != false ) {
cout << "found a response." << endl;
while ( udpSocket.hasPendingDatagrams() ) {
QByteArray datagram(command_socket_device.bytesAvailable(), '0' );
command_socket_device.readDatagram(datagram.data(), datagram.size());
QString the_return;
QDataStream in( &datagram, QIODevice::ReadWrite);
in.setVersion( QDataStream::Qt_4_5 );
in >> the_return;
this->statusBar()->showMessage( the_return );
}
} else {
cout << "No response." << endl;
}
}
}
To copy to clipboard, switch view to plain text mode
server side:
(called in the constructuor after a 1 sec delay)
while ( !done ) {
if ( command_socket_device.hasPendingDatagrams() == true ) {
cout << "found something to read." << endl;
QByteArray datagram
(command_socket_device.
bytesAvailable(),
'0' );
command_socket_device.read(datagram.data(), datagram.size());
in >> command;
cout << "Received: " << command.toAscii().data() << endl;
ui.log_textEdit->append( command );
if ( command == "HADRON_STARTUP" ) {
command.prepend( "0 0 0 " );
out << command;
cout << "sending response: " << command.toAscii().data() << endl;
command_socket_device.write( datagram2 );
ui.log_textEdit->append( command );
}
cout << "Should have logged something." << endl;
}
qApp->processEvents();
}
while ( !done ) {
if ( command_socket_device.hasPendingDatagrams() == true ) {
cout << "found something to read." << endl;
QString command( "" );
QByteArray datagram(command_socket_device.bytesAvailable(), '0' );
command_socket_device.read(datagram.data(), datagram.size());
QDataStream in( &datagram, QIODevice::ReadWrite);
in.setVersion( QDataStream::Qt_4_5 );
in >> command;
cout << "Received: " << command.toAscii().data() << endl;
ui.log_textEdit->append( command );
if ( command == "HADRON_STARTUP" ) {
command.prepend( "0 0 0 " );
QByteArray datagram2;
QDataStream out( &datagram2, QIODevice::ReadWrite );
out.setVersion( QDataStream::Qt_4_5 );
out << command;
cout << "sending response: " << command.toAscii().data() << endl;
command_socket_device.write( datagram2 );
ui.log_textEdit->append( command );
}
cout << "Should have logged something." << endl;
}
qApp->processEvents();
}
To copy to clipboard, switch view to plain text mode
I've tried a lot of combinations binding and not binding to the port.
Also, this is the blocking version I tried. I've also tried a version based on the more normal "readyRead".
Bookmarks