Changed code to support Windows libraries and functions as well

This commit is contained in:
2024-02-29 16:38:36 -05:00
parent ddbbc322a6
commit 4b3d5387a1
6 changed files with 55 additions and 30 deletions

View File

@@ -1,10 +1,12 @@
#ifndef EASYSOCK_HPP_
#define EASYSOCK_HPP_
#ifdef _WIN_32
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#else
#include <winsock.h>
#include <ws2tcpip.h>
#endif
#ifdef linux
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
@@ -12,7 +14,7 @@
#include <arpa/inet.h>
#endif
#ifndef _WIN_32
#ifndef _WIN32
typedef int SOCKET;
#endif
@@ -25,13 +27,12 @@ const char ES_UDP = 'U';
a layer 3 - network layer - integer, which must be '4' for IPv4
and 6 for IPv6;
a layer 4 - transport layer - character, which must be 'T' for
TCP or 'U' for UDP; and
a bool that indicates whether the socket should be blocking or non-blocking.
TCP or 'U' for UDP.
It creates a _blocking_ socket, and returns the created socket, or -1
if the socket creation failed.*/
It returns the created socket, or -1 if the socket creation failed.*/
SOCKET create_socket(int network, char transport, bool is_blocking = false);
SOCKET create_socket(int network, char transport);
/* This function fills in the sockaddr struct 'dest' based on the given information.
@@ -43,7 +44,7 @@ and dest is a pointer to the sockaddr struct that will be filled in.
The function returns with -202 if the network parameter contained neither '4'
nor '6'. */
SOCKET create_addr(int network, char* address, int port,struct sockaddr* dest);
int create_addr(int network, char* address, int port,struct sockaddr* dest);
@@ -53,7 +54,7 @@ same as above.
It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct, bool is_blocking = false);
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct);
/* This function utilizes the same functions as 'create_local' but _connects_ to the
@@ -62,7 +63,7 @@ as above. This function needs an empty 'sockaddr *' structure passed to it, whic
If something goes wrong, this function returns with ( -1 * errno ). */
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct, bool is_blocking = false);
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */

View File

@@ -2,7 +2,13 @@
#define _SOCK_CLASS
#include <string>
#include <sys/socket.h>
#ifdef linux
#include <sys/socket.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
/* Global constants - can be used by children classes as return values, and by any clients to check what type the socket is of */
const int SOCK_CLIENT = 'C';