-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_final.c
More file actions
181 lines (100 loc) · 3.09 KB
/
cli_final.c
File metadata and controls
181 lines (100 loc) · 3.09 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <arpa/inet.h>
const int size = 1000;
char delimiter[1] = {' '};
char * token;
//char printbuffer[size];
//int number;
//char buff[size];
typedef struct my_process {
int pid;
char * name;
bool isActive;
bool isValid;
time_t startingTime;
time_t endingTime;
} my_process;
#define MAX_LIMIT 100
int ctr = 0;
// Create an array of processes
my_process process_arr[MAX_LIMIT];
int main(int argc, char * argv[]){
int sockfd, portno;
struct sockaddr_in serv_addr;
struct hostent * server;
char buff[size];
// char buffer[256];
if(argc<3) {
write(STDOUT_FILENO,"Executable, hostname port\n",sizeof("Executable, hostname port\n"));
exit(1);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET,SOCK_STREAM,0);
if (sockfd < 0) {
perror("Error at Socket Api ");
exit(1);
}
server = gethostbyname(argv[1]);
if (server == NULL) {
write(STDOUT_FILENO,"Cannot resolve hostname.\n",sizeof("Cannot resolve hostname.\n"));
exit(1);
}
bzero((char *) &serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
// connect
int con = connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (con <0 ) {
perror("Error at Connect Api ");
exit(1);
}
//
int n=0;
while(1) {
bzero(buff,size);
n = write(STDOUT_FILENO,"Enter Command: ", sizeof("Enter Command: "));
if (n<0) {
perror("Error at Write Api ");
exit(1);
}
n = read(STDIN_FILENO,buff,size);
if (n<0) {
perror("Error at Read Api ");
exit(1);
}
// if n == 0 condition unimplemented
n = write(sockfd,buff,strlen(buff)+1);
if (n<0) {
perror("Error at Sending Data ");
exit(1);
}
bzero(buff,size);
n = read(sockfd,buff,size);
if (n<0) {
perror("Error at Receiving Data");
exit(1);
}
write(STDOUT_FILENO,buff,strlen(buff) + 1);
} // while terminates
return 0;
}