Note: This is fucking broken! it's too early in the morning. Fix tomorrow.
38 lines
796 B
C++
38 lines
796 B
C++
#pragma once
|
|
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <unistd.h>
|
|
#include <string>
|
|
#include <cstdio>
|
|
|
|
namespace BettolaLib {
|
|
namespace Network {
|
|
|
|
class Socket {
|
|
public:
|
|
Socket(void);
|
|
~Socket(void);
|
|
|
|
bool create(void);
|
|
bool bind(unsigned short port);
|
|
bool listen(int backlog = 5);
|
|
Socket* accept(void); /* Return a new Socket for the accepted connection. */
|
|
bool connect(const std::string& ip_address, unsigned short port);
|
|
|
|
ssize_t send(const void* buffer, size_t length);
|
|
ssize_t recv(void* buffer, size_t length);
|
|
|
|
void close(void);
|
|
|
|
int get_sockfd(void) const { return _sockfd; }
|
|
bool is_valid(void) const { return _sockfd != -1; }
|
|
|
|
private:
|
|
int _sockfd;
|
|
struct sockaddr_in _address;
|
|
};
|
|
|
|
} /* namespace Network. */
|
|
} /* namespace BettolaLib. */
|