Finished project using VS2022
This commit is contained in:
parent
0ad810a19f
commit
0c61a9cc7c
2
.gitignore
vendored
2
.gitignore
vendored
@ -36,3 +36,5 @@
|
||||
build
|
||||
test/web_server
|
||||
.vscode
|
||||
.vs
|
||||
out
|
15
CMakeSettings.json
Normal file
15
CMakeSettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "x64-Debug",
|
||||
"generator": "Ninja",
|
||||
"configurationType": "Debug",
|
||||
"inheritEnvironments": [ "msvc_x64_x64" ],
|
||||
"buildRoot": "${projectDir}\\out\\build\\${name}",
|
||||
"installRoot": "${projectDir}\\out\\install\\${name}",
|
||||
"cmakeCommandArgs": "",
|
||||
"buildCommandArgs": "",
|
||||
"ctestCommandArgs": ""
|
||||
}
|
||||
]
|
||||
}
|
@ -16,3 +16,12 @@ The program should now be compiled at ./build/bin/proxy
|
||||
Simply run the program using:
|
||||
|
||||
build/bin/proxy
|
||||
|
||||
## Windows Notes
|
||||
Project was built under the CMake directives using Visual Studio 2022
|
||||
|
||||
No errors occurred and project was built successfully
|
||||
|
||||
web_server.cpp is included so CMake does not break
|
||||
|
||||
proxy.cpp and network.hpp are all that are needed (one .cpp and one .h file)
|
||||
|
@ -1,11 +1,9 @@
|
||||
add_executable(proxy
|
||||
./proxy.cpp
|
||||
./network.cpp
|
||||
)
|
||||
|
||||
add_executable(web_server
|
||||
./web_server.cpp
|
||||
./network.cpp
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
|
171
src/network.cpp
171
src/network.cpp
@ -1,171 +0,0 @@
|
||||
#include "network.hpp"
|
||||
#ifdef UNIX
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h> // Needed for exit()
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
// Hazardous globals
|
||||
char hazardous_contents_CS_01[256] = "password.txt";
|
||||
char hazardous_contents_CS_02[256] = "admin.config";
|
||||
|
||||
void PipeSockets(int sender_s, int receiver_s) {
|
||||
char in_buf[BUF_SIZE]; // Input buffer for GET resquest
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
|
||||
// Pass GET along from browser to server
|
||||
std::cout << "Attempting to send data to receiver" << std::endl;
|
||||
do {
|
||||
// Receive
|
||||
buf_len = recv(sender_s, in_buf, BUF_SIZE, 0);
|
||||
std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n';
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR (info) - recv" << std::endl;
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
return;
|
||||
}
|
||||
if (buf_len == 0) { continue; }
|
||||
|
||||
// Hazardous check
|
||||
if ((strstr(in_buf, hazardous_contents_CS_01) != NULL)
|
||||
|| (strstr(in_buf, hazardous_contents_CS_02) != NULL)) {
|
||||
std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl;
|
||||
strcpy(in_buf, FORBIDDEN_403);
|
||||
send(sender_s, in_buf, strlen(in_buf), 0);
|
||||
strcpy(in_buf, MESS_403);
|
||||
send(sender_s, in_buf, strlen(in_buf), 0);
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Send
|
||||
buf_len = send(receiver_s, in_buf, buf_len, 0);
|
||||
std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n';
|
||||
} while (buf_len == BUF_SIZE);
|
||||
std::cout << "Sent data to receiver" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
void ProxySockets(int sender_s, int receiver_s) {
|
||||
char out_buf[BUF_SIZE]; // Output buffer for HTML response
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
|
||||
// Pass response along from server to browser
|
||||
sleep(1);
|
||||
std::cout << "Attempting to send to browser" << std::endl;
|
||||
do {
|
||||
// Receive
|
||||
buf_len = recv(sender_s, out_buf, BUF_SIZE, 0);
|
||||
std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n';
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR (info) - recv -1" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Hazardous check
|
||||
if ((strstr(out_buf, hazardous_contents_CS_01) != NULL)
|
||||
|| (strstr(out_buf, hazardous_contents_CS_02) != NULL)) {
|
||||
std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl;
|
||||
strcpy(out_buf, FORBIDDEN_403);
|
||||
send(receiver_s, out_buf, strlen(out_buf), 0);
|
||||
strcpy(out_buf, MESS_403);
|
||||
send(receiver_s, out_buf, strlen(out_buf), 0);
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Send
|
||||
buf_len = send(receiver_s, out_buf, buf_len, 0);
|
||||
if (buf_len == 96) { break; }
|
||||
std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n';
|
||||
} while ((buf_len == BUF_SIZE) || (buf_len == 40));
|
||||
std::cout << "Sent to browser" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
}
|
||||
|
||||
Client::Client(void) {
|
||||
addressLength = sizeof(address);
|
||||
address.sin_family = AF_INET;
|
||||
}
|
||||
|
||||
bool Client::ConnectFrom(int serverFD) {
|
||||
socketFD = accept(serverFD, (struct sockaddr *)&address, &addressLength);
|
||||
if (socketFD == -1) { return 1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Client::ConnectTo(int portNumber) {
|
||||
std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl;
|
||||
address.sin_port = htons(portNumber);
|
||||
if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) {
|
||||
std::cerr << "ERROR (info) - inet_pton" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
socketFD = socket(address.sin_family, SOCK_STREAM, 0);
|
||||
if (connect(socketFD, (struct sockaddr *)&address, addressLength) == -1) {
|
||||
std::cerr << "ERROR (info) - connect" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Connection successful" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Server::Server(int portNumber) {
|
||||
std::cout << "Opening the server" << std::endl;
|
||||
#ifdef WIN
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
#endif
|
||||
if (socketFD == -1) {
|
||||
std::cerr << "ERROR - Unable to create socket on server" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons(portNumber);
|
||||
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
// Create a socket, fill-in address information, and then bind it
|
||||
if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) {
|
||||
std::cerr << "ERROR - Unable to bind socket" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
// Listen for connections and then accept
|
||||
listen(socketFD, 100);
|
||||
std::cout << "Server is now open" << std::endl;
|
||||
}
|
||||
|
||||
void Server::Close(void) {
|
||||
std::cout << "Closing the server" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(socketFD);
|
||||
WSACleanup();
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(socketFD);
|
||||
#endif
|
||||
}
|
@ -5,12 +5,13 @@
|
||||
|
||||
#ifdef UNIX
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#ifdef WIN
|
||||
#include <winsock.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
typedef int socklen_t;
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
|
||||
#define BUF_SIZE 4096 // Buffer size (big enough for a GET)
|
||||
|
||||
|
190
src/proxy.cpp
190
src/proxy.cpp
@ -1,10 +1,21 @@
|
||||
//----- Include files ---------------------------------------------------------
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h> // Needed for exit()
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include "network.hpp"
|
||||
#ifdef UNIX
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
// Hazardous globals
|
||||
char hazardous_contents_CS_01[256] = "password.txt";
|
||||
char hazardous_contents_CS_02[256] = "admin.config";
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@ -49,3 +60,182 @@ int main(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PipeSockets(int sender_s, int receiver_s) {
|
||||
char in_buf[BUF_SIZE]; // Input buffer for GET resquest
|
||||
#ifdef UNIX
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
#ifdef WIN
|
||||
SSIZE_T buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
|
||||
// Pass GET along from browser to server
|
||||
std::cout << "Attempting to send data to receiver" << std::endl;
|
||||
do {
|
||||
// Receive
|
||||
buf_len = recv(sender_s, in_buf, BUF_SIZE, 0);
|
||||
std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n';
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR (info) - recv" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
if (buf_len == 0) { continue; }
|
||||
|
||||
// Hazardous check
|
||||
if ((strstr(in_buf, hazardous_contents_CS_01) != NULL)
|
||||
|| (strstr(in_buf, hazardous_contents_CS_02) != NULL)) {
|
||||
std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl;
|
||||
strcpy(in_buf, FORBIDDEN_403);
|
||||
send(sender_s, in_buf, strlen(in_buf), 0);
|
||||
strcpy(in_buf, MESS_403);
|
||||
send(sender_s, in_buf, strlen(in_buf), 0);
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Send
|
||||
buf_len = send(receiver_s, in_buf, buf_len, 0);
|
||||
std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n';
|
||||
} while (buf_len == BUF_SIZE);
|
||||
std::cout << "Sent data to receiver" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
void ProxySockets(int sender_s, int receiver_s) {
|
||||
char out_buf[BUF_SIZE]; // Output buffer for HTML response
|
||||
#ifdef UNIX
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
#ifdef WIN
|
||||
SSIZE_T buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
|
||||
// Pass response along from server to browser
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
std::cout << "Attempting to send to browser" << std::endl;
|
||||
do {
|
||||
// Receive
|
||||
buf_len = recv(sender_s, out_buf, BUF_SIZE, 0);
|
||||
std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n';
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR (info) - recv -1" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Hazardous check
|
||||
if ((strstr(out_buf, hazardous_contents_CS_01) != NULL)
|
||||
|| (strstr(out_buf, hazardous_contents_CS_02) != NULL)) {
|
||||
std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl;
|
||||
strcpy(out_buf, FORBIDDEN_403);
|
||||
send(receiver_s, out_buf, strlen(out_buf), 0);
|
||||
strcpy(out_buf, MESS_403);
|
||||
send(receiver_s, out_buf, strlen(out_buf), 0);
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Send
|
||||
buf_len = send(receiver_s, out_buf, buf_len, 0);
|
||||
if (buf_len == 96) { break; }
|
||||
std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n';
|
||||
} while ((buf_len == BUF_SIZE) || (buf_len == 40));
|
||||
std::cout << "Sent to browser" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
}
|
||||
|
||||
Client::Client(void) {
|
||||
addressLength = sizeof(address);
|
||||
address.sin_family = AF_INET;
|
||||
}
|
||||
|
||||
bool Client::ConnectFrom(int serverFD) {
|
||||
socketFD = accept(serverFD, (struct sockaddr*)&address, &addressLength);
|
||||
if (socketFD == -1) { return 1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Client::ConnectTo(int portNumber) {
|
||||
std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl;
|
||||
address.sin_port = htons(portNumber);
|
||||
#ifdef WIN
|
||||
if (InetPton(address.sin_family, (PCSTR)kWebserverIP, &address.sin_addr) <= 0) {
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) {
|
||||
#endif
|
||||
std::cerr << "ERROR (info) - inet_pton" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
socketFD = socket(address.sin_family, SOCK_STREAM, 0);
|
||||
if (connect(socketFD, (struct sockaddr*)&address, addressLength) == -1) {
|
||||
std::cerr << "ERROR (info) - connect" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Connection successful" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Server::Server(int portNumber) {
|
||||
std::cout << "Opening the server" << std::endl;
|
||||
#ifdef WIN
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
#endif
|
||||
if (socketFD == -1) {
|
||||
std::cerr << "ERROR - Unable to create socket on server" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons(portNumber);
|
||||
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
// Create a socket, fill-in address information, and then bind it
|
||||
if (bind(socketFD, (struct sockaddr*)&address, sizeof(address)) == -1) {
|
||||
std::cerr << "ERROR - Unable to bind socket" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
// Listen for connections and then accept
|
||||
listen(socketFD, 100);
|
||||
std::cout << "Server is now open" << std::endl;
|
||||
}
|
||||
|
||||
void Server::Close(void) {
|
||||
std::cout << "Closing the server" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(socketFD);
|
||||
WSACleanup();
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(socketFD);
|
||||
#endif
|
||||
}
|
||||
|
@ -33,11 +33,20 @@
|
||||
#include <string.h> // Needed for strcpy() and strlen()
|
||||
#include <fcntl.h> // Needed for file i/o constants
|
||||
#include <sys/stat.h> // Needed for file i/o constants
|
||||
#include <stdlib.h> // Needed for exit()
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include "network.hpp"
|
||||
|
||||
// Hazardous globals
|
||||
char hazardous_contents_CS_01[256] = "password.txt";
|
||||
char hazardous_contents_CS_02[256] = "admin.config";
|
||||
|
||||
|
||||
/* FOR BSD UNIX/LINUX ---------------------------------------------------- */
|
||||
#ifdef UNIX
|
||||
#include <sys/types.h> //
|
||||
@ -172,3 +181,182 @@ void ClientRequest(int client_s) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void PipeSockets(int sender_s, int receiver_s) {
|
||||
char in_buf[BUF_SIZE]; // Input buffer for GET resquest
|
||||
#ifdef UNIX
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
#ifdef WIN
|
||||
SSIZE_T buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
|
||||
// Pass GET along from browser to server
|
||||
std::cout << "Attempting to send data to receiver" << std::endl;
|
||||
do {
|
||||
// Receive
|
||||
buf_len = recv(sender_s, in_buf, BUF_SIZE, 0);
|
||||
std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n';
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR (info) - recv" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
if (buf_len == 0) { continue; }
|
||||
|
||||
// Hazardous check
|
||||
if ((strstr(in_buf, hazardous_contents_CS_01) != NULL)
|
||||
|| (strstr(in_buf, hazardous_contents_CS_02) != NULL)) {
|
||||
std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl;
|
||||
strcpy(in_buf, FORBIDDEN_403);
|
||||
send(sender_s, in_buf, strlen(in_buf), 0);
|
||||
strcpy(in_buf, MESS_403);
|
||||
send(sender_s, in_buf, strlen(in_buf), 0);
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Send
|
||||
buf_len = send(receiver_s, in_buf, buf_len, 0);
|
||||
std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n';
|
||||
} while (buf_len == BUF_SIZE);
|
||||
std::cout << "Sent data to receiver" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
void ProxySockets(int sender_s, int receiver_s) {
|
||||
char out_buf[BUF_SIZE]; // Output buffer for HTML response
|
||||
#ifdef UNIX
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
#ifdef WIN
|
||||
SSIZE_T buf_len; // Buffer length for file reads
|
||||
#endif
|
||||
|
||||
// Pass response along from server to browser
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
std::cout << "Attempting to send to browser" << std::endl;
|
||||
do {
|
||||
// Receive
|
||||
buf_len = recv(sender_s, out_buf, BUF_SIZE, 0);
|
||||
std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n';
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR (info) - recv -1" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Hazardous check
|
||||
if ((strstr(out_buf, hazardous_contents_CS_01) != NULL)
|
||||
|| (strstr(out_buf, hazardous_contents_CS_02) != NULL)) {
|
||||
std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl;
|
||||
strcpy(out_buf, FORBIDDEN_403);
|
||||
send(receiver_s, out_buf, strlen(out_buf), 0);
|
||||
strcpy(out_buf, MESS_403);
|
||||
send(receiver_s, out_buf, strlen(out_buf), 0);
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Send
|
||||
buf_len = send(receiver_s, out_buf, buf_len, 0);
|
||||
if (buf_len == 96) { break; }
|
||||
std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n';
|
||||
} while ((buf_len == BUF_SIZE) || (buf_len == 40));
|
||||
std::cout << "Sent to browser" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(sender_s);
|
||||
closesocket(receiver_s);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
#endif
|
||||
}
|
||||
|
||||
Client::Client(void) {
|
||||
addressLength = sizeof(address);
|
||||
address.sin_family = AF_INET;
|
||||
}
|
||||
|
||||
bool Client::ConnectFrom(int serverFD) {
|
||||
socketFD = accept(serverFD, (struct sockaddr*)&address, &addressLength);
|
||||
if (socketFD == -1) { return 1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Client::ConnectTo(int portNumber) {
|
||||
std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl;
|
||||
address.sin_port = htons(portNumber);
|
||||
#ifdef WIN
|
||||
if (InetPton(address.sin_family, (PCSTR)kWebserverIP, &address.sin_addr) <= 0) {
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) {
|
||||
#endif
|
||||
std::cerr << "ERROR (info) - inet_pton" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
socketFD = socket(address.sin_family, SOCK_STREAM, 0);
|
||||
if (connect(socketFD, (struct sockaddr*)&address, addressLength) == -1) {
|
||||
std::cerr << "ERROR (info) - connect" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Connection successful" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Server::Server(int portNumber) {
|
||||
std::cout << "Opening the server" << std::endl;
|
||||
#ifdef WIN
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
#endif
|
||||
if (socketFD == -1) {
|
||||
std::cerr << "ERROR - Unable to create socket on server" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons(portNumber);
|
||||
address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
// Create a socket, fill-in address information, and then bind it
|
||||
if (bind(socketFD, (struct sockaddr*)&address, sizeof(address)) == -1) {
|
||||
std::cerr << "ERROR - Unable to bind socket" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
// Listen for connections and then accept
|
||||
listen(socketFD, 100);
|
||||
std::cout << "Server is now open" << std::endl;
|
||||
}
|
||||
|
||||
void Server::Close(void) {
|
||||
std::cout << "Closing the server" << std::endl;
|
||||
#ifdef WIN
|
||||
closesocket(socketFD);
|
||||
WSACleanup();
|
||||
#endif
|
||||
#ifdef UNIX
|
||||
close(socketFD);
|
||||
#endif
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user