Hello,
I'm having a problem with passing a QString to another class..
I'm making a multiplayer game (ticTacToe) where i have to use UDP and TCP socket/server.
To send a commando to a client with UDP i need the IP of the client, so in the TCP server i did this:
ipClient = socket->peerAddress().toString();
This gives me the Ip of the client.
Now i have to pass this Ip to my udpUser1 class :
udpUser1 userOne;
userOne.setIpClient1(ipClient);
in the class udpUser1 i make a private variable:
QString ipUser1;
in the function void udpUser1::setIpClient1(QString Ip)
ipUser1 = Ip;
to send a commando to the client, i use as QHostAddress:
QHostAddress hostIp(ipUser1);
problem -> ipUser1 is in the function to send data to the client always empty...
Can someone please help me...
i don't see what i'm doing wrong.
btw:
I'm new to this forum, so if i'm doing something wrong please tell me

.
server.cpp
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
void server::incomingConnection(int handle)
{
udpUser1 userOne;
udpUser2 userTwo;
clientServer *socket = new clientServer(this);
socket->setSocketDescriptor(handle);
qDebug() << "connected to: "<< socket->peerAddress().toString() << " @ "<< socket->peerPort();
ipClient = socket->peerAddress().toString();
ipClientt = ipClient;
if(ipClient1 == "")
{
userOne.setIpClient1(ipClientt);
qDebug() << ipClientt;
}
else
{
if(!(ipClient1 == ipClientt))
{
userTwo.setIpClient2(ipClientt);
qDebug() << ipClientt;
}
}
}
|
udpUser1.cpp
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
udpUser1::udpUser1(QObject *parent) :
QUdpSocket(parent)
{
this->bind(5821);
connect(this,SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}
void udpUser1::setIpClient1(QString Ip)
{
ipUser1 = Ip;
qDebug() << ipUser1;
}
void udpUser1::processPendingDatagrams()
{
qDebug() << "udp1 reading ";
QByteArray datagram;
do
{
datagram.resize(this->pendingDatagramSize());
this->readDatagram(datagram.data(), datagram.size());
}
while(this->hasPendingDatagrams());
QDataStream in(&datagram, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_3);
in >> incommingData;
logic logi;
logi.incommingDataFunctionUser1(incommingData);
}
void udpUser1::sendCommandoToClient1(QString commando)
{
QByteArray datagram;
QDataStream out(&datagram, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
out << commando;
qDebug() << " send " << commando << "commando to : " << ipUser1;
QHostAddress hostIp(ipUser1);
this->writeDatagram(datagram, hostIp, 5821);
}
|