-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient-server.cpp
More file actions
184 lines (132 loc) · 3.86 KB
/
client-server.cpp
File metadata and controls
184 lines (132 loc) · 3.86 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
182
183
184
#include "../src/CppVec.h"
#include <iostream>
/* For networking */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
using namespace std;
#define SERVER_PORT 8887
#define CLIENT_PORT 8888
#define SERVERIP "127.0.0.1"
#define MAXBUFLEN 100
#define MESSAGES 10
/*
Compile using:
g++ -W -Wall -Werror -std=c++11 client-server.cpp ../src/CppVec.cpp ../src/VClock/VClock.cpp
*/
int initSocket(int portno) {
struct sockaddr_in addr;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
cout << "ERROR opening socket" << endl;
exit(0);
}
cout << "Socket opened on " << sockfd << endl;
bzero((char *) &addr, sizeof(addr));
// Set the appropriate fields in the server address
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
cout << "ERROR on binding" << endl;
exit(0);
}
return sockfd;
}
int server() {
cout << "Start server" << endl;
CppVec cv;
cv.initCppVector("server", "serverLogFile");
int sockfd;
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
sockfd = initSocket(SERVER_PORT);
int i;
int n = 0, nMinOne = 0, nMinTwo = 0;
char inBuf[MAXBUFLEN];
bzero(inBuf,MAXBUFLEN);
for (i = 0; i < MESSAGES; i++) {
int nBytes = recvfrom(sockfd, inBuf, MAXBUFLEN-1, 0,
(struct sockaddr*) &cli_addr, &clilen);
if (nBytes < 0) {
cout << "ERROR reading from socket" << endl;
exit(0);
}
int reply = 0;
cv.unpackReceive("Received message from client.", inBuf, &reply, MAXBUFLEN-1);
printf("Here is the message: %d. \n", reply);
/* Calculate fibonacci */
if (reply == 0){
nMinTwo = 0;
n = 0;
} else if (reply == 1){
nMinOne = 0;
n = 1;
} else {
nMinTwo = nMinOne;
nMinOne = n;
n = nMinOne + nMinTwo;
}
char* outBuf = cv.prepareSend("Sending message to client", n);
nBytes = sendto(sockfd, outBuf, MAXBUFLEN, 0, (struct sockaddr*) &cli_addr, clilen);
if (nBytes < 0) {
cout << "ERROR writing to socket" << endl;
exit(0);
}
free(outBuf);
}
return 0;
}
int client() {
cout << "Start client" << endl;
CppVec cv;
cv.initCppVector("client", "clientLogFile");
int sockfd;
struct sockaddr_in serv_addr;
socklen_t servlen = sizeof(serv_addr);
// Set up server address
bzero((char *) &serv_addr, servlen);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVERIP, &serv_addr.sin_addr);
// And the client address
sockfd = initSocket(CLIENT_PORT);
int i;
for (i = 0; i < MESSAGES; i++) {
// Use GoVector prepareSend, then send the buffer
char* outBuf = cv.prepareSend("Sending message to server", i);
int nBytes = sendto(sockfd, outBuf, MAXBUFLEN-1, 0, (struct sockaddr*) &serv_addr, servlen);
if (nBytes < 0) {
cout << "ERROR writing to socket" << endl;
exit(0);
}
free(outBuf);
char inBuf[MAXBUFLEN];
bzero(inBuf, MAXBUFLEN);
nBytes = recvfrom(sockfd, inBuf, MAXBUFLEN-1, 0,
(struct sockaddr*) &serv_addr, &servlen);
if (nBytes < 0) {
cout << "ERROR reading from socket" << endl;
exit(0);
}
int reply = 0;
cv.unpackReceive("Received message from server.", inBuf, &reply, MAXBUFLEN-1);
printf("GOT BACK: %d\n", reply);
}
return 0;
}
int main() {
int pid=fork();
if (pid==0){
sleep(2);
client();
}
else{
server();
}
// Generate a shiviz log
//system("./shiviz.sh");
return 0;
}