Initial Commit

This commit is contained in:
jsrobson10 2019-03-20 14:13:25 +11:00
commit 4eff89cf75
12 changed files with 190 additions and 0 deletions

8
client/connections.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "lib/socket.h"
client_tcp *client;
int connections_init()
{
client = new client_tcp()
}

0
client/connections.h Normal file
View File

1
client/lib Symbolic link
View File

@ -0,0 +1 @@
../lib

7
client/main.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "connections.h"
int main(int argc, char const *argv[])
{
return 0;
}

BIN
lib/connections.o Normal file

Binary file not shown.

130
lib/socket.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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);
}

32
lib/socket.h Normal file
View File

@ -0,0 +1,32 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
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();
};

3
server/connections.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "lib/socket.h"
server_tcp server;

0
server/connections.h Normal file
View File

1
server/lib Symbolic link
View File

@ -0,0 +1 @@
../lib

7
server/main.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "connections.h"
int main(int argc, char const *argv[])
{
return 0;
}

1
test.txt Normal file
View File

@ -0,0 +1 @@
dfasfsdfsdaf