Quantcast
Channel: Active questions tagged zeromq - Stack Overflow
Viewing all articles
Browse latest Browse all 193

ZMQ timeout while trying to create socket

$
0
0

So I am trying to create a server, which controls multiple workers. The code I am having problems with right now, is the worker code. I want each of the workers to bind to their port and wait for a command to come in from the server, which will tell the worker thread to process stuff. Workers and server each have their own main() function and are being launched seperately. Both need to create as many threads as there are ports being supplied to the main function. One thread on the server side will connect to one thread worker side.

Now, let's get to business:

typedef struct{    void *context;    int port;}worker_data;void *worker_thread(void *data){    // printf("worker thread created");    assert(data);    worker_data *worker = (worker_data *) data;    assert(worker->context);    assert(worker->port>0);    void *context_parameter = worker->context;    void *worker_socket = zmq_socket(context_parameter, ZMQ_REP);    // convert port number to string and copy to buffer    char port_buff[20];    if(snprintf(port_buff, 20, "tcp://*:%d", worker->port < 0)){        fprintf(stderr, "Port number could not be copied. Buffer size wasn't sufficient.\n");        pthread_exit(NULL);    }    int rc = zmq_bind(worker_socket, port_buff);    assert(rc == 0);    while(true){        // handle request, action, and response        // just accept the buffer sizes as they are :)        char msg_buff[MSG_LEN];        char payload_buff[MSG_LEN - 3];        char result_buff[MSG_LEN - 3];        zmq_recv(worker_socket, msg_buff, MSG_LEN, 0);        MSG_TYPE type = decode_msg(msg_buff, payload_buff);        switch(type){            // process stuff and send it back with zmq_send;            // then: goto kill_worker_thread;        }    }    kill_worker_thread: ;      // not the cleanest way to do this, but it works    zmq_close(worker_socket);    pthread_exit(NULL);}int main(int argc, char **argv){    unsigned int amount_of_ports = 0;    if(argc==1){        fprintf(stderr, "Not enough arguments: %d.\n", argc);        return 1;    }    amount_of_ports = (unsigned int) argc - 1;     // subtract 1 because program name is the first parameter    assert(amount_of_ports>0);    //printf("now creating context");    void *context = zmq_ctx_new();    // parse all port numbers    pthread_t workers[(const unsigned int)amount_of_ports];    worker_data worker_arguments[(const unsigned int)amount_of_ports];    for(unsigned int i=0; i<amount_of_ports; i++){        worker_arguments[i].context = context;        worker_arguments[i].port = atoi(argv[i+1]);        // printf("about to create thread\n");        pthread_create((pthread_t *)&workers[i], NULL, worker_thread, &worker_arguments[i]);    }    for(unsigned int i=0; i<amount_of_ports; i++){        pthread_join((pthread_t)workers[i], NULL);    }    zmq_ctx_destroy(context);    return 0;}

The main problem appears to be this line of code (in the worker_thread function):

void *worker_socket = zmq_socket(context_parameter, ZMQ_REP);

I don't know why, but if I put a return statement in front of the zmq_socket() call, the code fails the tests, but it doesn't timeout.If I put a return statement directly after the zmq_socket() call, the code doesn't fail the test as in the output does not match the expected output, but it instead timeouts and never reaches the return statement.

If you have any ideas for a potential fix or for why this could be, please let me know.Cheers


Viewing all articles
Browse latest Browse all 193

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>