-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertCode.c
64 lines (52 loc) · 1.23 KB
/
InsertCode.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#define PORT 25555
int main(int argc, char const *argv[]){
int sock = 0;
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("\n Socket creation error \n");
return -1;
}
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if(connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
printf("\nConnection Failed \n");
return -1;
}
pid_t pid = fork();
if(pid){
char err[1];
read(sock, err, 1);
if(err[0] == '1' || err[0] == '2'){
printf("Times Up\n");
kill(pid, SIGKILL);
}
if(err[0] == '0'){
printf("Code received with success\n");
}
close(sock);
}
else{
printf("Insert access code:\n");
char buff[37];
read(0, buff, 37);
buff[36] = '\0';
printf("Sending code\n");
ssize_t sended = send(sock, buff, 37, 0);
if(sended > 0){
printf("Code sended with success\n");
}
}
return 0;
}