-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextra.c
94 lines (85 loc) · 1.49 KB
/
extra.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
#include "extra.h"
void va_cli(int argc, char *argv[], char *server_ip, int *server_port, char *name)
{
if(argc == 4)
{
struct sockaddr_in tmp_addr;
if(inet_pton(AF_INET, argv[1], &(tmp_addr.sin_addr)) == 0)
{
printf("IP Address is invalid\n");
exit(0);
}
else
{
strcpy(server_ip, argv[1]);
}
int i;
char *port_str = argv[2];
for(i = 0; port_str[i] != '\0'; i++)
{
if(!isdigit(port_str[i]))
{
printf("Port is invalid\n!");
exit(0);
}
}
if (port_str[i] == '\0')
{
*server_port = atoi(port_str);
}
strcpy(name, argv[3]);
}
else
{
printf("ERROR!!! Syntax like: ./client ip port machine_name\n");
exit(0);
}
}
/*
* check argument valid in server
*/
void
va_ser(int argc,
char *argv[],
int *port)
{
if (argc == 2)
{
int i;
char *port_str = argv[1];
for (i = 0; port_str[i] != '\0'; i++)
{
if (!isdigit(port_str[i]))
{
printf("Port is invalid\n");
exit(0);
}
}
if (port_str[i] == '\0')
{
*port = atoi(port_str);
}
}
else
{
printf("ERROR!!! Syntax like: ./server port\n");
exit(0);
}
}
/*
* handle sigchild
*/
void
sig_chld(int signo)
{
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
{
printf("child %d terminated\n", pid);
}
}
void throwMallocException(){ // hope it will work
fprintf(stderr, "failed to locate !\n");
exit(-1);
}