-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientFill.c
More file actions
92 lines (79 loc) · 2.64 KB
/
clientFill.c
File metadata and controls
92 lines (79 loc) · 2.64 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
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#define SERVER_PORT 1060 /// CHANGE!
#define BUF_SIZE 256
char store[BUF_SIZE];
char * content= " 4096:[content]";
char sendLine[BUF_SIZE];
char receiveLine[BUF_SIZE];
void * fill(void * character){
printf("\nFilling\n");
strcpy(store, "store ");
int serverSocket, bytesRead;
// These are the buffers to talk back and forth with the server
char * name = (char *) character;
// Create socket to server
if ( (serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Unable to create socket\n");
exit(-1);
// return -1;
}
// Setup server connection
struct sockaddr_in serverAddress;
bzero(&serverAddress, sizeof(serverAddress)); // Ensure address is blank
// Setup the type of connection and where the server is to connect to
serverAddress.sin_family = AF_INET; // AF_INET - talk over a network, could be a local socket
serverAddress.sin_port = htons(SERVER_PORT); // Conver to network byte order
// Try to convert character representation of IP to binary
if (inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr) <= 0) {
printf("Unable to convert IP for server address\n");
exit(-1);
// return -1;
}
// Connect to server, if we cannot connect, then exit out
if (connect(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) {
printf("Unable to connect to server");
exit(-1);
}
strcat(store, name);
strcat(store, content);
strcpy(sendLine, store);
printf("\nSending: %s\n", sendLine);
// Write will actually write to a file (in this case a socket) which will transmit it to the server
write(serverSocket, sendLine, strlen(sendLine));
// Now start reading from the server
// Read will read from socket into receiveLine up to BUF_SIZE
while ( (bytesRead = read(serverSocket, receiveLine, BUF_SIZE)) > 0) {
receiveLine[bytesRead] = 0; // Make sure we put the null terminator at the end of the buffer
printf("Received %d bytes from server with message: %s\n", bytesRead, receiveLine);
// Got response, get out of here
break;
}
// Close the server socket
close(serverSocket);
//return 1;
}
int main(int argc, char *argv[]) {;
char * a = "a";
char * b = "b";
char * c = "c";
char * d = "d";
char * e = "e";
char * f = "f";
char * g = "g";
char * h = "h";
fill(a);
fill(b);
fill(c);
fill(d);
fill(e);
fill(f);
fill(g);
fill(h);
}