-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
163 lines (143 loc) · 4.31 KB
/
Server.cpp
File metadata and controls
163 lines (143 loc) · 4.31 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
//
// Server.cpp
// CPSC441 Assignment2
//
// TCP-based client example of socket programming.
// This server interacts with the Assignment2 client.
// It hashes strings it recieves from the client word-by-word and returns the has
//
// Created by Nathan Godard on 2018-10-12.
// Adapted from code given by Dr. Carey Williamson at http://pages.cpsc.ucalgary.ca/~carey/CPSC441/examples/wordlengthserver.c (Retrieved Oct. 19, 2018)
//
#include "Server.hpp"
#include "Hasher.hpp"
#define DEBUG 0
#define HASH1 0
#define HASH2 1
#define MYHASH 0
void Server::run_server(const int PORTNUMBER) {
/* Setup socket: error message printouts implemented in member functions */
initialize_address(PORTNUMBER);
sock = create_socket();
if(sock < 0){ exit(1); } //TCP setup failed
status = bind_socket();
if(status < 0){ exit(1); } //Binding socket to address and port failed
status = set_to_listen();
if(status < 0){ exit(1); } //Setting socket to listen for incoming clients failed
std::cout << "Looks like everything set up correctly! Searching for connection...";
status = accept_connection();
if(status < 0){ exit(1); } //Establishing connection failed
/* Loop for listening to client */
while (1) {
size_t bytes_rcvd = rcv_from_client();
if(bytes_rcvd <= 0){
close(accepted_connection);
break;
}
#if DEBUG
std::cout << rcv_message;
#endif
if (strcmp(rcv_message, "CLOSE CONNECTION") == 0) {
close(accepted_connection);
break;
}
#if HASH1
hash1(rcv_message, (int)bytes_rcvd, message, MAXMESSAGELENGTH);
#elif HASH2
hash2(rcv_message, (int)bytes_rcvd, message, MAXMESSAGELENGTH);
#elif MYHASH
myhash(rcv_message, (int)bytes_rcvd, message, MAXMESSAGELENGTH);
#endif
#if DEBUG
std::cout << "about to send\n";
#endif
send_msg();
#if DEBUG
std::cout << "sent\n";
#endif
}
close(sock);
return;
}
void Server::initialize_address(const int PORTNUMBER){
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORTNUMBER);
server.sin_addr.s_addr = htonl(INADDR_ANY);
}
int Server::create_socket(){
int sock = socket(PF_INET, SOCK_STREAM, 0);
#if DEBUG
if (sock < 0) {
std::cout << "Error in socket()\n";
} else {
std::cout << "Socket created successfully.\n";
}
#endif
return sock;
}
int Server::bind_socket(){
//Makes the socket reusable if the program crashes
int status_placeholder = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &status_placeholder, sizeof(status_placeholder)) == -1) {
std::cerr << "setsockopt failed!!";
return -1;
}
int status = bind(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_in));
#if debug
if (status < 0) { std::cout << "Error in bind()\n"; }
else{ std::cout << "Binding completed.\n"; }
#endif
return status;
}
int Server::set_to_listen(){
bzero(rcv_message, MAXMESSAGELENGTH);
bzero(message, MAXMESSAGELENGTH);
int status = listen(sock, 5);
#if debug
if (status < 0) {
std::cout << "Error in listen()\n";
} else {
std::cout << "Listening for connection requests...\n";
}
#endif
return status;
}
int Server::accept_connection(){
accepted_connection = accept(sock, NULL, NULL);
#if DEBUG
if (accepted_connection < 0) {
std::cout << "Error in accept()\n";
} else{
std::cout << "Connection established.\n";
}
#endif
return accepted_connection;
}
size_t Server::send_msg(){
size_t count;
//message = (char*)"For termination send \"Bye\"\n";
count = send(accepted_connection, message, MAXMESSAGELENGTH, 0);
#if DEBUG
if (count < 0) {
std::cout << "Error in send()\n";
} else {
std::cout << "Sent message!";
}
#endif
return count;
}
size_t Server::rcv_from_client(){
ssize_t bytes_rcvd = recv(accepted_connection, rcv_message, MAXMESSAGELENGTH, 0 );
rcv_message[bytes_rcvd] = '\0';
#if DEBUG
if (bytes_rcvd <= 0){
std::cout << "Error in recieving from client\n";
} else { std::cout << "We have recieved a message (yay)\n"; }
#endif
return bytes_rcvd;
}
void Server::closeAndExit(const int sock){
close(sock);
exit(0);
}