-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross_socket.h
71 lines (56 loc) · 1.52 KB
/
cross_socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#define SOCKET int
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#endif
#define BUFF_SIZE 8192
enum class ErrorCode {NO_ERR, CREATE_ERROR, BIND_ERROR, LISTEN_ERROR, ACCEPT_ERROR, CONNECT_ERROR, RECV_ERROR, SEND_ERROR};
class Socket {
protected:
SOCKET my_socket;
struct sockaddr_in my_address;
SOCKET your_socket;
struct sockaddr_in your_address;
unsigned int your_address_size;
ErrorCode error_code = ErrorCode::NO_ERR;
public:
Socket();
bool IsValid();
bool IsInvalid();
virtual int Recv(char *buffer)=0;
virtual int Send(char *buffer)=0;
virtual std::string Recv()=0;
virtual int Send(std::string data)=0;
virtual void Close();
static int StartUp();
static int CleanUp();
};
class ServerSocket : public Socket {
public:
int Bind(std::string ip_address, int port);
int Listen(int backlog);
int Accept();
void CloseClient();
virtual int Recv(char *buffer);
virtual int Send(char *buffer);
virtual std::string Recv();
virtual int Send(std::string data);
virtual void Close();
};
class ClientSocket : public Socket {
public:
int Connect(std::string ip_address, int port);
virtual int Recv(char *buffer);
virtual int Send(char *buffer);
virtual std::string Recv();
virtual int Send(std::string data);
};