QUdpSocket
에 문제가 있습니다. UDP protokol을 사용하여 데이터를주고받는 간단한 프로그램을 만들고 싶습니다. 이미 많은 비슷한 주제를 읽었지만 해결되지 않은 것으로 나타났습니다. 통신은 QHostAdress::LocalHost
에 대해서만 작동합니다. 그런 다음 send와 동일한 데이터를 제공합니다. 그러나 예를 들어 194.181.161.134
과 같이 구체적인 주소 외부로 데이터를 보내려면 작동하지 않습니다. 데이터가 전송된다는 의미지만받을 수는 없습니다. 이건 내 코드입니다 :QUdpSocket : 프로그램을 보내지 만받을 수 없습니다.
class Okno_GL : public QMainWindow
{
Q_OBJECT
public:
explicit Okno_GL(QWidget *parent = 0);
QWidget *wg;
QPushButton *pb;
QPushButton *pl;
QGridLayout *gr;
QUdpSocket *socket;
QHostAddress host;
QHostAddress bcast;
signals:
public slots:
void SLOT_Write();
void SLOT_load();
};
class Receiver : public QObject
{
Q_OBJECT
public:
Receiver();
QUdpSocket *udpSocket;
public slots:
void SLOT_processPendingDatagrams();
void SLOT_StCh(QAbstractSocket::SocketState state);
};
Okno_GL::Okno_GL(QWidget *parent) :
QMainWindow(parent)
{
pb = new QPushButton("write" , this);
pl = new QPushButton("read" , this);
wg = new QWidget(this);
setCentralWidget(wg);
gr = new QGridLayout(wg);
gr->addWidget(pb);
gr->addWidget(pl);
socket = new QUdpSocket(this);
connect(pb , SIGNAL(clicked()) , SLOT(SLOT_Write()));
connect(pl , SIGNAL(clicked()) , SLOT(SLOT_load()));
}
void Okno_GL::SLOT_Write()
{
QByteArray datagram = "gS";
int send;
send = socket->writeDatagram(datagram.data(), QHostAddress("194.181.161.134"), 1200);
}
void Okno_GL::SLOT_load()
{
}
Receiver::Receiver()
{
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)) , this , SLOT(SLOT_StCh(QAbstractSocket::SocketState)));
if(udpSocket->bind(QHostAddress::Any , 1200))
{
qd "bind";
}
else
{
qd "not bind";
}
}
void Receiver::SLOT_processPendingDatagrams()
{
qd "receiver";
QByteArray datagram;
do {
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
} while (udpSocket->hasPendingDatagrams());
qd "datagram" << datagram;
}
void Receiver::SLOT_StCh(QAbstractSocket::SocketState state)
{
qd "slot" << state;
QByteArray datagram = "gS";
if (state == QAbstractSocket::BoundState) {
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(SLOT_processPendingDatagrams()) , Qt::QueuedConnection);
}
}
는 'Receiver'의 어딘가에 생성 된 개체입니까 ?? – Blueman
확인해야 할 중요한 사항 중 하나 : 수신 시스템에서 Wireshark의 패킷을 볼 수 있습니까? – jturcotte
죄송합니다. ofcourse 오브젝트 수신자는 오브젝트 Okno_GL과 동일하게 메인에서 작성됩니다. Ofocourse firs는 Creetet Okno_GL입니다. jtucotte ok 내가 이것을 씹어 답장을했다. – FanQt