I write a program in Qt with raw c++ sockets.
The trouble is that my function listen(listener, 1);
blocks the showing of main window of the program until it gets any data via the socket. If I delete listen function then the window is shown.
That`s all code that I use:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
int listener, sock;
struct sockaddr_in addr;
int bytes_read;
listener = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(3366);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(listener, (struct sockaddr *)&addr, sizeof(addr));
listen(listener, 1);
sock = accept(listener, NULL, NULL);
char buf[1024];
bytes_read = recv(sock, buf, 1024, 0);
ui->label->setText(ui->label->text() + QString(buf));
::close(sock);
}
Help me please to show the window straight at the start of the program. Many thanks in advance!