PDA

View Full Version : simple socket client example



Tomasz
26th July 2010, 10:15
Hello!

I've wrote server and client using sockets.h in C. Now I'm trying to write client in QT4. I can't find any working example. My server program sends always 20 bytes if we first send him one byte which is 'o', and I just want to read It. Anyone can help? Maybe some sample code?

thanks in advance
best regards
Tomasz

aamer4yu
26th July 2010, 10:51
Did you go through the Qt demos ? You can find examples for Networking there.

Tomasz
26th July 2010, 11:30
Yes. I've found some examples at http://doc.qt.nokia.com/4.0/qt4-network.html#tcp-client. But It isn't what I'm looking for so I've read QTcpSocket class reference and tried to write something on my own:



char bufor[20];
tcpSocket = new QTcpSocket(this);

tcpSocket->abort();
tcpSocket->connectToHost("localhost",57717);

bufor[0] = 'o';
tcpSocket->write(bufor,1);
tcpSocket->read(bufor,19);


but when I read I've got what I've just wrote ('o'). Tutorial in link above isn't clear for me. Can someone tell me what should I change in my code to make it work?

thanks in advance
best regards
Tomasz

squidge
26th July 2010, 13:04
Ok: Use signals and slots. Wait until the connection is established before writing. Wait until there is something to read before reading.

aamer4yu
26th July 2010, 13:15
or may be use QAbstractSocket::waitForReadyRead