diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6e23713 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Josua Robson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..77b4bdd --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# online-game +An online game created with C++ diff --git a/lib/connections.o b/lib/connections.o deleted file mode 100644 index d9dd418..0000000 Binary files a/lib/connections.o and /dev/null differ diff --git a/lib/socket.cpp b/lib/socket.cpp index 84b83a8..dd428b8 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -1,8 +1,9 @@ -#include #include +#include #include #include #include +#include #include "socket.h" @@ -12,7 +13,7 @@ client_tcp::client_tcp(int client) sock = client; } -client_tcp::client_tcp(char *name, int port) +client_tcp::client_tcp(const char *name, int port) { // Create the TCP socket if((sock=socket(AF_INET, SOCK_STREAM, 0))<0) @@ -21,10 +22,18 @@ client_tcp::client_tcp(char *name, int port) return; } + // Set reuse_addr + int enable = 1; + if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) + { + status = 2; + return; + } + // Get the server if((server = gethostbyname(name)) == NULL) { - status = 2; + status = 3; return; } @@ -33,7 +42,7 @@ client_tcp::client_tcp(char *name, int port) // Set some settings serv_addr.sin_family = AF_INET; - serv_addr.sin_port = port; + serv_addr.sin_port = htons(port); // Copy the server address to the serv_addr variable bcopy( @@ -43,14 +52,14 @@ client_tcp::client_tcp(char *name, int port) ); // Try connecting to the server - if (connect(sockfd, (struct sockaddr*)& serv_addr, sizeof(serv_addr)) < 0) + if (connect(sock, (struct sockaddr*)& serv_addr, sizeof(serv_addr)) < 0) { - status = 3; + status = 4; return; } // Set the status - status = 0 + status = 0; } client_tcp::~client_tcp() @@ -62,7 +71,10 @@ client_tcp::~client_tcp() char* client_tcp::recv(int size) { // Make the buffer - char* buffer = new char[size](); + char* buffer = new char[size+1](); + + // Clear the buffer + bzero((char*)buffer, size); // Get the data from the client bzero((char*)buffer, size); @@ -72,7 +84,7 @@ char* client_tcp::recv(int size) return buffer; } -bool client_tcp::send(char* data, int size) +bool client_tcp::send(const char* data, int size) { // Send the data write(sock, data, size); diff --git a/lib/socket.h b/lib/socket.h index 9218a94..693e0ae 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -13,8 +13,8 @@ public: ~client_tcp(); client_tcp(int client); - client_tcp(char *name, int port); - bool send(char* data, int size); + client_tcp(const char *name, int port); + bool send(const char* data, int size); char* recv(int size); }; diff --git a/lib/socks_test_client b/lib/socks_test_client new file mode 100644 index 0000000..80dbc92 Binary files /dev/null and b/lib/socks_test_client differ diff --git a/lib/socks_test_client.cpp b/lib/socks_test_client.cpp new file mode 100644 index 0000000..78c3d8d --- /dev/null +++ b/lib/socks_test_client.cpp @@ -0,0 +1,13 @@ +#include +#include + +#include "socket.h" + +int main(int argc, char const *argv[]) +{ + client_tcp client("localhost", 12345); + std::cout << "Status: " << client.status << '\n'; + std::cout << client.recv(2) << '\n'; + + return 0; +} diff --git a/lib/socks_test_server b/lib/socks_test_server new file mode 100644 index 0000000..ae44e07 Binary files /dev/null and b/lib/socks_test_server differ diff --git a/lib/socks_test_server.cpp b/lib/socks_test_server.cpp new file mode 100644 index 0000000..d0a4156 --- /dev/null +++ b/lib/socks_test_server.cpp @@ -0,0 +1,16 @@ +#include + +#include "socket.h" + +int main(int argc, char const *argv[]) +{ + server_tcp server(12345); + + while(1) + { + client_tcp client = server.cliaccept(); + client.send("hi", 2); + } + + return 0; +}