-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_socket.cpp
150 lines (139 loc) · 3.69 KB
/
cross_socket.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <string>
#include <cstring>
#include "cross_socket.h"
Socket::Socket() {
my_socket = socket(AF_INET, SOCK_STREAM, 0);
if (my_socket == INVALID_SOCKET) {
error_code = ErrorCode::CREATE_ERROR;
return;
}
}
inline bool Socket::IsValid() {
return error_code == ErrorCode::NO_ERR;
}
inline bool Socket::IsInvalid() {
return error_code != ErrorCode::NO_ERR;
}
void Socket::Close() {
#ifdef _WIN32
closesocket(my_socket);
#else
close(my_socket);
#endif
}
int Socket::StartUp() {
#ifdef _WIN32
WSADATA wsa_data;
return WSAStartup(MAKEWORD(1, 1), &wsa_data);
#else
return 0;
#endif
}
int Socket::CleanUp() {
#ifdef _WIN32
return WSACleanup();
#else
return 0;
#endif
}
int ServerSocket::Bind(std::string ip_address, int port) {
if (IsInvalid()) return -2;
memset(&my_address, 0, sizeof(my_address));
my_address.sin_family = AF_INET;
my_address.sin_port = htons(port);
my_address.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(my_socket, (struct sockaddr*)&my_address, sizeof(my_address)) == -1) {
error_code = ErrorCode::BIND_ERROR;
return -1;
}
return 0;
}
int ServerSocket::Listen(int backlog) {
if (IsInvalid()) return -2;
if (listen(my_socket, backlog) == SOCKET_ERROR) {
error_code = ErrorCode::LISTEN_ERROR;
return -1;
}
return 0;
}
int ServerSocket::Accept() {
#ifdef _WIN32
your_socket = accept(my_socket, (struct sockaddr*)&your_address, (int *)&your_address_size);
#else
your_socket = accept(my_socket, (struct sockaddr*)&your_address, &your_address_size);
#endif
if (your_socket == INVALID_SOCKET) {
error_code = ErrorCode::ACCEPT_ERROR;
return -1;
}
return 0;
}
int ServerSocket::Recv(char *buffer) {
return recv(your_socket, buffer, BUFF_SIZE, 0);
}
int ServerSocket::Send(char *buffer) {
return send(your_socket, buffer, strlen(buffer) + 1, 0); // with NULL
}
std::string ServerSocket::Recv() {
char buffer[BUFF_SIZE + 5];
if (recv(your_socket, buffer, BUFF_SIZE, 0) < 0) {
throw -1;
}
return std::string(buffer);
}
int ServerSocket::Send(std::string data) {
return send(your_socket, data.c_str(), data.length() + 1, 0);
}
void ServerSocket::Close() {
CloseClient();
#ifdef _WIN32
closesocket(my_socket);
#else
close(my_socket);
#endif
}
void ServerSocket::CloseClient() {
#ifdef _WIN32
closesocket(your_socket);
#else
close(your_socket);
#endif
}
int ClientSocket::Connect(std::string ip_address, int port) {
#ifdef _WIN32
#else
struct hostent* host_info;
struct in_addr addr;
if ((host_info = gethostbyname(ip_address.c_str())) == 0) {
error_code = ErrorCode::CONNECT_ERROR;
return -2;
}
memcpy(&addr, host_info->h_addr_list[0], host_info->h_length);
ip_address = std::string(inet_ntoa(addr));
#endif
memset(&your_address, 0, sizeof(your_address));
your_address.sin_family = AF_INET;
your_address.sin_port = htons(port);
your_address.sin_addr.s_addr = inet_addr(ip_address.c_str());
if (connect(my_socket, (struct sockaddr*)&your_address, sizeof(your_address)) == SOCKET_ERROR) {
error_code = ErrorCode::CONNECT_ERROR;
return -1;
}
return 0;
}
int ClientSocket::Recv(char *buffer) {
return recv(my_socket, buffer, BUFF_SIZE, 0);
}
int ClientSocket::Send(char *buffer) {
return send(my_socket, buffer, strlen(buffer) + 1, 0); // with NULL
}
std::string ClientSocket::Recv() {
char buffer[BUFF_SIZE + 5];
if (recv(my_socket, buffer, BUFF_SIZE, 0) < 0) {
throw -1;
}
return std::string(buffer);
}
int ClientSocket::Send(std::string data) {
return send(my_socket, data.c_str(), data.length() + 1, 0);
}