3

I have two projects, one for a server and one for a client. On the client, when I want to send data, I create a QTcpSocket member variable, and then send data using the write() method.

On the server, I receive the information, but I want to send information back to the client. Specifically I'm trying to validate a user, so I'm sending the user name and password that they log in with on the client to the server, and I want to check that data against the database saved on the server, and then send information back to the client to verify that the information was correct.

My problem is that I don't know how to send information from the server to the client. On the server, I have two member variables,QTcpServer server; and QTcpSocket* client;.

I've looked through documentation on QTcpServer and it has no equivalent to write() as far as I'm aware, and the QTcpSocket* is the client that's currently connected to the server.

I was thinking about creating a QTcpSocket variable on the server and sending information to the client the same way I send information to the sever, but that seems wrong because then the client would be behaving like a server, complete with a QTcpServer variable on the client.

How else would I go about doing this?

Nejat
  • 31,784
  • 12
  • 106
  • 138
ozzymado
  • 960
  • 3
  • 15
  • 26

1 Answers1

3

You should use QTcpServer to accept incoming TCP connections. First call listen() to wait for a connection :

QTcpServer* tcpServer = new QTcpServer();

connect(tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));

if( !tcpServer->listen( QHostAddress::Any, port_no ) )
    qDebug() << "Couldn't listen to port";

This signal newConnection is emitted every time a new connection is available. So we connect the signal to a slot to get the server QTcpSocket object to communicate with the client :

void MyClass::onNewConnection()
{
    serverSocket = tcpServer->nextPendingConnection();

    connect(serverSocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
}

You can now send data to the client socket, using the write method :

serverSocket->write("Hello!");

And read data when new data is arrived :

void MyClass::readSocket()
{
    QByteArray data = serverSocket->readAll();
    ...
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Once I write data using `serverSocket->write("Hello!");` how do I read that on that client? With the `readSocket` function that I include in my client program? I can already read data sent from the client to the server, so I assume that's where you want me to put the `readSocket` function. – ozzymado Jul 17 '14 at 18:09
  • 1
    Reading data on the client side is similar. Just connect the readyRead signal of the client socket to some slot. When new data is arrived the signal is emitted, the slot gets called and you can read data there. – Nejat Jul 17 '14 at 19:04
  • I've added two variables to my Client program, QTcpSocket* incomingFromServer; to handle data coming from the server and QTcpServer* server; to listen for an incoming connection from the server. I have a greeting function to send a message to the server every time a user opens the client program, and to test this I want to get a message back from the server. So after I call write on the client, I call connect, and then listen for a response on that port, just like I do on the server when I wait for a response from the client. Is this correct so far? – ozzymado Jul 17 '14 at 19:49
  • 1
    This is not correct. You should only have a `QTcpSocket` on the client side and only connect to the ip of the server at the port server is listening. `QTcpServer` is just for the server side. The server listens on a specific port and the client connects to it. So on the client side just connect to the server port and ip. – Nejat Jul 17 '14 at 19:56
  • So how do I get the client to wait for a response from the server? Do I put `connect(&client, SIGNAL(readyRead()), this, SLOT(someSlot()));` in the constructor for the Client object? I don't need to `listen` at all? – ozzymado Jul 17 '14 at 20:00
  • It seems that's the answer. I just did it and it's working great. Thank you very much! – ozzymado Jul 17 '14 at 20:06