-
Notifications
You must be signed in to change notification settings - Fork 0
/
Socket.c
121 lines (100 loc) · 2.39 KB
/
Socket.c
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
////////////////////////////////////////////////////////////////////////////////
//
// Global including files.
//
// Including the files for the system.
//
// These including files are generally used in different operation system.
//
////////////////////////////////////////////////////////////////////////////////
#include "Global.h"
////////////////////////////////////////////////////////////////////////////////
//
// Special including files.
//
// Including the files for the specifal operation system.
//
// These including files are used for the specifal operation system.
//
////////////////////////////////////////////////////////////////////////////////
//For windows.
#ifdef _MICROSOFT_WINDOWS
#include <winsock.h>
#endif
//For linux.
#ifdef _REDHAT_LINUX
#include <sys/socket.h>
#include <unistd.h>
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Initialize socket.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL InitializeSocket()
{
//For windows.
#ifdef _MICROSOFT_WINDOWS
WSADATA wsaData;
//Startup socket module.
if(WSAStartup(MAKEWORD(2,1),&wsaData) != 0)
{
#ifdef _DEBUG
PrintLine(stderr,"Socket::InitializeSocket : fail to startup socket !");
#endif
return _FALSE;
}
#endif
//Return true.
return _TRUE;
}
////////////////////////////////////////////////////////////////////////////////
//
// Uninitialize socket.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL UninitializeSocket()
{
//For windows.
#ifdef _MICROSOFT_WINDOWS
//Clean up all the sockets.
if(WSACleanup() != 0)
{
#ifdef _DEBUG
PrintLine(stderr,"Socket::UninitializeSocket : fail to cleanup socket !");
#endif
return _FALSE;
}
#endif
//Return true.
return _TRUE;
}
////////////////////////////////////////////////////////////////////////////////
//
// Close a socket.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL CloseSocket(_SOCKET socket)
{
#ifdef _DEBUG
assert(socket != _INVALID_SOCKET);
#endif
//For windows.
#ifdef _MICROSOFT_WINDOWS
//Close socket.
if(closesocket(socket) == _SOCKET_ERROR)
#endif
//For linux.
#ifdef _REDHAT_LINUX
//Close socket.
if(close(socket) == _SOCKET_ERROR)
#endif
{
#ifdef _DEBUG
PrintLine(stderr,"Socket::CloseSocket : error on closing socket !");
#endif
return _FALSE;
}
//Return true.
return _TRUE;
}