Qt endl looks great from linux telnet, wrong from windows telnet - qt

I am building a telnet server app in Qt, and when I connect from a linux telnet client output looks great. For example, sending "A" << endl << "B" << endl << "C" to my console looks like:
A
B
C
Now when I connect from a Windows telnet client I see
A
B
C
obviously Qt's endl is sending only '\n'. Is there a SIMPLE solution to this? If I replace endl with "\r\n" do I mess up linux clients now? Do I have to force a flush too?
Here is actual code I am using to send to my telnet client:
QString block;
QTextStream out(&block, QIODevice::WriteOnly);
out << "Valid commands are: " << endl
<< " help Print this list" << endl
<< " version Print this version" << endl
<< " clientcount Show the number of active telnet clients" << endl
<< " logrotate Rotate the event log file" << endl
<< " shutdown Initiate shutdown secast" << endl
<< " quit Disconnect your telnet session" << endl
<< " stop Shutdown secast" << endl;
tcpSocketPtr->write(block.toUtf8());

You could simply drop the QTextStream and write the QString directly in here:
QString block = QString("Valid commands are: \n")
+ " help Print this list\n"
+ " version Print this version\n"
+ " clientcount Show the number of active telnet clients\n"
+ " logrotate Rotate the event log file\n"
+ " shutdown Initiate shutdown secast\n"
+ " quit Disconnect your telnet session\n"
+ " stop Shutdown secast\n";
tcpSocketPtr->write(block.toUtf8());
Based on your comment though, you seem to use some old DOS client (on Windows!) which expects "\r\n". In that case, I would send "\r\n" for that, but only the usual "\n" for Linux. It is a not so good practice to send carriage return as well on Linux, and not just line feed even though "\r\n" may seem to work on Linux.

Related

Listing Directory Entries with Qt on Remote Windows Server

I am on a Windows Server which is in the same network as the Server with the computer name service.
I got this simple code which tries to list the content
QFileInfoList fiList = QDir("\\service\\Documents").entryInfoList(QDir::Files);
qDebug() << "sizeof filist: " << fiList.size();
for (const QFileInfo& fi : fiList)
{
qDebug() << fi.absoluteFilePath();
}
The output is the following:
sizeof filist: 0
I make sure that the folder is shared on the network by checking the properties and also using the windows explorer. I can access the folder via Windows Explorer.
Is the functionality I am trying to achieve not possible with QDir?
It turns out there are 2 backslashes more needed because \ needs to be escaped.
So the right code would be:
QFileInfoList fiList = QDir("\\\\service\\Documents").entryInfoList(QDir::Files);
qDebug() << "sizeof filist: " << fiList.size();
for (const QFileInfo& fi : fiList)
{
qDebug() << fi.absoluteFilePath();
}

How to receive a message through TCPBasicClientApp in omnet++?

I have made two different TCP Applications in Omnet++, one is TCPBasicClientApp and other is TCPGenericServerApp. TCP client application is successfully sending a GenericAppMsg through TCP Protocol. Once the message is received on the Server Side (with a specific replyLength) it is sending it back to the client side through SendBack() method (also mentioned in the inet example application).
My question is, how to receive this message back on the client side?
Here is the omnet.ini file code, for this transfer,
The Client Side,
**.host[0].numTcpApps = 1
**.host[0].tcpApp[0].typename = "ReputationAlgorithmApplication"
**.host[0].tcpApp[0].localAddress = ""
**.host[0].tcpApp[0].localPort = -1
**.host[0].tcpApp[0].connectAddress = "host[3]"
**.host[0].tcpApp[0].connectPort = 2000
**.host[0].tcpApp[0].dataTransferMode = "object"
The Server Side,
**.host[3].numTcpApps = 1
**.host[3].tcpApp[*].typename = "ReputationServerApplication"
**.host[3].tcpApp[*].localAddress = "host[3]"
**.host[3].tcpApp[*].localPort = 2000
Here is the sendBack method on Server Side,
void ReputationServerApplication::sendBack(cMessage *msg) {
cPacket *packet = dynamic_cast<cPacket *>(msg);
if (packet) {
msgsSent++;
bytesSent += packet->getByteLength();
emit(sentPkSignal, packet);
EV_INFO << "sending \"" << packet->getName() << "\" to TCP, "
<< packet->getByteLength() << " bytes\n";
} else {
EV_INFO << "sending \"" << msg->getName() << "\" to TCP\n";
}
DummyMessageForReputation *msgDum =
dynamic_cast<DummyMessageForReputation *>(msg);
std::cout << "\n Tested: Message with the string "
<< msgDum->getMessageString() << " is sending back to "
<< msgDum->getNodeName();
send(msgDum, "tcpOut");
}
Any help would be appreciated.
You can use TCPBasicClientApp::socketDataArrived() to process the received message on the client side.

Qt: QUrl::fromUserInput intepretes FTP Url wrong

I have some issue in passing a FTP string into QUrl:
std::cout << QUrl::fromUserInput("ftp://user#host.com:password#ftphost:21/path/file.ext").toString().toStdString().c_str() << std::endl;
Is always resulting in
http://ftp//user#host.com:password#ftphost:21/path/file.ext
which is obviously wrong. What's the problem with the above FTP Url? Or is that a known issue within Qt4?
I am working on Linux, using Qt 4.8.1.
Even following code
if(QUrl("ftp://user#host.com:password#ftphost:21/path/file.ext").isValid())
std::cout << "is valid" << std::endl;
else
std::cout << "is not valid" << std::endl;
Is resulting in "is not valid"
Thanks in advance
You need manually replace # in username with %40. That's what QUrl does internally if QUrl::setUserName() is called with user#domain.tld.

Print the content of a memory address typed by user in C++

How can I print the address typed by the user? This way don't work.
This is the code. Thanks.
#include <iostream>
using namespace std;
int main()
{
int num = 123456;
int *addr = &num;
cout << "Var: num, address: " << &num << ", has: " << num << endl
<< "Var: *addr, address: " << &addr << ", has: " << addr << endl
<< "Printing value of num using the pointer *addr: " << *addr << endl;
int addr_user;
cout << "Type the memory address: "; cin >> addr_user;
int *p_addr_user = (int *)addr_user;
cout << "The address given (" << addr_user << ") has: " << *p_addr_user << endl;
return( 0 );
}
Sorry, I was not so clear:
What the program must to do:
ask to type an integer, print the memory address from those integer, ask to type the memory address printed above, print the content of that memory address and confirm if that address has the number typed in step one.
All in one runtime. Thank you in advance.
I tried this out in Linux:
g++ q.cpp
q.cpp: In function ‘int main()’:
q.cpp:17:31: warning: cast to pointer from integer of different size [-Wint-to-pointer- cast]
./a.out
Var: num, address: 0x7fff562d2828, has: 123456
Var: *addr, address: 0x7fff562d2818, has: 0x7fff562d2828
Printing value of num using the pointer *addr: 123456
Type the memory address: 0x7fff562d2828
Segmentation fault (core dumped)
So I notice a couple of issues:
I Naturally want to try to put in the address of num, but it is displayed in hex
Seg fault
To input in hex I change the input line to:
cout << "Type the memory address: "; cin >> hex >> addr_user;
(otherwise it is interpreted as 0)
But it is still segfaulting.
Here's the problem:
int *p_addr_user = (int*)addr_user;
Oh, there was a warning about it above. Sometime about different sizes (also note that pointers are unsigned.)
ints and pointers can be different sizes (it depends on your platform) For me int is 32 bit and pointers are 64 bit.
Here's how I got it working:
#include <stdint.h>
#...
uintptr_t addr_user;
cout << "Type the memory address: "; cin >> hex >> addr_user;
uintptr_t *p_addr_user =(uintptr_t*) addr_user;

How can I direct the output of a QProcess to a file?

I want to have the output of qconf redirected to test_settings.txt in my tmp folder. I've thought of two possibilities:
QProcess procWriteProject;
procWriteProject.start("qconf", QStringList() << " -sprj "
<< projectList[0] << " >> " << "\"/tmp/testing.txt\"");
procWriteProject.start("qconf -sprj " + projectList[0] + " >> "
+ "/tmp/test_settings");
Will either of those work? Is there a better way?
QProcess procWriteProject;
procWriteProject.setStandardOutputFile("/tmp/test_settings.txt");
procWriteProject.start("qconf", QStringList() << "-sprj" << projectList[0]);

Resources