-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.cpp
69 lines (53 loc) · 1.31 KB
/
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
#include "stdafx.h"
#include "Socket.h"
Winsock_base::Winsock_base()
{
WSAStartup( MAKEWORD(2,0), &this->m_Wsa);
}
bool Winsock_base::CreateSocket(int af, int type, int protocol)
{
this->m_Family = af;
this->m_Sock = socket( af, type, protocol );
if(this->m_Sock == SOCKET_ERROR){
return false;
}
return true;
}
bool Winsock_base::Connect(char *pIpAddr, unsigned short Port)
{
this->m_IpAddr = gethostbyname(pIpAddr);
this->m_Port = Port;
this->m_Sockinf.sin_family = this->m_Family;
this->m_Sockinf.sin_port = htons(this->m_Port);
this->m_Sockinf.sin_addr.s_addr = ((struct in_addr *)(this->m_IpAddr->h_addr))->s_addr;
memset(this->m_Sockinf.sin_zero, 0, sizeof(this->m_Sockinf.sin_zero));
int iError;
iError = connect( this->m_Sock, (LPSOCKADDR) &this->m_Sockinf, sizeof(this->m_Sockinf) );
if(iError == SOCKET_ERROR){
return false;
}
return true;
}
int Winsock_base::Recv(char *Buffer, int Bufferlen)
{
int size;
size = recv(this->m_Sock, reinterpret_cast<char*>(Buffer), Bufferlen, 0);
if(size <= 0){
return 0;
}
return size;
}
bool Winsock_base::Send(const char *Buffer, int Bufferlen)
{
int iError;
iError = send(this->m_Sock, Buffer, Bufferlen, 0);
if(iError == SOCKET_ERROR){
return false;
}
return true;
}
Winsock_base::~Winsock_base()
{
closesocket(this->m_Sock);
WSACleanup();
}