-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_server.c
261 lines (207 loc) · 7.77 KB
/
client_server.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* exit, atoi, malloc, free */
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include <ctype.h> /* isdigit */
#define RESPONSE_SIZE 4096
//#define CONTENT_TYPE "Content-Type: application/json; charset=utf-8"
#define CONTENT_TYPE "Content-Type: text/html; charset=utf-8"
#define NUM_ARGUMENTS 4
#define STRING_LENGTH 80
#define CONTENT_BODY "{\"name\": \"test18\", \"content\": \"FROM c program\"}"
// calculate size of message
int getMessageSize ();
// create message to be sent via HTTP
char* getMessage ();
// fill info in struct
void fillAddrStruct (struct sockaddr_in** server_addr, struct hostent** server);
// connect the socket
void connectSocket (struct sockaddr_in** server_addr, int* socket_ref);
// create socket for connection
void createSocket (int* socket_ref);
// look up ip address
void setServer (struct hostent** server);
// send the request through write to socket
void sendRequest (char** message, int* socket_ref);
// receive the response
char* receiveResponse(int* socket_ref);
void initArgs (int* argc, char*** args);
char** getArguments(int* argc, char*** args);
int getIntValue (char** string);
char HTTP_METHOD[] = "GET";
char HOST[] = "localhost";
int PORT = 3001;
char PATH[] = "/temp";
char QUERY[] = "?sensorId=56&value=55";
int main (int argc, char ** args) {
struct hostent *server = NULL;
struct sockaddr_in* server_addr = NULL;
int socket_ref, bytes, sent, received, total;
char *message = NULL, *response = NULL;
initArgs(&argc, &args);
message = getMessage();
createSocket(&socket_ref);
setServer(&server);
fillAddrStruct(&server_addr, &server);
connectSocket(&server_addr, &socket_ref);
sendRequest(&message, &socket_ref);
response = receiveResponse(&socket_ref);
// close the socket
close(socket_ref);
//process response
printf("Response:\n%s\n", response);
free(message);
free(response);
return EXIT_SUCCESS;
}
int getMessageSize () {
int msg_size = 0;
if (strcmp(HTTP_METHOD, "POST") == 0) {
msg_size += strlen("%s %s%s HTTP/1.0\r\n");
msg_size += strlen(HTTP_METHOD);// method
msg_size += strlen(PATH);// path
msg_size += strlen(CONTENT_TYPE) + strlen("\r\n"); // header
msg_size += strlen("Content-Length: %d\r\n");
msg_size += strlen("\r\n");
msg_size += strlen(CONTENT_BODY); // body content
} else if (strcmp(HTTP_METHOD, "GET") == 0) {
msg_size += strlen("%s %s HTTP/1.0\r\n");
msg_size += strlen(HTTP_METHOD);// method
msg_size += strlen(PATH);// path
msg_size += strlen(CONTENT_TYPE); // header
msg_size += strlen(QUERY) + strlen("\r\n");
msg_size += strlen("\r\n");
}
return msg_size;
}
char* getMessage () {
// *Note - 2 new lines to end header
char* message = malloc(getMessageSize());
if (strcmp(HTTP_METHOD, "POST") == 0) {
sprintf(message,"%s %s HTTP/1.0\r\n", HTTP_METHOD, PATH);
strcat(message, CONTENT_TYPE); //header
strcat(message, "\r\n");
sprintf(message+strlen(message), "Content-Length: %lu\r\n", strlen(CONTENT_BODY));
strcat(message, "\r\n");
strcat(message, CONTENT_BODY);
} else if (strcmp(HTTP_METHOD, "GET") == 0) {
sprintf(message,"%s %s%s HTTP/1.0\r\n", HTTP_METHOD, PATH, QUERY);
strcat(message, CONTENT_TYPE); // header
strcat(message, "\r\n");
strcat(message, "\r\n");
}
//sending this...
//printf("---\n%s\n---\n", message);
return message;
}
// fill info in struct
void fillAddrStruct (struct sockaddr_in **server_addr, struct hostent** server) {
*server_addr = calloc(1, sizeof(*server_addr));
(*server_addr)->sin_family = AF_INET;
(*server_addr)->sin_port = htons(PORT);
memcpy(&(*server_addr)->sin_addr.s_addr, (*server)->h_addr, (*server)->h_length);
}
void createSocket (int* socket_ref) {
// create socket (domain, type, protocol)
// http://man7.org/linux/man-pages/man2/socket.2.html
if ((*socket_ref = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket not created yo");
exit(EXIT_FAILURE);
}
}
// connect the socket
void connectSocket (struct sockaddr_in** server_addr, int* socket_ref) {
// https://linux.die.net/man/3/connect
if (connect(*socket_ref, (struct sockaddr *) &(**server_addr), sizeof(**server_addr)) == -1) {
perror("ERROR connecting yo");
exit(EXIT_FAILURE);
} else printf("Connected yo\n");
}
// look up ip address
void setServer (struct hostent** server) {
if ((*server = gethostbyname(HOST)) == NULL) {
perror("no such host yo");
exit(EXIT_FAILURE);
}
}
// send the request through write to socket
void sendRequest (char** message, int* socket_ref) {
int total = strlen(*message);
int bytes, sent = 0;
do {
bytes = write(*socket_ref, *message+sent, total-sent);
if (bytes < 0) {
perror("ERROR writing message to socket yo");
exit(EXIT_FAILURE);
}
if (bytes == 0) break;
sent += bytes;
} while (sent < total);
printf("Data sent...yo\n\n");
}
// receive the response
char* receiveResponse(int* socket_ref) {
char* response = malloc(RESPONSE_SIZE);
int total, bytes, received = 0;
memset(response, 0, RESPONSE_SIZE);
total = RESPONSE_SIZE - 1;
do {
bytes = read(*socket_ref, response + received, total - received);
if (bytes < 0) {
perror("ERROR reading response from socket yo");
exit(EXIT_FAILURE);
}
if (bytes == 0) break;
received += bytes;
} while (received < total);
if (received == total) {
perror("ERROR storing complete response from socket yo");
exit(EXIT_FAILURE);
}
return response;
}
void initArgs (int* argc, char*** args ) {
char** arguments = NULL;
if ((arguments = getArguments(argc, args)) != NULL) {
// HOST
if (arguments[0] != NULL) strcpy(HOST, arguments[0]);
// PORT
if (arguments[1] != NULL) PORT = getIntValue(&arguments[1]);
// PATH
if (arguments[2] != NULL) strcpy(PATH, arguments[2]);
// QUERY
if (arguments[3] != NULL) strcpy(QUERY, arguments[3]);
printf("Using following values > HOST: %s PORT: %i PATH: %s QUERY: %s\n", HOST, PORT, PATH, QUERY);
}
}
char** getArguments(int* argc, char*** args) {
int numArguments = *argc;
char** arguments = *args;
char** returnList;
if (numArguments == 1) {
printf("Using default values > HOST: %s PORT: %i PATH: %s QUERY: %s\n", HOST, PORT, PATH, QUERY);
return NULL;
} else if (numArguments == NUM_ARGUMENTS + 1) {
returnList = malloc(NUM_ARGUMENTS);
for (int i = 0; i < NUM_ARGUMENTS; i++) {
returnList[i] = malloc(STRING_LENGTH * sizeof(char));
strcpy(returnList[i], arguments[i+1]);
}
} else {
printf("Usage: <HOST> <PORT> <PATH> <QUERY>\n");
exit(EXIT_FAILURE);
}
return returnList;
}
int getIntValue (char** string) {
for (int i = 0; i < strlen(*string); i++) {
if(!isdigit((*string)[i])) {
printf("PORT conversion error - found non-integer digit\n");
exit(EXIT_FAILURE);
}
}
return atoi(*string);
}