I'm trying to write a chat program using Qt. It's half completed but it has some problems.
First of all I get an error when I want to send my written message in lineedit to the client. It's a QString, but the writeDatagram only sends a QByteArray. I've googled it and there are some ways for converting QString to QByteArray, but I'm looking for a better solution.
I think I should use connectToHost(), but read() and write() don't work.
Second and main problem is that I can't get to send and receive messages continuously! Obviously this one hasn't occurred yet but I know there is something wrong with it because I've tested it on Qt console and it didn't work there too.
I'm new to GUI and Socket programming, therefore I've searched a lot before I post this topic.
Update: My first problem solved, but now the UDP packets don't get send and receive let alone working like a chat application.
Update: I found out what was the problem and I solved it. The code needed two QUdpSocket Objects. I also updated the code. It's now fully functional.
If you have other inputs I would love to listen to them, otherwise I've got my answer.
Server:
#include "schat.h"
#include "ui_schat.h"
schat::schat(QWidget *parent) :
QWidget(parent),
ui(new Ui::schat)
{
ui->setupUi(this);
socketServerc=new QUdpSocket(this);
socketServer=new QUdpSocket(this);
socketServer->bind(QHostAddress::LocalHost, 8001);
connect(socketServer,SIGNAL(readyRead()),this,SLOT(readPendingDatagrams()));
}
schat::~schat()
{
delete ui;
}
void schat::on_sendButton_clicked()
{
QString word=ui->lineEdit->text();
ui->textBrowser->append(word);
QByteArray buffer;
buffer=word.toUtf8();
QHostAddress sender;
quint16 senderPort;
socketServerc->writeDatagram(buffer.data(), QHostAddress::LocalHost, 7000 );
}
void schat::readPendingDatagrams()
{
while (socketServer->hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(socketServer->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socketServer->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
ui->textBrowser->append(buffer.data());
}
}
Client:
#include "uchat.h"
#include "ui_uchat.h"
uchat::uchat(QWidget *parent) :
QWidget(parent),
ui(new Ui::uchat)
{
ui->setupUi(this);
clientSocket=new QUdpSocket(this);
clientSocketc=new QUdpSocket(this);
clientSocketc->bind(QHostAddress::LocalHost, 7000);
connect(clientSocketc,SIGNAL(readyRead()),this,SLOT(readPendingDatagrams()));
}
uchat::~uchat()
{
delete ui;
}
void uchat::on_sendButton_clicked()
{
QString word=ui->lineEdit->text();
ui->textBrowser->append(word);
QByteArray buffer;
buffer.resize(clientSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
buffer=word.toUtf8();
clientSocket->writeDatagram(buffer.data(), QHostAddress::LocalHost, 8001 );
}
void uchat::readPendingDatagrams()
{
while (clientSocketc->hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(clientSocketc->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
clientSocketc->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
ui->textBrowser->append(buffer.data());
}
}
Converting the QString to a QByteArray is indeed the thing to do. The reason is that UDP packets carry only a series of bytes -- but a QString does not unambiguously represent a sequence of bytes, it represents a sequence of notional characters (a.k.a. QChars). So in order to place that QString into an array of bytes, you have to decide which binary representation you want to encode it as. For example, if you wanted to encode the string using UTF8 encoding (which I would recommend), you'd use QString's toUtf8() method to get the QByteArray representing the UTF8 encoding, and the receiver would use QString's fromUtf8() method to turn the received bytes back into a QString. There are other encodings also (ascii, latin1, local8Bit) but they may not handle internationalization as well as UTF8 does.
As for your second problem ("I can't get to send and receive messages continuously"), you're going to have to be more explicit and descriptive about what happens vs what you expected to happen. I don't know what "continuously" means in this context.
Related
I've got a server and a client and they're connected by TCP (QTcpSocket and QTcpServer). data is sent using QByteArray.
void Receive::newConnection()
{
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), SLOT(disconnected()));
QByteArray *buffer = new QByteArray();
qint32 *s = new qint32(0);
buffers.insert(socket, buffer);
sizes.insert(socket, s);
qDebug()<<buffer;
}
}
last Line prints the text entered in client in server's console. i want to convert buffer to QString. (or i want to send it to qml file). so when i try :
QString receivedText = QTextCodec::codecForMib(1015)->toUnicode(buffer);
and give me the error :
no matching function for call to 'QTextCodec::toUnicode(QByteArray*&)'
receivedText = QTextCodec::codecForMib(1015)->toUnicode(buffer);
^
when using fromAscii or fromStringC it says it's not a member of QString.
what should i do?
According to the documentation:
QString QTextCodec::toUnicode(const QByteArray &a) const
Converts a from the encoding of this codec to Unicode, and returns the
result in a QString.
From the above, it follows that the reference is needed and not the pointer. In your case you should change it to:
QString receivedText = QTextCodec::codecForMib(1015)->toUnicode(*buffer);
I've been trying to do a Lights GUI with an Arduino Mega 2560 with its Xbee Shield and two Xbee Pro S1, one connected to the Arduino and the other one to the PC. My problem is: however I can send data from Qt to my arduino and read it, i can't do the same in the other way. When trying to send a String as "Confirmado\r\n", it arrives to my Qt label wrong, sometimes I get the full String, other ones I receive half of it.
My arduino code is
char buffer[50];
String trama, dir, com, data;
int indexdir, indexcom, indexdata;
void setup(){
Serial.begin(9600);
}
void loop(){
trama= "Confirmado\r\n";
const char *bf = trama.c_str();
if(Serial.available() > 0)
{
Serial.readBytesUntil('/', buffer,500);
Serial.print(bf);
Serial.flush();
}
}
My Qt QSerialPort config is
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
serial->setPortName("COM3"); //COM-port your Arduino is connected to
serial->open(QIODevice::ReadWrite);
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
}
And I send and read data like this
void MainWindow::serialReceived()
{
QByteArray readData = serial->readAll();
//while (serial->waitForReadyRead(500))
// readData.append(serial->readAll());
ui->label->setText(readData);
}
void MainWindow::writeData(const QByteArray &data)
{
serial->write(data);
serial->flush();
serial->waitForBytesWritten(500);
}
The toogle lines means I've tried both options...
I've noticed, doing Debug, that if I place a breakpoint in ui->label->setText(readData);; if it doesnt arrive well (the full "Confirmado\r\n" string), this breakpoint gets twice in this line, the first one readData equals the second half of the string (i.e "mado\r\n") and the other one it values the rest of the string (i.e "Confir").
I've also tried to set a higher baudrate, 57600, but I cant send or receive any data, though I've set the baudrate in the XCTU app before.
Does anyone know a way to receive the full string from Arduino? Or at leats how to setup properly Arduino's and PC's Xbee to work with higher baudrates?
Thanks for the answers, and sorry for my writing skills...
Try use serial->readLine() instead of serial->readall() you can for example wait in loop after the serial->canReadLine() returns the true then you be sure that the data are you received is a full string.
I am tryig to display data I receive via UART on my Qt application.
I send all my data from my microcontroller at once, but my qt application receives it in multiple parts why?
this is what I get: http://imgur.com/kLXRvU5
in stead of: http://imgur.com/h2yNZjl
So every time I receive data my slot function gets called, hence the "data received". But my data is split in two parts. Why please?
my code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)//, mijnAnimatie(new animatie())
{
serialPort->setPortName("COM13");
serialPort->setBaudRate(QSerialPort::Baud115200);
// serialPort->setDataBits(QSerialPort::Data8);
// serialPort->setParity(QSerialPort::NoParity);
// serialPort->setStopBits(QSerialPort::OneStop);
if (!serialPort->open(QIODevice::ReadWrite))
{
qDebug("some error when opening\n");
}
connect(serialPort, SIGNAL(readyRead()), this, SLOT(updateReceivedData()));
}
void MainWindow::updateReceivedData()
{
qDebug("received data\n");
QString m_readData;
ui->receiveLabel->setText(m_readData);
QByteArray result = serialPort->readAll();
QString command(result); //to convert byte array to string
qDebug()<<result;
ui->receiveLabel->setText(command);
}
Streaming connections, like TCP/IP or serial ports, give you zero guaranties about how the data is cut up into pieces. If you want packets, you have to implement them yourself on top of a streaming connection.
I have problems to find why my short QUdpSocket example is not working. I plan to use only one UDP socket to read and write to an embedded device at 192.168.2.66 on port 2007. The device will reply always on port 2007 to the sender. I tested the device with an UDP terminal software and works as I said. So, I designed a simple class to embed the functions needed to manage the device:
class QUdp : public QObject
{
// Q_OBJECT
public:
explicit QUdp(QObject *parent = 0, const char *szHost = 0, uint16_t wPort = 0);
~QUdp();
bool Open();
int64_t Write(QByteArray &data);
int64_t Write(QString strData);
private:
QString m_strHost;
uint16_t m_wPort;
QUdpSocket *OUdp;
private slots:
void received();
};
I suppose that the problem is in the Open method:
bool QUdp::Open()
{
QHostAddress OHost;
connect(OUdp, &QUdpSocket::readyRead, this, &QUdp::received);
bool zRet = OUdp->bind(QHostAddress::AnyIPv4, m_wPort, QUdpSocket::ShareAddress);
OHost.setAddress(m_strHost);
OUdp->connectToHost(OHost, m_wPort, QIODevice::ReadWrite);
return(zRet);
}
//------------------------------------------------------------------------
I used the Qt 5 syntax for the connect(), m_strHost value is "192.168.2.66" and m_wPort is 2007
my Write method is very simple (the part inside #if 0 was added to see if the socket received any data)
int64_t QUdp::Write(QString strData)
{
QByteArray data(strData.toStdString().c_str(), strData.length());
int64_t iCount = OUdp->write(data);
#if 0
bool zRecved = OUdp->waitForReadyRead(3000);
int64_t iRecvCount = OUdp->bytesAvailable();
#endif
return(iCount);
}
//------------------------------------------------------------------------
and this is my test received() method... I wrote it just to see if the signal-slot works or not:
void QUdp::received()
{
int64_t iRecvCount = OUdp->bytesAvailable();
}
//------------------------------------------------------------------------
I don't understand what is wrong.. I found some posts saying that is not possible read and write using only one UDP socket in Qt (Qt uses BSD sockets so it should be possible) but my example looks as the proposed solutions so I really don't understand what is not working.
You can read and write using just one UDP socket in Qt. I have this running in Qt5 on both windows and Linux, so no worries there :)
To establish Rx direct comms in QUdpSocket you should really use the bind() function, something like this:
// Rx connection: check we are not already bound
if (udpSocket->state() != udpSocket->BoundState)
{
// Rx not in bound state, attempt to bind
udpSocket->bind(address, port);
}
Once this has completed you will be able to check that udpSocket->state() == udpSocket->BoundState is true, then you are successfully "bound" to this ip/port. Now your listening can begin if your connection to readready() is correct. I have not used this connection syntax that you are using, so I can't say much about that, but here is the example of how I connect:
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(rxDataEvent()), Qt::QueuedConnection);
Where "this" is the class which contains my QUdpSocket and udpSocket is a QUdpSocket pointer. Then rxDataEvent is defined below:
void CIpComms::rxDataEvent(void)
{
QByteArray rxData;
QHostAddress sender;
quint16 senderPort;
while (udpSocket->hasPendingDatagrams())
{
// Resize and zero byte buffer so we can make way for the new data.
rxData.fill(0, udpSocket->pendingDatagramSize());
// Read data from the UDP buffer.
udpSocket->readDatagram(rxData.data(),
rxData.size(),
&sender,
&senderPort);
// Emit ipDataReceived Signal
emit ipDataReceived(rxData);
}
}
Here we continually check for datagrams until there are none pending (bit easier then doing the whole "bytesAvailable thing") and stick the data into a QByteArray and emit it off elsewhere (which you obviously don't have to do!).
That is all you need to do for connection. Then to send is very easy, you simply have to call writeDatagram(), well there are other options but this is by far the easier to use:
if (-1 == udpSocket->writeDatagram(txData, address, port))
{
// Data write failed, print out warning
qWarning() << "Unable to write data to " << address.toString() << ":" << port << endl;
return false;
}
I have pretty much cut and pasted this from my working code (with a few edits to keep it short-n-simple so it should give you a starting point. In summary where I believe you are going wrong is that you have not "bound" to the IP address/port and are therefore NOT listening to it and will not receive any readReady() events.
I haven't been able to find a similar issue, so here goes:
I'm sending a QString from a QLocalSocket to a QLocalServer across two applications. The receiving (QLocalServer) application does receive the message, but it seems the encoding is completely wrong.
If I send a QString = "x" from the QLocalSocket (client), I'm getting a foreign (Chinese?) symbol in the QLocalServer. My code is literally copied from the Nokia Developer website
If I printout the message via QDebug, I get "??". If I fire it in a message box, Chinese characters are printed. I've tried re-encoding the received message to UTF-8, Latin1, etc., with no luck.
Code is as follows:
//Client
int main(int argc, char *argv[])
{
QLocalSocket * m_socket = new QLocalSocket();
m_socket->connectToServer("SomeServer");
if(m_socket->waitForConnected(1000))
{
//send a message to the server
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << "x";
out.device()->seek(0);
m_socket->write(block);
m_socket->flush();
QMessageBox box;
box.setText("mesage has been sent");
box.exec();
...
}
//Server - this is within a QMainWindow
void MainWindow::messageReceived()
{
QLocalSocket *clientConnection = m_pServer->nextPendingConnection();
while (clientConnection->bytesAvailable() < (int)sizeof(quint32))
clientConnection->waitForReadyRead();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_7);
if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
QString message;
in >> message;
QMessageBox box;
box.setText(QString(message));
box.exec();
}
Any help is highly appreciated.
The client is serializing a const char* while the server is deserializing a QString. These aren't compatible. The former literally writes the string bytes, the latter first encodes to UTF-16. So, I guess on the server side, the raw string data "fff" is being decoded into a QString as though it were UTF-16 data... perhaps resulting in character U+6666, 晦.
Try changing the client to also serialize a QString, i.e.
// client writes a QString
out << QString::fromLatin1("fff");
// server reads a QString
QString message;
in >> message;