1+ #include " BaseSocket.h"
2+
3+
4+ BaseSocket::BaseSocket ()
5+ {
6+ _ip = " " ;
7+ _port = 0 ;
8+ _protoType = SocketProtoType::PROTO_TCP;
9+ _socket = INVALID_SOCKET;
10+ _inited = false ;
11+ }
12+ BaseSocket::~BaseSocket ()
13+ {
14+ }
15+ BaseSocket::BaseSocket (string ip, unsigned short port, SocketProtoType prototype) :
16+ _ip(ip),
17+ _port(port),
18+ _protoType(prototype),
19+ _socket(INVALID_SOCKET),
20+ _inited(false )
21+ {
22+ }
23+ bool BaseSocket::Init ()
24+ {
25+ _inited = true ;
26+ WORD wversion = MAKEWORD (2 , 2 );
27+ WSADATA wsdata;
28+ int err = -1 ;
29+ err = WSAStartup (wversion, &wsdata);
30+ if (err == SOCKET_ERROR && wsdata.wVersion != wversion)
31+ {
32+ return false ;
33+ }
34+ int type = 0 ;
35+ int protocol = 0 ;
36+ switch (_protoType)
37+ {
38+ case PROTO_TCP:
39+ type = SOCK_STREAM;
40+ protocol = IPPROTO_TCP;
41+ break ;
42+ case PROTO_UDP:
43+ type = SOCK_DGRAM;
44+ protocol = IPPROTO_UDP;
45+ break ;
46+ case PROTO_RAW:
47+ type = SOCK_RAW;
48+ protocol = IPPROTO_RAW;
49+ break ;
50+ default :
51+ type = SOCK_DGRAM;
52+ protocol = IPPROTO_IP;
53+ break ;
54+ }
55+ _socket = socket (AF_INET, type, protocol);
56+ if (_socket == INVALID_SOCKET || _socket == SOCKET_ERROR)
57+ {
58+ Clean ();
59+ return false ;
60+ }
61+
62+ if (!Bind (_ip, _port))
63+ {
64+ Clean ();
65+ return false ;
66+ }
67+
68+ return true ;
69+ }
70+ void BaseSocket::Clean ()
71+ {
72+ if (_socket != INVALID_SOCKET)
73+ closesocket (_socket);
74+
75+ if (_inited)
76+ WSACleanup ();
77+
78+ _ip = " " ;
79+ _port = 0 ;
80+ _protoType = SocketProtoType::PROTO_TCP;
81+ _socket = INVALID_SOCKET;
82+ _inited = false ;
83+ }
84+ bool BaseSocket::Bind (string ip, unsigned short port)
85+ {
86+ if (!_inited && !Init ())
87+ return false ;
88+
89+ if (_socket == INVALID_SOCKET)
90+ return false ;
91+
92+ SOCKADDR_IN addr;
93+ int len = sizeof (SOCKADDR);
94+ memset (&addr, 0 , sizeof (SOCKADDR_IN));
95+
96+ _ip = ip;
97+ _port = port;
98+
99+ if (_ip == " " )
100+ addr.sin_addr .s_addr = htonl (ADDR_ANY);
101+ else
102+ addr.sin_addr .s_addr = inet_addr (_ip.c_str ());
103+
104+ addr.sin_family = AF_INET;
105+ addr.sin_port = htons (_port);
106+
107+ if (bind (_socket, (SOCKADDR*)&addr, len) == SOCKET_ERROR)
108+ {
109+ return false ;
110+ }
111+ return true ;
112+ }
113+ bool BaseSocket::Connect (string ip,unsigned short port)
114+ {
115+ SOCKADDR_IN addr;
116+ int len = sizeof (SOCKADDR);
117+ memset (&addr, 0 , sizeof (SOCKADDR_IN));
118+
119+ if (ip == " " )
120+ addr.sin_addr .s_addr = htonl (ADDR_ANY);
121+ else
122+ addr.sin_addr .s_addr = inet_addr (ip.c_str ());
123+
124+ addr.sin_family = AF_INET;
125+ addr.sin_port = htons (port);
126+
127+ if (SOCKET_ERROR == connect (_socket, (SOCKADDR*)&addr, len))
128+ return false ;
129+
130+ return true ;
131+ }
132+ bool BaseSocket::CheckRead ()
133+ {
134+ fd_set readset;
135+ timeval timeout;
136+ timeout.tv_sec = 0 ;
137+ timeout.tv_usec = 0 ;
138+ FD_ZERO (&readset);
139+ int ret = select (FD_SETSIZE, &readset, NULL , NULL , &timeout);
140+ if (ret > 0 && FD_ISSET (_socket, &readset))
141+ {
142+ return true ;
143+ }
144+ return false ;
145+ }
146+ bool BaseSocket::CheckWrite ()
147+ {
148+
149+ }
150+ bool BaseSocket::Receive ()
151+ {
152+ return true ;
153+ }
154+ bool BaseSocket::Send ()
155+ {
156+ return true ;
157+ }
158+ void BaseSocket::ReceiveFrom ()
159+ {
160+
161+ }
162+ void BaseSocket::SendTo ()
163+ {
164+
165+ }
166+ void BaseSocket::OnReceive ()
167+ {
168+
169+ }
170+ void BaseSocket::OnSend ()
171+ {
172+
173+ }
174+ void BaseSocket::OnSendTo ()
175+ {
176+
177+ }
178+ void BaseSocket::OnReceiveFrom ()
179+ {
180+
181+ }
0 commit comments