jonging
20th June 2006, 20:13
Hi. I'm writing a few small test socket programs using Qt libraries without using the event loop (i.e. no Signals and Slots). Here is the source code for a small server:
#include <QtNetwork>
#include <iostream>
#include <qdatastream.h>
int main(int *argc, char *argv[]){
QTcpServer server;
QTcpSocket *socket;
server.listen(QHostAddress::LocalHost,8000);
bool timeout;
server.waitForNewConnection(50000,&timeout);
socket=server.nextPendingConnection();
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);
int number;
in>>number;
std::cout<<number<<std::endl;
}
and a simple client:
#include <QtNetwork>
#include <iostream>
#include <qdatastream.h>
int main(int *argc, char *argv[]){
QTcpSocket socket;
socket.connectToHost(QHostAddress::LocalHost,8000) ;
if (!socket.waitForConnected(5000)) {
std::cout<<"Cannot connect to GUI."<<std::endl;
return 0;
}
QDataStream out(&socket);
out.setVersion(QDataStream::Qt_4_0);
out<<1;
}
when I run the server (receiving data) and then the client, the server displays the message "QDataStream: No device" followed by a zero. What am I doing wrong? thanks.
Edit: I forgot to note that the client displays the error message "Cannot connect to GUI"
#include <QtNetwork>
#include <iostream>
#include <qdatastream.h>
int main(int *argc, char *argv[]){
QTcpServer server;
QTcpSocket *socket;
server.listen(QHostAddress::LocalHost,8000);
bool timeout;
server.waitForNewConnection(50000,&timeout);
socket=server.nextPendingConnection();
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);
int number;
in>>number;
std::cout<<number<<std::endl;
}
and a simple client:
#include <QtNetwork>
#include <iostream>
#include <qdatastream.h>
int main(int *argc, char *argv[]){
QTcpSocket socket;
socket.connectToHost(QHostAddress::LocalHost,8000) ;
if (!socket.waitForConnected(5000)) {
std::cout<<"Cannot connect to GUI."<<std::endl;
return 0;
}
QDataStream out(&socket);
out.setVersion(QDataStream::Qt_4_0);
out<<1;
}
when I run the server (receiving data) and then the client, the server displays the message "QDataStream: No device" followed by a zero. What am I doing wrong? thanks.
Edit: I forgot to note that the client displays the error message "Cannot connect to GUI"