You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Jagwah

Beginner

  • "Jagwah" is male
  • "Jagwah" started this thread

Posts: 3

Location: Belgium

  • Send private message

1

Saturday, May 19th 2012, 2:04pm

Pass a QString to other class doesn't work.

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);

}

2

Saturday, May 19th 2012, 2:34pm

debug this section, pay attention to ipClient1 - you never mention this. When is it updated?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ipClient = socket->peerAddress().toString();

    ipClientt = ipClient;

    if(ipClient1 == "")
    {
        userOne.setIpClient1(ipClientt);
        qDebug() << ipClientt;
    }
    else
    {
        if(!(ipClient1 == ipClientt))
        {
            userTwo.setIpClient2(ipClientt);
            qDebug() << ipClientt;
        }
    }
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Jagwah

Beginner

  • "Jagwah" is male
  • "Jagwah" started this thread

Posts: 3

Location: Belgium

  • Send private message

3

Saturday, May 19th 2012, 2:39pm

hi, first of all thanks for your response.

i just declare ipClient1 in the header file.
So when it's empty the first connection of the TCP server = Client1.

i get no error's btw.

4

Saturday, May 19th 2012, 4:12pm

so debug it. use your debugger.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.