September 15, 2011

_ttt _ttt
Lab Rat
3 posts

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:

  1.    
  2. //in class QTcpSocket *dCtrl
  3. DataAdderItem::DataAdderItem(QWidget *parent) :
  4.     QFrame(parent),
  5.     ui(new Ui::DataAdderItem)
  6. {
  7. //(...)
  8.  connect(ui->addNew, SIGNAL(clicked()), this, SLOT(addNewData()));
  9. //(...)
  10. }
  11. void ProxyAdderItem::addNewData(){
  12.  
  13.     QByteArray aa = "aa";
  14.     QString str(ui->data->text().toUtf8());
  15.  
  16.             dCtrl->open(QIODevice::ReadWrite);
  17.             dCtrl->write(str.toAscii());
  18.             //dCtrl->write("aa");
  19.     qDebug() << str.toAscii() << "aa";// write(ui->address->text().toAscii());
  20. }

and it works. It prints me a text on a terminal’s window.
In second program i have:
  1. //in class - QTcpSocket* dCtrl;
  2.  
  3. NetworkManager::NetworkManager(Manager* c,QObject *parent) :
  4.     QObject(parent)
  5. {
  6. //(...)
  7.    dCtrl = new QTcpSocket;
  8.   connect(dCtrl,SIGNAL(readyRead()),
  9.           this, SLOT(addProxy()));
  10. //(...)
  11. //connect(dCtrl, SIGNAL(error(QAbstractSocket::SocketError)),
  12. //                   this, SLOT(error(QAbstractSocket::SocketError))); // not neccesary
  13. }
  14.  
  15. void ManagerControl::addData(){
  16.     //qDebug("sss");
  17.     //QDataStream in(dCtrl);
  18.     dCtrl->open(QIODevice::ReadWrite);
  19.     QByteArray ar(dCtrl->readAll());
  20.     QString newData(ar);
  21.     //QString newData = dCtrl->readLine();
  22.     qDebug() << "A";
  23.     qDebug() << newProxy;
  24.     int port = 1238;
  25. }  

And it doesn’t write anything on output (looks like socket is not ready).
What am i doing wrong?

9 replies

September 15, 2011

koahnig koahnig
Gene Splicer
2904 posts

Welcome to the forum

Either this part has been excluded or the socket in the NetworkManager does not connectToHost. [doc.qt.nokia.com]

If you are trying to learn how to handle TcpServer/Client with Qt I would recommend starting with the Fortune Server [doc.qt.nokia.com] / Client [doc.qt.nokia.com] example code

September 15, 2011

arianoo arianoo
Lab Rat
19 posts

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

September 19, 2011

_ttt _ttt
Lab Rat
3 posts

I wanna connect two clients.
So i need to use connectToHost or not?

September 19, 2011

koahnig koahnig
Gene Splicer
2904 posts

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.

September 19, 2011

koahnig koahnig
Gene Splicer
2904 posts

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 [doc.qt.nokia.com] / Client [doc.qt.nokia.com] example code

September 19, 2011

_ttt _ttt
Lab Rat
3 posts

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.

September 19, 2011

koahnig koahnig
Gene Splicer
2904 posts

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.

September 19, 2011

KA51O KA51O
Hobby Entomologist
478 posts

Server and Client.—

September 19, 2011

arianoo arianoo
Lab Rat
19 posts

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

 
  ‹‹ Qt + 3D Model + Support      Resize QGraphicsSvgItem by dragging corner ››

You must log in to post a reply. Not a member yet? Register here!