-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvusocket.cpp
More file actions
56 lines (48 loc) · 871 Bytes
/
vusocket.cpp
File metadata and controls
56 lines (48 loc) · 871 Bytes
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
/**
* Socket example based on https://stackoverflow.com/questions/28027937/cross-platform-sockets
*/
#include "vusocket.h"
#include <iostream>
int sock_init()
{
#ifdef _WIN32
WSADATA wsa_data;
return WSAStartup(MAKEWORD(2,2), &wsa_data);
#else
return 0;
#endif
}
bool sock_valid(SOCKET socket) {
#ifdef _WIN32
return socket != INVALID_SOCKET;
#else
return socket >= 0;
#endif
}
int sock_error_code() {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
int sock_close(SOCKET sock)
{
int status = 0;
#ifdef _WIN32
status = shutdown(sock, SD_BOTH);
if (status == 0) { status = closesocket(sock); }
#else
status = shutdown(sock, SHUT_RDWR);
if (status == 0) { status = close(sock); }
#endif
return status;
}
int sock_quit()
{
#ifdef _WIN32
return WSACleanup();
#else
return 0;
#endif
}