Unsolved QT app:exec is blocking the code.
-
Now I corrected the mistake the address given in sin_addr was wrong. I mentioned it now as
127.0.0.1with this I am able to receive message from UdpSocket from QT application to normal socket in non-qt application but when I am trying to send message from my non-qt application to QT application signal readyRead not emitted and not able to read the data.
-
@Ayush-Gupta
Then you are doing something wrong in your receiving side, or possibly at the sender side. What else do you want us to say? Simple sample code is given in https://doc.qt.io/qt-5/qudpsocket.html. You have bound to the port for receiving, haven't you? -
@JonB I am pasting my code here
UdpSocket.h
#ifndef UDPSOCKET_H #define UDPSOCKET_H #include <QUdpSocket> #include <MMISimForm.h> class UdpSocket : public QObject { Q_OBJECT public: UdpSocket(QObject* parent, int portno, MMISimForm* simForm_); ~UdpSocket(); private: int portNo_; MMISimForm* mmiSimForm_; QUdpSocket *socketUdp; public slots: void dataRead(); //methods public: int sendData(); }; #endif // UDPSOCKET_H
UdpSocket.cpp
#include "UdpSocket.h" UdpSocket::UdpSocket(QObject *parent, int portno, MMISimForm* simForm_) :QObject(parent), portNo_(portno), mmiSimForm_(simForm_) { socketUdp = new QUdpSocket(this); socketUdp->bind(QHostAddress::LocalHost, portNo_); connect(socketUdp, SIGNAL(readyRead()), this, SLOT(dataRead())); } UdpSocket::~UdpSocket() { } void UdpSocket::dataRead() { QByteArray buffer; QHostAddress sender; quint16 senderPort; buffer.resize(socketUdp->pendingDatagramSize()); int test = socketUdp->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); QString text; text.sprintf("Bytes Read %d", test); mmiSimForm_->logMessage(buffer); } int UdpSocket::sendData() { return socketUdp->writeDatagram("GetStream", QHostAddress::LocalHost, 5000); }
My non - qt application code
ServerSocket.cpp
#include "ServerSocket.h" #include "mmisimlogger.h" ServerSocket::ServerSocket() { WSAStartup(0x0101, &w) != 0; /* Open a datagram socket */ sd = socket(AF_INET, SOCK_DGRAM, 0); /* Clear out server struct */ memset((void *)&server, '\0', sizeof(struct sockaddr_in)); memset((void *)&client, '\0', sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_port = htons(5000); /* Get host name of this computer */ gethostname(host_name, sizeof(host_name)); hp = gethostbyname(host_name); /*Assign the address server.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0]; server.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1]; server.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2]; server.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];*/ server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)); } int ServerSocket::recieveData() { client_length = (int)sizeof(struct sockaddr_in); int test = 0; //Receive bytes from client bytes_received = recvfrom(sd, buffer, 4096, 0, (struct sockaddr *)&client, &client_length); bytes_received = sendto(sd, (const char *)"hello", strlen("hello"), 0, (const struct sockaddr *) &client, client_length); char teststring[40]; sprintf(teststring,"ErrorCode %d", bytes_received); MMISimLogger::instance()->log(teststring); MMISimLogger::instance()->close(); if (bytes_received < 0) { } else { test = bytes_received; //MMISimLogger::instance()->log(buffer); //MMISimLogger::instance()->close(); return test; } }
I know this code is mess but I don't know where I am doing wrong.
[Fixed code tags ~kshegunov]
-
It seems that readyRead signal will be not be emitted when we send data from other non-QT application.
I am able to connect to the server (QT application side) when I send data using send() in non-QT application it returns number of byte send correctly but no readyRead signal is emitted.
-
@Ayush-Gupta said in QT app:exec is blocking the code.:
It seems that readyRead signal will be not be emitted when we send data from other non-QT application.
No, there is no differentiation between Qt and non Qt UDP-Source. I'm able to receive and send UDP datagrams just fine that way.
I am able to connect to the server (QT application side) when I send data using send() in non-QT application it returns number of byte send correctly but no readyRead signal is emitted.
Pls make sure you actually really send data. A 3rd Party listening software could help here.
-
@Ayush-Gupta said in QT app:exec is blocking the code.:
It seems that readyRead signal will be not be emitted when we send data from other non-QT application.
As said multiple times, there is no difference in socket operation between a Qt program versus a non-Qt one, nor between blocking or non-blocking. TCP is TCP, UDP is UDP, regardless of who the sender/receiver is.
Concentrate instead on discovering what you are doing differently/wrong in either side of the communication.
-
@J-Hilk Is it possible for you to share the example code?
-
void MyTcpServer::incomingConnection(qintptr socketDescriptor) { socket = new QTcpSocket(); ``` // set the ID if(!socket->setSocketDescriptor(socketDescriptor)) { //OnErrorSocket(socket->error()); return; } else { simForm_->logMessage("Server Started"); socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); //OnConnected(); } // connect socket and signal // note - Qt::DirectConnection is used because it's multithreaded // This makes the slot to be invoked immediately, when the signal is emitted. connect(socket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()), Qt::DirectConnection); connect(socket, SIGNAL(disconnected()), this, SLOT(OnDisconnected())); }
incomingConnection call is success means connection is successful. But when sending data using below function. ready Read is not emitting. int ServerSocket::sendData() { send(sd , "hello" , strlen("hello") , 0 ); } ClientSocket::ClientSocket() { WSAStartup(0x0101, &w) != 0; /* Open a datagram socket */ sd = socket(AF_INET, SOCK_STREAM, 0); /* Clear out server struct */ memset((void *)&server, '\0', sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_port = htons(5000); server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); connect(sd, (struct sockaddr *)&server, sizeof(server)); }
-
-
@Ayush-Gupta said in QT app:exec is blocking the code.:
@J-Hilk Is it possible for you to share the example code?
maybe I would have to cut out as significant part, to be able to show it
2nd post
Here you're using a TcpSocket, previously you posted Udp. What is it, do you use both at the same time?
are you sure you pass the correct port when creating the class. I noticed, your write function has hardcoded port value while your bind uses a value defined at run time
-
@J-Hilk Yes I am using same port number. I failed using UDP then I am trying using UDP.
Port number is used correctly that is why connection was established and incomingConnection() was called.
-
@J-Hilk Is it possible for you to post some working code?