Zainspirowany wątkiem postanowiłem sam napisać działanie na gnieździe UDP.
Próbuję napisać program do wysyłania wiadomości tekstowej i napisałem sobie program okienkowy do tego zadania i napotkałem pewien problem. Otóż mam dwa pola do wypełnienia
adres IP
i port
i teraz tak, gdy w pole z adresem IP wpiszę adres 127.0.0.1
i w pole z portem wpiszę dowolny numer portu, to przy pomocy qInfo
wszystkie informacje mi się wyświetlają (zrzut niżej)
natomiast gdy wpiszę zupełnie inny adres IP np 172.19.88.244
i jakiś numer portu to wcale nie chce mi się pokazać przy pomocy polecenia qInfo
(zrzut niżej)
dane do połączenia z portem znajdują się w funkcji void MainWindow::setRemoteHostdata()
i ta sama funkcja wyświetla też te powyższe dane na konsolę
cały kod wygląda tak
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
this->resize(400,300);
mainWidget = new QWidget(this);
mainLayout = new QVBoxLayout(mainWidget);
lblIPv4 = new QLabel(this);
lblIPv4->setText(QString(tr("Adres IP")));
leIPv4 = new QLineEdit(this);
leIPv4->setMaxLength(15);
remoteHostIpAddress = new QHostAddress(); //tutaj tworzę obiekt który przechowuje adres IP do użytku z UDP
lblPort = new QLabel(this);
lblPort->setText(QString(tr("Port")));
lePort = new QLineEdit(this);
lePort->setMaxLength(5);
remoteHostData = new QHBoxLayout();
sendText = new QTextEdit(this);
newUdpSocket = new QUdpSocket(this); //tutaj tworzę socket UDP
buttonLayout = new QHBoxLayout();
bttSend = new QPushButton(this);
bttSend->setText(QString(tr("Wyślij wiadomość")));
bttOpenFile = new QPushButton(this);
bttOpenFile->setText(QString(tr("Wyślij plik")));
bttClear = new QPushButton(this);
bttClear->setText(QString(tr("Wyczyść pola")));
bttConnect = new QPushButton(this);
bttConnect->setText(QString(tr("Połącz")));
bttExit = new QPushButton(this);
bttExit->setText(QString(tr("Zamknij")));
mainLayout->addLayout(remoteHostData);
remoteHostData->addWidget(lblIPv4);
remoteHostData->addWidget(leIPv4);
remoteHostData->addWidget(lblPort);
remoteHostData->addWidget(lePort);
mainLayout->addWidget(sendText);
mainLayout->addLayout(buttonLayout);
buttonLayout->addWidget(bttSend);
buttonLayout->addWidget(bttOpenFile);
buttonLayout->addWidget(bttClear);
buttonLayout->addWidget(bttConnect);
buttonLayout->addWidget(bttExit);
this->setCentralWidget(mainWidget);
QObject::connect(leIPv4, &QLineEdit::textChanged, this, &MainWindow::setIPv4addr);
QObject::connect(lePort, &QLineEdit::textChanged, this, &MainWindow::setPort);
QObject::connect(bttClear, &QPushButton::clicked, leIPv4, &QLineEdit::clear);
QObject::connect(bttClear, &QPushButton::clicked, lePort, &QLineEdit::clear);
QObject::connect(bttClear, &QPushButton::clicked, this, &MainWindow::clearField);
QObject::connect(bttConnect, &QPushButton::clicked, this, &MainWindow::ipv4Addr);
QObject::connect(bttConnect, &QPushButton::clicked, this, &MainWindow::port);
QObject::connect(bttConnect, &QPushButton::clicked, this, &MainWindow::setRemoteHostdata);
QObject::connect(bttExit, &QPushButton::clicked, this, &QMainWindow::close);
}
MainWindow::~MainWindow()
{
}
void MainWindow::setPort(const QString &port) //pobieram numer portu z kontrolki
{
this->_port = port.toInt();
// qInfo()<< this->_port;
}
quint16 MainWindow::port() //zwracam numer portu z kontrolki
{
// qInfo()<< this->_port;
return this->_port;
}
void MainWindow::setIPv4addr(const QString &ipAddr) //pobieram adres IP z kontrolki
{
remoteHostIpAddress->setAddress(ipAddr);
// qInfo()<< remoteHostIpAddress->toString();
}
QHostAddress MainWindow::ipv4Addr() //zwracam adres IP
{
// qInfo()<< remoteHostIpAddress->toString();
return *this->remoteHostIpAddress;
}
void MainWindow::setRemoteHostdata() //w tej funkcji próbuję się połączyć z socketem
{
this->newUdpSocket->bind(ipv4Addr(),port());
if(newUdpSocket->isOpen()){
qInfo()<<"połączono" << newUdpSocket->localAddress().toString() << "o porcie" << newUdpSocket->localPort();;
}
else{
qInfo()<<"brak połączenia z adresem" << newUdpSocket->localAddress().toString() << "o porcie" << newUdpSocket->localPort();
qInfo()<< newUdpSocket->error();
}
}
void MainWindow::clearField()
{
this->_port = 0;
this->remoteHostIpAddress->clear();
}
czy powie mi ktoś gdzie tkwi błąd i co trzeba poprawić żeby było dobrze ? Swoją drogą co trzeba zrobić aby uzyskać connect
z otwartym socketem udp ?