-
Notifications
You must be signed in to change notification settings - Fork 0
/
laclient.cpp
91 lines (73 loc) · 1.76 KB
/
laclient.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pthread.h>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "netutils.h"
#include "command.h"
#include "miscutils.h"
#include "configparser.h"
#include "logutils.h"
const int STR_MAX_LEN = 1024;
void sighandler(int signo)
{
CommandExecuter::runningflag = 0;
}
int main(int argc, char** argv)
{
string myname, port;
int sockfd, ret;
sockaddr_in myaddr;
sockaddr_in peeraddr;
socklen_t len;
pthread_t tid;
char buff[64];
miscutils::initdaemon(argc, argv);
gethostname(buff, 64);
gparser->initkvmap("conf/lasync.conf");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd == -1)
{
perror("socket");
goto ERROR;
}
ret = gparser->getvalue("port", port);
if(!ret)
{
glogger << "cannot get server listen port" << endl;
goto ERROR;
}
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(atoi(port.c_str()));
myaddr.sin_addr.s_addr = INADDR_ANY;
len = sizeof(struct sockaddr);
ret = bind(sockfd, (struct sockaddr*)&myaddr, len);
if(ret == -1)
{
perror("bind");
goto ERROR;
}
ret = pthread_create(&tid, NULL, CommandExecuter::executecmd, NULL);
if(ret)
{
glogger << "cannot create thread" << endl;
goto ERROR;
}
while(1)
{
char recvbuf[STR_MAX_LEN];
int msglen = recvfrom(sockfd, recvbuf, STR_MAX_LEN, 0, (struct sockaddr*)&peeraddr, &len);
CommandExecuter::extractmsg((char*)recvbuf, msglen, peeraddr, myname);
}
return 0;
ERROR:
close(sockfd);
exit(1);
}