PDA

View Full Version : Programming client-server with socket in multi-languages



philiptine
6th September 2007, 03:54
Hi all,
I'm amateur programmer of, but I must developpe an network-application with socket which side-client write by VC++ and side-server write by QT4. I'm tried many ways, and they could realize themselves but the connection is not understanding thoroughly, so they couldn't receive data from other.
Have you some appropriate solutions?
one thing else, I don't know the way to improve this method to Fotune Server can receive data from Fotune Client, Can we make alterations this method? or Must create other. Please help me!

void Server::sendFortune(){
QByteArray block;
char bufff[256];

QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->write(block);
clientConnection->disconnectFromHost();
}


Thanks for your replies>

wysota
6th September 2007, 09:03
Don't use QDataStream. It's a serialization mechanism, not a general purpose binary stream. If you want to use it, you'd have to "teach" the other side how QDataStream encodes its data which would probably be an overkill using plain WinAPI.

philiptine
7th September 2007, 03:50
Thank for your reply, but you can speak more clair. With these examples, we can improve how to maintain the connection and can transfert data between them??

wysota
7th September 2007, 07:35
Use QIODevice API for reading and writing to the socket instead of QDataStream.