summaryrefslogtreecommitdiff
path: root/src/client.c
blob: cd0123237bf5d7f88675bc9708bd6b104e73611d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//  Weather update client
//  Connects SUB socket to tcp://localhost:5556
//  Collects weather updates and finds avg temp in zipcode
#include <zmq.h>
#include <assert.h>
#include <time.h>
#include <string.h>

#include "utils.h"

int main (int argc, char *argv [])
{
  if(argc != 3){
    printf("Usage: %s <address> <port>",argv[0]);
    exit(1);
  }

  //----- Arguments
  char *ip=argv[1];
  int port=atoi(argv[2]);

  //----- Init ZMQ
  void *context = zmq_ctx_new ();
  void *subscriber = zmq_socket (context, ZMQ_SUB);
  char bindto[30];
  sprintf(bindto,"tcp://%s:%d",ip,port);
  int rc = zmq_connect (subscriber, bindto);
  if(rc!=0){
    printf("Failed to bind zmq on %s\n",bindto);
    exit(1);
  }
  rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
                        ZMQ_TOKEN, strlen(ZMQ_TOKEN));

  //----- Listen
  char buffer[ZMQ_MSG_SIZE];
  int size;
  while(1){
    size=zmq_recv (subscriber, buffer, ZMQ_MSG_SIZE-1, 0);
    buffer[size < ZMQ_MSG_SIZE ? size : ZMQ_MSG_SIZE - 1] = '\0';
    printf("Received: %s\n",buffer);
  }



  zmq_close (subscriber);
  zmq_ctx_destroy (context);

  return 0;
}