Two-way communication with QTcpSocket
-
First of all I have 0 experience with network programming.
Task: To create a client program that communicates to a device.
- Sends 4 bytes over a local network to set parameters on the device or to request data from the device. byte 0 - command, byte 1 - sensorId, byte 2 and byte 3 - data.
- The client queries the device every two seconds with a "GET" command (byte 0).
- The user can set/(send to) data on the device with a "SET" command (byte 0).
For me to test this scenario, I followed the documentation and the threaded fortune server example and client. I made a server and a client. They are not quite finished yet.
Unfortunately most of the "packets" are not received by the client (I haven't tested setting data on the server yet).My actual question is: what would be a proper architecture and implementation of my scenario?
I am not posting any code because I don't think it would help.
-
Which is your device ? Does it have network support ? Is the TCP Socket EndPoint created on Device ?
In general you can use the socket programming for your task. You need to define the message exchanges between your client application and device application.
-
The device is not yet available. That's why I am trying to make an emulation. I have created a server with QTcpServer (that would be the device) and and the client code. Also this is a study project before I apply my acquired knowledge to the real thing.
I used as a base the Threaded Fortune Server example and the Fortune Client. Since the client queries the server for data every two seconds, it doesn't receive an answer to all the queries.
I cannot find anything similar on this topic made with Qt.
I need to use Tcp Sockets. -
First and foremost
- First you write Client and Server
- Just pass "Hello" between client and server.
- Ensure that data is received by both perspective.
Once you ready with basic communication ready, start implementing the client and server protocol what you wanted.
If you don't find simple program, do let me know. I can send you the very simple program.
-
As i understand your primary goal is client side app? In network applications hard part is server side(usually).
From your description, server side never initiate data sending, right? For your case fortune server should be ok. Try test fortune server and client without changing their code. Does client receive all data from server?For alternative, take a look to Bryan's tutorial and code:
"C++ Qt 65 - QTcpSocket basics":http://voidrealms.com/index.php?r=tutorial/view&id=251
"C++ Qt 66 - QTCPSocket using signals and slots":http://voidrealms.com/index.php?r=tutorial/view&id=252
"C++ Qt 67 - QTCPServer - a basic TCP server application":http://voidrealms.com/index.php?r=tutorial/view&id=253
"C++ Qt 68 - QTcpServer using multiple threads":http://voidrealms.com/index.php?r=tutorial/view&id=254
"C++ Qt 69 - QTcpServer using QThreadPool":http://voidrealms.com/index.php?r=tutorial/view&id=255
"C++ Qt 70 Advanced Asynchronous QTcpServer with QThreadPool":http://voidrealms.com/index.php?r=tutorial/view&id=256 -
Thanx for the videos. I watched these.
I guess I didn't explained myself well enough.
What I currently have is a threaded server modeled after the Threaded Fortune example - it uses TcpServer. And I have a Client. I even have a GUI for both.
The client queries the server every couple of seconds for data - temperatureCurrent or sends a manual request to set data on the server - temperatureTarget.
The problem is: the client does not receive every answer by the server. In fact it misses most of them.
I'll try to use a QThreadPool next to see if the server will work better.Generally the client should look for the server/device, connect to it and start requesting data. What I don't know is if I should open a socket and keep it open all the time (and if that's possible at all) or if I need to open a socket everytime when I need data. I'm completely lost here :)
-
good. What are you asking is understanding about network programming. I request you to write one simple "Hello World" data transfer between client and server. You will have answer for many questions you are asking here. Also questions fall into network programming domain.
-
[quote author="Zingam" date="1403690794"]
The problem is: the client does not receive every answer by the server. In fact it misses most of them.[/quote]
you have two approaches :
1)Blocking [Synchronous]-Client receives data from server until it gets all data.To do so use waitForReadyRead() function//e.g.
@while(vpClientSocket->waitForReadyRead(500))
{
QByteArray bta = vpClientSocket->readAll();
vstrJsonData.append(bta);
}@2)Non Blocking [Asynchronous]-Client receive data as soon as it comes from sever or there is a data to be read.
use readyread[signal] for that.
//e.g;
@
void CMessage::SlotReadyRead()
{QByteArray ba = vpClientSocket->readAll();
vstrJsonData.append(ba);
if(vstrJsonData.contains(""SUCCESS"}"))
{
qDebug() << "Tot : " <<vstrJsonData;
vstrJsonData.clear();
}
}
@[quote author="Zingam" date="1403690794"]
Generally the client should look for the server/device, connect to it and start requesting data. What I don't know is if I should open a socket and keep it open all the time (and if that's possible at all) or if I need to open a socket everytime when I need data. I'm completely lost here :)[/quote]yeah it is possible to open a socket for all the time
yeah it can also be possible to open a socket everytime when I need data.
it's up to you.