Added license, readme, fixed and tested sockets.
This commit is contained in:
parent
b3406400aa
commit
d77dc480d2
|
@ -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.
|
Binary file not shown.
|
@ -1,8 +1,9 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#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);
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
Binary file not shown.
|
@ -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;
|
||||
}
|
Binary file not shown.
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue