-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.c
135 lines (117 loc) · 3.13 KB
/
client.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//////////////////////////////////////////////////////////
// \ /\ Simple power supply program - client side //
// ) ( ') //
// ( / ) //
// \(__)| N. //
//////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFF_SIZE 8192
int main(int argc, char const *argv[])
{
if(argc != 3) {
fprintf(stderr,"Usage: %s <Server IP> <Echo Port>\n",argv[0]);
exit(1);
}
// Get info for device
char name[50] = "T fucking V";
int mode_2 = 4700;
int mode_3 = 1000;
int use_mode = 0;
// Get info for device
char name[50];
int mode_2;
int mode_3;
int use_mode;
printf("Device name: ");
sscanf(stdin, "%^[\n]%*c", name);
printf("Normal power mode: ");
scanf("%d", &mode_2);
getchar();
printf("Limited power mode: ");
scanf("%d", &mode_3);
getchar();
// Step 0: Init variable
int client_sock;
char buff[BUFF_SIZE];
struct sockaddr_in server_addr;
int msg_len, bytes_sent, bytes_received;
// Step 1: Construct socket
client_sock = socket(AF_INET, SOCK_STREAM, 0);
// Step 2: Specify server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
// Step 3: Request to connect server
if (connect(client_sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0) {
perror("accept() failed\n");
exit(1);
}
// Step 4: Communicate with server
// First, send device info to server
memset(buff, '\0', strlen(buff) + 1);
sprintf(buff, "%s|%d|%d", name, mode_2, mode_3);
msg_len = strlen(buff);
if (msg_len == 0) {
printf("No info on device\n");
close(client_sock);
exit(1);
}
bytes_sent = send(client_sock, buff, msg_len, 0);
if (bytes_sent <= 0) {
printf("Connection close\n");
close(client_sock);
exit(1);
}
// Then, wait for server response
if (fork() == 0) {
// Child: listen from server
while(1) {
bytes_received = recv(client_sock, buff, BUFF_SIZE-1, 0);
if (bytes_received <= 0) {
// if DISCONNECT
printf("\nServer shuted down.\n");
break;
} else {
buff[bytes_received] = '\0';
}
int buff_i = atoi(buff);
// if (buff_i = 9) => max device reached => quit
if (buff_i == 9) {
printf("Max devices reached. Can't connect to server\n");
}
}
} else {
// Parent: open menu for user
do {
sleep(1);
printf(
"---- MENU ----\n"
"0. Turn off\n"
"1. Normal mode\n"
"2. Power saving mode\n"
"(Choose 0,1 or 2, others to disconnect): ");
char menu = getchar();
getchar();
switch (menu) {
case '0': printf("TURN OFF\n\n"); break;
case '1': printf("NORMAL MODE\n\n"); break;
case '2': printf("POWER SAVING MODE\n\n"); break;
default: menu = '3'; printf("DISCONNECTED\n");
}
if (menu == '3')
break;
send(client_sock, &menu, 1, 0);
} while (1);
}
// Step 5: Close socket
close(client_sock);
kill(0, SIGKILL);
return 0;
}