forked from jcyfkimi/aprs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudptomysql2.c
78 lines (67 loc) · 1.3 KB
/
udptomysql2.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <ctype.h>
#include "sock.h"
// #define DEBUG 1
#define MAXLEN 16384
#include "db.h"
#include "tomysql2.c"
void Process(int u_fd)
{
char buff[MAXLEN];
int n;
while (1) {
n = recv(u_fd, buff, MAXLEN, 0);
if (n < 0) {
err_sys("recv get %d from udp client\n", n);
exit(0);
}
if (n == 0)
continue;
buff[n] = 0;
#ifdef DEBUG
err_msg("C: %s", buff);
#endif
ToMysql(buff, n);
}
}
int main(int argc, char *argv[])
{
int u_fd;
int llen;
signal(SIGCHLD, SIG_IGN);
if (argc != 1)
err_quit("usage: udptomysql\n");
#ifndef DEBUG
daemon_init("udptomysql", LOG_DAEMON);
while (1) {
int pid;
pid = fork();
if (pid == 0) // i am child, will do the job
break;
else if (pid == -1) // error
exit(0);
else
wait(NULL); // i am parent, wait for child
sleep(2); // if child exit, wait 2 second, and rerun
}
#endif
err_msg("starting\n");
u_fd = Udp_server("127.0.0.1", "14583", (socklen_t *) & llen);
#ifdef DEBUG
err_msg("u_fd=%d\n", u_fd);
#endif
mysql = connectdb();
Process(u_fd);
return 0;
}