PDA

View Full Version : Simple Networking



winston2020
28th October 2008, 15:54
Hi, I'm trying to write a very simple MSN client using a few articles I've found which describe the MSN protocol. So far it doesn't work in the slightest, but I don't know if I'm doing the networking properly or not, so it's hard to troubleshoot.

Could someone tell me the simplest way to just connect to the host, send a string ("VER 0 MSNP9 MSNP8 MSNP7"), and then wait for and output a response from the server?

winston2020
28th October 2008, 18:18
Here's a sample of what I've tried, but this doesn't work it just loops forever.


// Just for a simple output window
QTextBrowser *text = new QTextBrowser();
text->show();

QTcpSocket *client = new QTcpSocket();
client->connectToHost( tr("messenger.hotmail.com"), 1863 );
client->write( "VER 0 MSNP8 MSNP7 MSNP6 MSNP5", strlen("VER 0 MSNP8 MSNP7 MSNP6 MSNP5") );
while( client->bytesToWrite() > 0 )
{
qApp->processEvents();
text->append( tr("Bytes Left: %1").arg(client->bytesToWrite()) );
}

caduel
28th October 2008, 19:51
I think you need to wait for a connection to be established before starting to send data.
(either by attaching to signals or by using QAbstractSocket::waitForConnected())



QTcpSocket *client = new QTcpSocket();
client->connectToHost( tr("messenger.hotmail.com"), 1863 );
if (!client->waitForConnected()) // not good if in GUI thread, use signals then
{
qDebug() << "connection failed";
return false;
}
client->write( "VER 0 MSNP8 MSNP7 MSNP6 MSNP5", strlen("VER 0 MSNP8 MSNP7 MSNP6 MSNP5") );

HTH

PS: I doubt it is necessary to tr() wrap the server url.
PPS: Note you can use a QTextStream for writing to that socket:

QTextStream out(client);
out << "VER 0 MSNP8 MSNP7 MSNP6 MSNP5"

winston2020
28th October 2008, 19:58
Thank you. That works now, but I still do not understand how to read from the socket.

caduel
28th October 2008, 20:18
Basically the same:
Wait for the connection to be established and data has arrived with QIODevice::waitForReadRead(), (or connect to QIODevice::readyRead()).

Then read. You may use a QTextStream, or e.g. readLine().
(You have to take care that data might arrive in packages: you might have to append several packages to get a line you sent.)

winston2020
28th October 2008, 20:30
Ok, waitForReadyRead() returns false every time. Does that mean the server is simply not sending a reply?

caduel
28th October 2008, 20:36
If you are connected and have waited long enough: yes.

You can check with a tool like ethereal.
For simple text based protocols you can also just telnet the server and paste your "command" there.

winston2020
28th October 2008, 20:45
For simple text based protocols you can also just telnet the server and paste your "command" there.

How do I do that? :o

caduel
28th October 2008, 21:11
(linux)
telnet server port

e.g.


telnet messenger.hotmail.com 1863

then type what you would send (or just paste it)
The server will reply...

(telnet is available for windows, too. you have to check if it has to be called differently.)

HTH

winston2020
28th October 2008, 21:25
(linux)
telnet server port

e.g.


telnet messenger.hotmail.com 1863

then type what you would send (or just paste it)
The server will reply...

(telnet is available for windows, too. you have to check if it has to be called differently.)

HTH

It has the same syntax, but I am unable to connect. I'm going to assume for now that the server has changed since the article was written, and I'll look into it a bit more. Thank you for all your help :D