-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.cpp
78 lines (61 loc) · 1.63 KB
/
client.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string>
#include <sys/time.h> //struct itimerval, setitimer()
#include <signal.h> //signal()
#define MAXLINE 4096
void SendMsg(int sig)
{
printf("send message...\n");
int sockfd, n;
struct sockaddr_in servaddr;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
if(inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s\n", "127.0.0.1");
exit(0);
}
if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
if(send(sockfd, "test", strlen("test"), 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
close(sockfd);
}
void SetTimer() {
struct itimerval tick;
signal(SIGALRM, SendMsg);
memset( &tick, 0, sizeof(tick) );
tick.it_value.tv_sec = 1;
tick.it_value.tv_usec = 0;
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
int res = setitimer(ITIMER_REAL, &tick, NULL);
if(res) {
printf( "set timer failed\n" );
}
}
int main(int argc, char** argv)
{
SetTimer();
while (1) {
pause();
}
return 0;
}