Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
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) :
connect(ui->addNew, SIGNAL(clicked()), this, SLOT(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) :
dCtrl = new QTcpSocket;
// this, SLOT(error(QAbstractSocket::SocketError))); // not neccesary
//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
arianoo last edited by
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.
KA51O last edited by
Server and Client.--
arianoo last edited by
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