[Solved] Can't get peer Adress from QTcpSocket
-
Hello,
I recently started programming with Qt and am now working on a asynchronous QTcpServer. Now what I am trying to do is create a new File for every unique client that connects to my Server with this Code:@socket = new QTcpSocket(this);
socket->setSocketDescriptor(handle); qDebug()<<socket->localAddress().toIPv4Address(); qDebug()<<socket->localPort(); MyClient::FileName = MainWindow::FileName.append("/").append(socket->localAddress().toIPv4Address()).append(".txt"); qDebug()<<MyClient::FileName; connect(socket, SIGNAL(connected()), this, SLOT(connected())); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); qDebug()<<"Client connected";@
But the Filename is either empty or I get "3232243626" as output from "qDebug()<<socket->localAddress().toIPv4Address();" what leads into having a File with the name "ᾪ".
I hope somebody can tell me what I am doing wrong.
Thanks in advance. -
Probably you should use "QTcpServer":http://qt-project.org/doc/qt-4.8/qtcpserver.html to listen incoming connections and then obtain socket descriptor via "nextPendingConnection":http://qt-project.org/doc/qt-4.8/qtcpserver.html#nextPendingConnection
Have a look at "Fortune Server Example":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html -
Ok I dont really know what you mean but I think I am already doing that. I will better post my complete Code:
Server.cpp:
@void MyServer::StartServer ()
{
if(listen(QHostAddress::Any, 45454))
{
qDebug()<<"Server is listening";
}
else
{
qDebug()<<"Server failed to start!";
}
}void MyServer::incomingConnection (qintptr handle)
{
client = new MyClient(this);
client->CreatSocket(handle);
}@
Client.cpp:
@void MyClient::CreatSocket(qintptr handle)
{
socket = new QTcpSocket(this);socket->setSocketDescriptor(handle); qDebug()<<socket->localAddress().toIPv4Address(); qDebug()<<socket->localPort(); MyClient::FileName = MainWindow::FileName.append("/").append(socket->localAddress().toIPv4Address()).append(".txt"); qDebug()<<MyClient::FileName; connect(socket, SIGNAL(connected()), this, SLOT(connected())); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); qDebug()<<"Client connected";
}@
The Server logic runs fine: I can connect, I can exchange data etc. the only thing that blows my mind is, why I cant get the correct Ip of the connected socket.
PS: Thank you for your fast response. -
OK, I see, you mix bsd socket API with Qt API. A bit offtopic, but I suggest you to start using "QTcpServer::listen":http://qt-project.org/doc/qt-4.8/qtcpserver.html#listen instead of just calling listen() next time.
If I understand, you want to obtain remote peer address. Have you tried calling "peerAddress":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#peerAddress and "peerPort":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#peerPort ?
-
Hey beemaster, thank you for helping me!
So I changed "if(listen(QHostAddress::Any, 45454))" to "if(this->listen(QHostAddress::Any, 45454))". Is that better?So I used peerAddress and peerPort instead and my output actually changed. I get "3232243573" for peerAddress now what leads into having a file named "ή". By the way, I discovered that if I connect from localhost the peerAddress is empty and if I connect from any maschine to the server they all get the same number as peer address. I dont know if this matters but the server is runnng on opensuse 13.1 and the clients I tested with are Windows 7 maschines.