Added license, readme, fixed and tested sockets.

This commit is contained in:
jsrobson10 2019-03-22 12:38:27 +11:00
parent b3406400aa
commit d77dc480d2
9 changed files with 75 additions and 11 deletions

21
LICENSE Normal file
View File

@ -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.

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# online-game
An online game created with C++

Binary file not shown.

View File

@ -1,8 +1,9 @@
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h>
#include "socket.h" #include "socket.h"
@ -12,7 +13,7 @@ client_tcp::client_tcp(int client)
sock = client; sock = client;
} }
client_tcp::client_tcp(char *name, int port) client_tcp::client_tcp(const char *name, int port)
{ {
// Create the TCP socket // Create the TCP socket
if((sock=socket(AF_INET, SOCK_STREAM, 0))<0) if((sock=socket(AF_INET, SOCK_STREAM, 0))<0)
@ -21,10 +22,18 @@ client_tcp::client_tcp(char *name, int port)
return; return;
} }
// Set reuse_addr
int enable = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
{
status = 2;
return;
}
// Get the server // Get the server
if((server = gethostbyname(name)) == NULL) if((server = gethostbyname(name)) == NULL)
{ {
status = 2; status = 3;
return; return;
} }
@ -33,7 +42,7 @@ client_tcp::client_tcp(char *name, int port)
// Set some settings // Set some settings
serv_addr.sin_family = AF_INET; 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 // Copy the server address to the serv_addr variable
bcopy( bcopy(
@ -43,14 +52,14 @@ client_tcp::client_tcp(char *name, int port)
); );
// Try connecting to the server // 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; return;
} }
// Set the status // Set the status
status = 0 status = 0;
} }
client_tcp::~client_tcp() client_tcp::~client_tcp()
@ -62,7 +71,10 @@ client_tcp::~client_tcp()
char* client_tcp::recv(int size) char* client_tcp::recv(int size)
{ {
// Make the buffer // 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 // Get the data from the client
bzero((char*)buffer, size); bzero((char*)buffer, size);
@ -72,7 +84,7 @@ char* client_tcp::recv(int size)
return buffer; return buffer;
} }
bool client_tcp::send(char* data, int size) bool client_tcp::send(const char* data, int size)
{ {
// Send the data // Send the data
write(sock, data, size); write(sock, data, size);

View File

@ -13,8 +13,8 @@ public:
~client_tcp(); ~client_tcp();
client_tcp(int client); client_tcp(int client);
client_tcp(char *name, int port); client_tcp(const char *name, int port);
bool send(char* data, int size); bool send(const char* data, int size);
char* recv(int size); char* recv(int size);
}; };

BIN
lib/socks_test_client Normal file

Binary file not shown.

13
lib/socks_test_client.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
#include <unistd.h>
#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;
}

BIN
lib/socks_test_server Normal file

Binary file not shown.

16
lib/socks_test_server.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
#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;
}