2 QTcpSockets (1 reads another writes) in 2 applications doesn't work
-
Hi, i wanna send data from program no.1 with socket A to program no.2 with socket B with 2 QTcpSockets:
In first program i have:
@
//in class QTcpSocket *dCtrl
DataAdderItem::DataAdderItem(QWidget *parent) :
QFrame(parent),
ui(new Ui::DataAdderItem)
{
//(...)
connect(ui->addNew, SIGNAL(clicked()), this, SLOT(addNewData()));
//(...)
}
void ProxyAdderItem::addNewData(){QByteArray aa = "aa"; QString str(ui->data->text().toUtf8()); dCtrl->open(QIODevice::ReadWrite); dCtrl->write(str.toAscii()); //dCtrl->write("aa"); qDebug() << str.toAscii() << "aa";// write(ui->address->text().toAscii());
}
@
and it works. It prints me a text on a terminal's window.
In second program i have:
@
//in class - QTcpSocket* dCtrl;NetworkManager::NetworkManager(Manager* c,QObject *parent) :
QObject(parent)
{
//(...)
dCtrl = new QTcpSocket;
connect(dCtrl,SIGNAL(readyRead()),
this, SLOT(addProxy()));
//(...)
//connect(dCtrl, SIGNAL(error(QAbstractSocket::SocketError)),
// this, SLOT(error(QAbstractSocket::SocketError))); // not neccesary
}void ManagerControl::addData(){
//qDebug("sss");
//QDataStream in(dCtrl);
dCtrl->open(QIODevice::ReadWrite);
QByteArray ar(dCtrl->readAll());
QString newData(ar);
//QString newData = dCtrl->readLine();
qDebug() << "A";
qDebug() << newProxy;
int port = 1238;
}@
And it doesn't write anything on output (looks like socket is not ready).
What am i doing wrong? -
Welcome to the forum
Either this part has been excluded or the socket in the NetworkManager does not "connectToHost.":http://doc.qt.nokia.com/4.7/qabstractsocket.html#connectToHost
If you are trying to learn how to handle TcpServer/Client with Qt I would recommend starting with the Fortune "Server":http://doc.qt.nokia.com/4.7/network-fortuneserver.html / "Client":http://doc.qt.nokia.com/4.7/network-fortuneclient.html example code
-
seems like you havent openned a new connection.
u need to connect the sockets first and then they'll transfer data.
as i remember u need to use connecttohost() for this purpose -
I wanna connect two clients.
So i need to use connectToHost or not? -
That is not the way it is supposed to work.
A server waits for a client to connect.
A client connects to the server.
At the end you will have two sockets communicating.You should start with the fortune server / client example. It is very simple and it will show you also the way of establishing the communication. (see my previous post)
You will see also that the term server refers to something less sophisticated as you might initially expect.
-
Yes, you need to use connectToHost, but by just using connectToHost your example will not work.
Once again, for learning how to handle TcpServer/Client you should look at the Fortune "Server":http://doc.qt.nokia.com/4.7/network-fortuneserver.html / "Client":http://doc.qt.nokia.com/4.7/network-fortuneclient.html example code
-
so if I wanna exchange info between two programs then both of them must have server and socket, right?
There is no other way and it's bad, ba, bad to think that two sockets can be connected. -
No, typically one side has a server implementation and the other only a client implementation.
The server application is just opening a port and listening until some client application is trying to connect. If you look into the example it will show you the basic concepts and you will understand.
-
Server and Client.--
-
u can also have one server and 2 clients if u strictly wish to use 2 clients
its really better to first understand the fortune examples very well