PDA

View Full Version : P2P ideas please....



chaosgeorge
9th December 2006, 20:35
Hi there, im developing a simple game and i it should be p2p, so as i know about sockets in Qt i can use for each peer application a QTcpSocket for handling messages from the other peer and use QTcpServer to send messages and to stay connected with the peer, will this work ok? for sending/receiving messages from the peer? the QTcpSocket should just connect with ip and port of the other peer QTcpServer object isn't it?
thanks

wysota
9th December 2006, 23:35
Yes and no :) If you make a tcp server and a tcp client then it's hard to call it p2p. But apart from that what you say is true.

chaosgeorge
10th December 2006, 00:43
uhm well yep i guess its not the best implementation but this works well for a p2p approach doesnt it?
:rolleyes:

wysota
10th December 2006, 09:04
"It works." ;)

In general p2p tends to use UDP rather than TCP as then you can really speak about peers. But there is nothing wrong in using TCP.

chaosgeorge
10th December 2006, 15:21
Indeed i'll be using udp sockets better since i prefer to send a broadcast to players looking for a challenger and i will be sending lots of messages however i dont know how to send and receive requests for playing ignoring my own message, i have this...



void startupDialog::readDatagrams(){
QHostAddress *remoteHost = new QHostAddress;
while( udpSocket->hasPendingDatagrams() ){
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size(), remoteHost);

if( qstrcmp(datagram.data(), "//SearchingPlayers") == 0 && remoteHost->toString() != ??? WTF ??? ){
playersFound++;
}
statusLabel2->setText(QString("PlayersFound :: %1").arg(playersFound));
}
}

And for the sender...


void startupDialog::sendDatagrams(){
statusLabel->setText("Looking for players...");
QByteArray datagram = "//SearchingPlayers";
udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress::Broadcast, 45454);
}

I dont know if this is the best way to find players :p but i want to avoid the message to myself so i dont know what to write instead of "WTF" can someone help? thanks