Qt and threaded local server , why is the whole UI stuck? - sockets

Here's a minimal test case , I tried to start a local domain server , with QThread , so the UI shouldn't stuck. But when it's starting , i saw Listening output from qDebug() , but the widgets added from form editor totally disappeared , everything went slow (e.g resizing the window) , if i remove thread.start() , the UI shows up and functions well.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect (&thread , SIGNAL(started()) , SLOT(setupServer()));
thread.start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setupServer()
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
// create socket .. and create socket file ..
// bind ...
// listen ..
qDebug() << "Listening ..";
while((connection_fd = ::accept(socket_fd,
(struct sockaddr *) &address,
&address_length)) > -1)
{
qDebug() << "Got an connection.";
::close (connection_fd);
}
// close socket and remove the socket file
}

The accept(2) syscall is by default blocking. You should take advantage of the multiplexing syscall poll(2) or select(2) used by the QApplication's exec event loop.
See this question and use the QtNetwork module.

First of all, your setupServer is always called in gui thread. Deliver your own QThread based class and reimplement run method. Put your "setupServer" code inside

Related

Synchronizing Netlink Socket Communication

I am writing a kernel module that hooks some system calls (e.g. tcp_send() ) using jprobes and sends some information to the userspace using netlink sockets.
netlink_unicast(nlsk, skb, pid, MSG_DONTWAIT);
my callback call is:
void nl_recv(struct sk_buff *skb) {
struct nlmsghdr *nlh;
if (skb == NULL) {
return;
}
nlh = (struct nlmsghdr *) skb->data;
pid = nlh->nlmsg_pid;
debug(KERN_NOTICE "Kernel Module: Received pid from %u\n", pid);
}
I'd like to pause the execution of my kernel module after every send. relaunch on receive.
I have tried using completions and wait queues, but it seems that they push the session into a GPF.

ZeroMQ: PUB socket binding problems

I have a simple PUB/SUB messaging program. I'm trying to bind it to tcp://*:5556 range to publish at all available ips with the port. When a socket is binding the error is returning:
Resource temporarily unavailable.
I'm trying to start the publisher thread first. The port is free.
What is wrong with the binding process?
Publisher:
int main (void) {
void *context = zmq_ctx_new();
void *publisher = zmq_socket(context, ZMQ_PUB);
zmq_bind(publisher, "tcp://*:5556");
printf("Binding: %s\n", zmq_strerror(errno));
srandom((unsigned) time(NULL));
while(true) {
char update[20];
sprintf(update, "%05d %d %d", randof(10000), randof(215) - 80, randof(50) + 10);
sleep(1);
zmq_send(publisher, update, strlen(update), 0);
printf("Sending: %s", zmq_strerror(errno));
}
zmq_close(publisher);
zmq_ctx_destroy(context);
return 0;
}
Subscriber:
int main () {
void *context = zmq_ctx_new();
void *subscriber = zmq_socket(context, ZMQ_SUB);
zmq_connect(subscriber, "tcp://localhost:5556");
printf("Connection: %s\n", zmq_strerror(errno));
while(true) {
char update[20];
zmq_recv(subscriber, update, 20, 0);
printf("Receiving: %s\n", zmq_strerror(errno));
printf("Message: %s\n", update);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
From the info given, most likely something else is already bound to that socket. Is there already an instance of your program running on your machine? Or another process using that port no.
Try replacing tcp with ipc. For writing these sample programs it can avoid problems like that.
Also: your SUB socket isn't subscribed to anything, so even if the bind works the SUB will receive nothing.

to create a socket to read image from url in linux

I want to write a socket program which read an image from an url and store it at a location.
I am familiar with basic of socket programming but never created such type of socket.
Below is the code to create a socket to read data from client which i want to modify for above mentioned problem.
Please help me with this.
Thank you
Code::
include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<errno.h>
#include<stdlib.h>
#define sport 5000
int main(int argc,char **argv[])//Main function
{
struct sockaddr_in serveraddr;
int m,sd,acceptsd,rc,i;
char command[]="hello\n";
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("error in socket creation\n");
exit(0);
}
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(sport);
serveraddr.sin_addr.s_addr=(INADDR_ANY);
//serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
rc=bind(sd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
if(rc<0)
{
perror("srver bind error\n");
close(sd);
exit(-1);
}
rc=listen(sd,5);
if(rc<0)
{
perror("listen error");
close(sd);
exit(-1);
}
printf("server is ready to accept connection\n");
printf("\n");
acceptsd=accept(sd,NULL,NULL);
if(acceptsd<0)
{
perror("error in accepting\n");
exit(-1);
close(sd);
}
rc=send(acceptsd,command2,sizeof(command2),0);
if(rc<0)
{
perror("error in sending\n");
close(acceptsd);
close(sd);
exit(-1);
}
close(acceptsd);
close(sd);
exit(0);
}
The code needs to use TCP sockets and implement HTTP protocol to be able to access any URLs.
Implementing HTTP protocol handling on your own is a daunting and labour intensive task. A good start would be using libcurl.

WSAConnect returns WSAEINVAL on WindowsXP

I use sockets in non-blocking mode, and sometimes WSAConnect function returns WSAEINVAL error.
I investigate a problem and found, that it occurs if there is no pause (or it is very small ) between
WSAConnect function calls.
Does anyone know how to avoid this situation?
Below you can found source code, that reproduce the problem. If I increase value of parameter in Sleep function to 50 or great - problem dissapear.
P.S. This problem reproduces only on Windows XP, on Win7 it works well.
#undef UNICODE
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
#pragma comment(lib, "Ws2_32.lib")
static int getError(SOCKET sock)
{
DWORD error = WSAGetLastError();
return error;
}
void main()
{
SOCKET sock;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Socket Initialization Error. Program aborted\n");
return;
}
for (int i = 0; i < 1000; ++i) {
struct addrinfo hints;
struct addrinfo *res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
hints.ai_protocol = IPPROTO_TCP;
if (0 != getaddrinfo("172.20.1.59", "8091", &hints, &res)) {
fprintf(stderr, "GetAddrInfo Error. Program aborted\n");
closesocket(sock);
WSACleanup();
return;
}
struct addrinfo *ptr = 0;
for (ptr=res; ptr != NULL ;ptr=ptr->ai_next) {
sock = WSASocket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol, NULL, 0, NULL); //
if (sock == INVALID_SOCKET)
int err = getError(sock);
else {
u_long noblock = 1;
if (ioctlsocket(sock, FIONBIO, &noblock) == SOCKET_ERROR) {
int err = getError(sock);
closesocket(sock);
sock = INVALID_SOCKET;
}
break;
}
}
int ret;
do {
ret = WSAConnect(sock, ptr->ai_addr, (int)ptr->ai_addrlen, NULL, NULL, NULL, NULL);
if (ret == SOCKET_ERROR) {
int error = getError(sock);
if (error == WSAEWOULDBLOCK) {
Sleep(5);
continue;
}
else if (error == WSAEISCONN) {
fprintf(stderr, "+");
closesocket(sock);
sock = SOCKET_ERROR;
break;
}
else if (error == 10037) {
fprintf(stderr, "-");
closesocket(sock);
sock = SOCKET_ERROR;
break;
}
else {
fprintf(stderr, "Connect Error. [%d]\n", error);
closesocket(sock);
sock = SOCKET_ERROR;
break;
}
}
else {
int one = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one));
fprintf(stderr, "OK\n");
break;
}
}
while (1);
}
std::cout<<"end";
char ch;
std::cin >> ch;
}
You've got a whole basketful of errors and questionable design and coding decisions here. I'm going to have to break them up into two groups:
Outright Errors
I expect if you fix all of the items in this section, your symptom will disappear, but I wouldn't want to speculate about which one is the critical fix:
Calling connect() in a loop on a single socket is simply wrong.
If you mean to establish a connection, drop it, and reestablish it 1000 times, you need to call closesocket() at the end of each loop, then call socket() again to get a fresh socket. You can't keep re-connecting the same socket. Think of it like a power plug: if you want to plug it in twice, you have to unplug (closesocket()) between times.
If instead you mean to establish 1000 simultaneous connections, you need to allocate a new socket with socket() on each iteration, connect() it, then go back around again to get another socket. It's basically the same loop as for the previous case, except without the closesocket() call.
Beware that since XP is a client version of Windows, it's not optimized for handling thousands of simultaneous sockets.
Calling connect() again is not the correct response to WSAEWOULDBLOCK:
if (error == WSAEWOULDBLOCK) {
Sleep(5);
continue; /// WRONG!
}
That continue code effectively commits the same error as above, but worse, if you only fix the previous error and leave this, this usage will then make your code start leaking sockets.
WSAEWOULDBLOCK is not an error. All it means after a connect() on a nonblcoking socket is that the connection didn't get established immediately. The stack will notify your program when it does.
You get that notification by calling one of select(), WSAEventSelect(), or WSAAsyncSelect(). If you use select(), the socket will be marked writable when the connection gets established. With the other two, you will get an FD_CONNECT event when the connection gets established.
Which of these three APIs to call depends on why you want nonblocking sockets in the first place, and what the rest of the program will look like. What I see so far doesn't need nonblocking sockets at all, but I suppose you have some future plan that will inform your decision. I've written an article, Which I/O Strategy Should I Use (part of the Winsock Programmers' FAQ) which will help you decide which of these options to use; it may instead guide you to another option entirely.
You shouldn't use AI_PASSIVE and connect() on the same socket. Your use of AI_PASSIVE with getaddrinfo() tells the stack you intend to use this socket to accept incoming connections. Then you go and use that socket to make an outgoing connection.
You've basically lied to the stack here. Computers find ways to get revenge when you lie to them.
Sleep() is never the right way to fix problems with Winsock. There are built-in delays within the stack that your program can see, such as TIME_WAIT and the Nagle algorithm, but Sleep() is not the right way to cope with these, either.
Questionable Coding/Design Decisions
This section is for things I don't expect to make your symptom go away, but you should consider fixing them anyway:
The main reason to use getaddrinfo() — as opposed to older, simpler functions like inet_addr() — is if you have to support IPv6. That kind of conflicts with your wish to support XP, since XP's IPv6 stack wasn't nearly as heavily tested during the time XP was the current version of Windows as its IPv4 stack. I would expect XP's IPv6 stack to still have bugs as a result, even if you've got all the patches installed.
If you don't really need IPv6 support, doing it the old way might make your symptoms disappear. You might end up needing an IPv4-only build for XP.
This code:
for (int i = 0; i < 1000; ++i) {
// ...
if (0 != getaddrinfo("172.20.1.59", "8091", &hints, &res)) {
...is inefficient. There is no reason you need to keep reinitializing res on each loop.
Even if there is some reason I'm not seeing, you're leaking memory by not calling freeaddrinfo() on res.
You should initialize this data structure once before you enter the loop, then reuse it on each iteration.
else if (error == 10037) {
Why aren't you using WSAEALREADY here?
You don't need to use WSAConnect() here. You're using the 3-argument subset that Winsock shares with BSD sockets. You might as well use connect() here instead.
There's no sense making your code any more complex than it has to be.
Why aren't you using a switch statement for this?
if (error == WSAEWOULDBLOCK) {
// ...
}
else if (error == WSAEISCONN) {
// ...
}
// etc.
You shouldn't disable the Nagle algorithm:
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, ...);

TCP Client with select , after connect waits for input

I've ran into a problem with a simple TCP Client implemented using select.
The problem is that,at the second printf it only displays before it gets to the connect() function then waits for user input. Does connect block the rest of the program until i send something? (The TCP server is also implemented using select but i didn't find anything wrong with it)
I've searched on the web and couldn't find a cause or maybe i didn't search for the right thing..
#include <includes.h>
int main()
{
int sfd;
fd_set rset;
char buff[1024]=" ";
char playerName[20]="";
int nameSet=0;
struct sockaddr_in server;
sfd= socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{ printf("socket not created\n"); return 0; }
bzero(&server,sizeof(struct sockaddr_in));
server.sin_family=AF_INET;
server.sin_port=htons(2020);
inet_aton("127.0.0.1",&server.sin_addr);
//here is the problem after %d which calls the connect() function
printf("Conexion returned:%d \n Name:",connect(sfd,(struct sockaddr *)&server,sizeof(server)));
for(;;)
{
bzero(buff,1024);
FD_ZERO(&rset);
FD_SET(0,&rset);
FD_SET(sfd,&rset);
if(select(sfd+1,&rset,NULL,NULL,NULL)<0)
{
printf("con-lost!\n");
break;
}
if(FD_ISSET(0,&rset))
{
printf("Talk: \n");
scanf("%s",buff);
if(nameSet==0)
{
strcpy(playerName,buff);
nameSet=1;
printf("Hi:%s\n",playerName);
}
if(write(sfd,buff,strlen(buff)+10)<0)
{
break;
}
}
if(FD_ISSET(sfd,&rset)>0)
{
if(read(sfd,buff,1024)<=0)
{
printf("con is off!\n");
break;
}
printf("msg rcd %s\n",buff);
}
} //endfor
close(sfd);
return 0;
} //endmain
The connect function, on a blocking socket, blocks until the connect operation succeeds or fails.
You should be warned that using select with a blocking socket, which is what your program does, does not ensure that your program will not block. When you get a select hit, that does not guarantee that a future operation will not block.
strlen(buff)+10
What's the reasoning behind the +10?