-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent.c
More file actions
97 lines (75 loc) · 2.4 KB
/
agent.c
File metadata and controls
97 lines (75 loc) · 2.4 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
// This is the agent application that will be used to send actions to the server.
// It takes 3 arguments, server IP or host, port, and the action requested.
// List of includes
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#define MAXBUF 1024
#define MAXHOSTNAME 200
#define MAXACTION 10
#define MAXPORT 6
int main (int argc,char* argv[]) {
char cServer[MAXHOSTNAME];
char cPort[MAXPORT];
char cAction [MAXACTION];
int nPort;
if (argc < 4)
{
printf ("Usage: client server_name server_port file_requested \r\n");
return(0);
}
memset(cServer,0,MAXHOSTNAME);
sprintf(cServer,"%s",argv[1]);
memset(cPort, 0, MAXPORT);
sprintf(cPort,"%s",argv[2]);
nPort = atoi(cPort);
memset(cAction,0,MAXACTION);
sprintf(cAction,"%s",argv[3]);
struct sockaddr_in name;
struct hostent *hent;
int sd;
// create a new socket to use for communication
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf (stderr, "ERROR: socket() failed\n");
exit (-1);
}
fprintf(stdout,"Socket Created\n");
// Used to correctly fill in the server host
if ((hent = gethostbyname (cServer)) == NULL) {
fprintf (stderr, "ERROR: Host not found\n");
exit(-1);
}
else
bcopy (hent->h_addr, &name.sin_addr, hent->h_length);
name.sin_family = AF_INET;
name.sin_port = htons (nPort); // notice the host to network conversion.
// connect to the server
if (connect (sd, (struct sockaddr *)&name, sizeof(name)) < 0) {
fprintf (stderr, "ERROR: connect() failed\n");
exit (-1);
}
printf("Connect OK\n");
// send the action to the server
int byteswritten = write (sd, cAction, strlen(cAction));
printf("wrote %d\n",byteswritten);
char buffer[MAXBUF];
int bytes_read = 0;
int total_bytes_read = 0;
do {
memset(buffer,0,MAXBUF);
bytes_read = read(sd, buffer, MAXBUF);
if (bytes_read < 0)
break;
fprintf(stdout,"Read %d bytes: [%s]\r\n", bytes_read,buffer);
total_bytes_read += bytes_read;
}
while ( bytes_read > 0 );
printf("Total Bytes Read = %d\r\n",total_bytes_read);
close(sd);
return 0;
}