Revision 9026523e code/server/receive.c

View differences:

code/server/receive.c
8 8
#include <arpa/inet.h>
9 9
#include <netinet/in.h>
10 10
#include <netdb.h>
11
#include <signal.h>
11 12

  
12
#define USE_SERVER
13
//#define USE_SERVER
13 14

  
14 15
#define XBEE_PORT_DEFAULT "/dev/ttyUSB0" //or try USB1
15 16
#define XBEE_GET_PACKET_TIMEOUT 1000
......
25 26
volatile int buffer_last = 0;
26 27
// first unread byte in buffer
27 28
volatile int buffer_first = 0;
29
int server_sock;
28 30

  
29 31
char *server_hostname;
30 32
char *ip_addr;
......
36 38
static void* listen_to_xbee();
37 39
int xbee_lib_init();
38 40
void wl_relay_update();
41
void sigint_handler(int param);
42

  
39 43

  
40 44
int main(int argc, char *argv[]) {
45
   
41 46
	if (argc != 3) {
42 47
        fprintf(stderr, "USAGE: %s <server_ip> <port>\n", argv[0]);
43 48
        exit(1);
44 49
    }
45 50
    
51
    server_sock = 0;
52
    signal(SIGINT, sigint_handler);
53

  
46 54
    // resolve server hostname
47 55
    server_hostname = argv[1];
48 56
    struct hostent *my_host = gethostbyname(server_hostname);
......
55 63

  
56 64
    // port on server
57 65
    port = atoi(argv[2]);
66
    
67
    // open connection to server
68
    #ifdef USE_SERVER
69
    server_sock = socket_open();
70
    #endif
58 71

  
59 72
    xbee_lib_init();
60 73
    while(1) {
......
202 215
    }
203 216

  
204 217
    #ifdef USE_SERVER
205
    int sock = socket_open();
218
    //int sock = socket_open();
206 219
    //printf("sending to server\n");
207
    if (send(sock, send_buf, len+4, 0) != len+4) {
220
    if (send(server_sock, send_buf, len+4, 0) != len+4) {
208 221
            printf("Mismatch in number of sent bytes");
209
            close(sock);
222
            //close(server_sock);
210 223
            return;
211 224
    }
212
    close(sock);
225
    //close(sock);
213 226
    #endif
214 227

  
215 228
    printf("Meter %X is now ", src);
......
248 261
    return sock;
249 262
}
250 263

  
264
void sigint_handler(int param) {
265
    close(server_sock);
266
    printf("Relay node shutting down.\n");
267
    exit(0);
268
}
269

  

Also available in: Unified diff