I am currently coding a port-scanner in Qt(C++) for Mac. The process of checking if a certain port is open or not works completely fine. But if the port range which the user wants to check is too big, every port will be checked but the output happens only after this process. The program actually should check for example port 1 and output the result. After that it should check the next and output and so on...
void MainWindow::checkPort(int portmin, int portmax, string ip) {
int dif = portmax - portmin;
if (dif <= 0)
return;
unsigned int open = 0;
unsigned int closed = 0;
int checked = 0;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip.c_str());
for (int i = portmin; i <= portmax; i++) {
int s = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_port = htons(i);
int con = ::connect(s, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr));
if (con == 0){
ui->textEdit->setTextColor(Qt::green);
ui->textEdit->append("Port " + QString::number(i) + " open.");
open++;
}
if (con == -1) {
ui->textEdit->setTextColor(Qt::red);
ui->textEdit->append("Port " + QString::number(i) + " closed.");
closed++;
}
::close(con);
::close(s);
checked++;
}
Have you got any advise how I could have an output after each iteration?