commit 4eff89cf759940f8e4e12f28baa5ca7faff43eb5 Author: jsrobson10 Date: Wed Mar 20 14:13:25 2019 +1100 Initial Commit diff --git a/client/connections.cpp b/client/connections.cpp new file mode 100644 index 0000000..f6a6f78 --- /dev/null +++ b/client/connections.cpp @@ -0,0 +1,8 @@ +#include "lib/socket.h" + +client_tcp *client; + +int connections_init() +{ + client = new client_tcp() +} diff --git a/client/connections.h b/client/connections.h new file mode 100644 index 0000000..e69de29 diff --git a/client/lib b/client/lib new file mode 120000 index 0000000..dc598c5 --- /dev/null +++ b/client/lib @@ -0,0 +1 @@ +../lib \ No newline at end of file diff --git a/client/main.cpp b/client/main.cpp new file mode 100644 index 0000000..57efb02 --- /dev/null +++ b/client/main.cpp @@ -0,0 +1,7 @@ +#include "connections.h" + +int main(int argc, char const *argv[]) +{ + + return 0; +} diff --git a/lib/connections.o b/lib/connections.o new file mode 100644 index 0000000..d9dd418 Binary files /dev/null and b/lib/connections.o differ diff --git a/lib/socket.cpp b/lib/socket.cpp new file mode 100644 index 0000000..1e6bb11 --- /dev/null +++ b/lib/socket.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include + +#include "socket.h" + +client_tcp::client_tcp(int client) +{ + // Set the client + sock = client; +} + +client_tcp::client_tcp(char *name, int port) +{ + // Create the TCP socket + if((sock=socket(AF_INET, SOCK_STREAM, 0))<0) + { + status = 1; + return; + } + + // Get the server + if((server = gethostbyname(name)) == NULL) + { + status = 2; + return; + } + + // Zero out the data + bzero((char *) &serv_addr, sizeof(serv_addr)); + + // Set some settings + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = port; + + // Copy the server address to the serv_addr variable + bcopy( + (char *)server->h_addr, + (char *)&serv_addr.sin_addr.s_addr, + server->h_length + ); + + // Try connecting to the server + if (connect(sockfd, (struct sockaddr*)& serv_addr, sizeof(serv_addr)) < 0) + { + status = 3; + return; + } +} + +client_tcp::~client_tcp() +{ + // Close the socket + close(sock); +} + +char* client_tcp::recv(int size) +{ + // Make the buffer + char* buffer = new char[size](); + + // Get the data from the client + bzero((char*)buffer, size); + read(sock, buffer, size); + + // Return the data + return buffer; +} + +bool client_tcp::send(char* data, int size) +{ + // Send the data + write(sock, data, size); +} + + + +server_tcp::server_tcp(int port) +{ + // Create the TCP socket + if((sock=socket(AF_INET, SOCK_STREAM, 0))<0) + { + status = 1; + return; + } + + // Zero out the data + bzero((char *) &serv_addr, sizeof(serv_addr)); + + // Set some config + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(port); + + // Bind the port + if(bind(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) + { + status = 2; + return; + } + + // Set status of 0 + status = 0; +} + +server_tcp::~server_tcp() +{ + // Close the socket + close(sock); +} + +client_tcp server_tcp::cliaccept() +{ + // Client address + struct sockaddr_in cli_addr; + + // Listen for new connections + listen(sock,5); + + // Get the client size + socklen_t clilen = sizeof(cli_addr); + + // Accept a new client + int newsock = accept(sock, (struct sockaddr *) &cli_addr, &clilen); + + // Return the client + return client_tcp(newsock); +} diff --git a/lib/socket.h b/lib/socket.h new file mode 100644 index 0000000..9218a94 --- /dev/null +++ b/lib/socket.h @@ -0,0 +1,32 @@ +#include +#include +#include + +class client_tcp +{ +public: + + struct sockaddr_in serv_addr; + struct hostent* server; + int status; + int sock; + + ~client_tcp(); + client_tcp(int client); + client_tcp(char *name, int port); + bool send(char* data, int size); + char* recv(int size); +}; + +class server_tcp +{ +public: + + struct sockaddr_in serv_addr; + int status; + int sock; + + server_tcp(int port); + ~server_tcp(); + client_tcp cliaccept(); +}; diff --git a/server/connections.cpp b/server/connections.cpp new file mode 100644 index 0000000..22d7477 --- /dev/null +++ b/server/connections.cpp @@ -0,0 +1,3 @@ +#include "lib/socket.h" + +server_tcp server; diff --git a/server/connections.h b/server/connections.h new file mode 100644 index 0000000..e69de29 diff --git a/server/lib b/server/lib new file mode 120000 index 0000000..dc598c5 --- /dev/null +++ b/server/lib @@ -0,0 +1 @@ +../lib \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp new file mode 100644 index 0000000..57efb02 --- /dev/null +++ b/server/main.cpp @@ -0,0 +1,7 @@ +#include "connections.h" + +int main(int argc, char const *argv[]) +{ + + return 0; +} diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..a80fd51 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +dfasfsdfsdaf