You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB
C++

#include "StdAfx.h"
#include "SocketMgr.h"
#include "LogMgr.h"
#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib")
CSocketMgr *gSocketMgr = new CSocketMgr;
CSocketMgr::CSocketMgr(void)
{
}
CSocketMgr::~CSocketMgr(void)
{
}
//ͨѶ<CDA8><D1B6>ʼ<EFBFBD><CABC>
void CSocketMgr::OnAppInitialize()
{
//<2F><>ʼ<EFBFBD><CABC><EFBFBD>׽<EFBFBD><D7BD><EFBFBD>socket
if(!AfxSocketInit())
{
gLogMgr->WriteDebugLog("Err---->CSocketMgr : AfxSocketInit : Err");
}
else
{
gLogMgr->WriteDebugLog("Func---->CSocketMgr : AfxSocketInit : OK");
}
}
//<2F><><EFBFBD><EFBFBD>Socket <20><><EFBFBD><EFBFBD>S_addr:<3A><><EFBFBD>ӵ<EFBFBD>IP <20><>ַ,port:<3A>˿ں<CBBF>
bool CSocketMgr::Connect(const char* S_addr,u_short port)
{
m_Socket.Create();
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr(S_addr);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(port);
if(m_Socket.Connect((SOCKADDR*)&addrSrv,sizeof(SOCKADDR)))
{
gLogMgr->WriteDebugLog("Socket <20><><EFBFBD>ӳɹ<D3B3>");
return true;
}
else
{
gLogMgr->WriteDebugLog("Socket <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>",_LOG_ERROR);
Close();
return false;
}
}
//<2F>ر<EFBFBD>socket <20><><EFBFBD><EFBFBD>
void CSocketMgr::Close()
{
gLogMgr->WriteDebugLog("Socket <20>ر<EFBFBD>");
m_Socket.Close();
}
//socket <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CSocketMgr::Send(char* lpBuf,int nBufLen)
{
m_Socket.Send(lpBuf,nBufLen,0);
}
//socket <20>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CSocketMgr::Receive(char* lpBuf,int nBufLen)
{
m_Socket.Receive(lpBuf,nBufLen,0);
}