-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
101 lines (78 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
// Sample execution commands on line 2 and 3
// $ gcc -o client client.c -lpthread -ljson-c -ldl
// $ ./client 11115 cos 1 math
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h> // For hostent structure
#include <string.h> // For strlen
#include <unistd.h> // for read, write
#include <json-c/json.h>
void error(char *s)
{
perror(s);
exit(1);
}
int main(int argc, char* argv[])
{
if(argc < 4)
{
perror("ERROR: Provide both server name and port no, function name and input");
}
int sockfd,portno,n;
struct sockaddr_in serv_addr; //Address of the server to be connected, internet address (defined in netinet/in.h library){sin_family,sin_port,(struct)sin_addr->s_addr,sin_zero}
struct hostent *server; //Hostent structure defines the host computer on internet {*h_name,**h_aliases_h_addrtype,h_length,**h_addr_list,h_addr = h_addr_list[0]}
char buffer[1000]; // Buffer to store messages from client
// Setting variables using cli arguments
char *func_name, *value,*dll_name;
portno = atoi(argv[1]);
server = gethostbyname("localhost"); //Returns pointer to hostent structure
func_name = argv[2];
value = argv[3];
dll_name = argv[4];
if(server==NULL)
{
error("ERROR: Host was not found");
}
sockfd = socket(AF_INET,SOCK_STREAM,0); //(Addressdomain,Typeofsocket,protocol(0 chooses appropriate))
if(sockfd<0)
{
error("ERROR executing socket()");
}
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; // sets binary to zero
bcopy((char*)server->h_addr,(char*)&serv_addr.sin_addr,server->h_length); // bcopy(char*s1,char*s2,int length) copies length bytes from s1 to s2
serv_addr.sin_port = htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR executing connect()");
bzero(buffer,1000); // Set the buffer bits to 0
// Creating a json object using json-c library functions
json_object *jobj = json_object_new_object();
// Json Strings
json_object *jfunction = json_object_new_string(func_name);
json_object *jvalue = json_object_new_string(value);
json_object *jdll = json_object_new_string(dll_name);
// Form the json object as key-val pairs
json_object_object_add(jobj,"FunctionName", jfunction);
json_object_object_add(jobj,"FunctionInput", jvalue);
json_object_object_add(jobj,"DLLName", jdll);
if (strcpy(buffer, json_object_to_json_string(jobj)) == NULL)
{
error("ERROR storing data in buffer");
}
// Send data/message to the server
if (write(sockfd, buffer, strlen(buffer)) == -1)
{
error("ERROR executing write() in client");
}
printf("Data sent to Server\n");
bzero(buffer,1000); // Set the buffer bits to 0
n = read(sockfd,buffer,1000); // Read message sent from the server
if(n<0)
error("ERROR executing read() in client");
printf("Message Received from server: %s\n",buffer);
close(sockfd); // Close the client socket
return 0;
}