#include "StdAfx.h"
#include "Ksf1Server.h"
#include "kufdll.h"
#include <windows.h>
// 接收客户端线程
DWORD __stdcall Acceptthread(void *p);
CKsf1Server::CKsf1Server(void)
{
}
CKsf1Server::~CKsf1Server(void)
{
}
void CKsf1Server::Init(u_short port, int msgLevel, HANDLE hQuit)
{
m_nPort = port;
m_nMsgLevel = msgLevel;
m_sClient = INVALID_SOCKET;
m_sServer = INVALID_SOCKET;
m_hQuit = hQuit;
}
bool CKsf1Server::IsConnect()
{
return (m_sClient != INVALID_SOCKET);
}
void CKsf1Server::DisConnect()
{
if (INVALID_SOCKET != m_sClient)
{
closesocket(m_sClient);
m_sClient = INVALID_SOCKET;
}
}
bool CKsf1Server::Close()
{
// 关闭套接字
if (INVALID_SOCKET != m_sServer)
{
closesocket(m_sServer);
m_sServer = INVALID_SOCKET;
}
if (INVALID_SOCKET != m_sClient)
{
closesocket(m_sClient);
m_sClient = INVALID_SOCKET;
}
// 释放套接字资源
WSACleanup();
return true;
}
bool CKsf1Server::Start()
{
bool bRet = false;
//初始化套结字动态库
WSADATA wsd;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
ShowMessageInfo(m_nMsgLevel, "WSAStartup failed!");
return bRet;
}
//创建套接字
m_sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == m_sServer)
{
ShowMessageInfo(m_nMsgLevel, "socket failed!");
WSACleanup();//释放套接字资源;
return bRet;
}
//服务器套接字地址
SOCKADDR_IN addrServ;
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(m_nPort);
addrServ.sin_addr.s_addr = INADDR_ANY;
//绑定套接字
int retVal = bind(m_sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));
if(SOCKET_ERROR == retVal)
{
ShowMessageInfo(m_nMsgLevel, "bind failed!");
closesocket(m_sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return bRet;
}
//开始监听
retVal = listen(m_sServer, 1);
if(SOCKET_ERROR == retVal)
{
ShowMessageInfo(m_nMsgLevel, "listen failed!");
closesocket(m_sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return bRet;
}
// 创建线程,接收客户端连接
unsigned long pid;
HANDLE hThread = CreateThread(NULL,NULL,Acceptthread,this,CREATE_SUSPENDED,&pid);
if(NULL == hThread)
{
ShowMessageInfo(m_nMsgLevel, "创建ksf1客户端连接线程失败!");
closesocket(m_sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return bRet;
}
ResumeThread(hThread);
return true;
}
// 接收客户端线程
DWORD __stdcall Acceptthread(void *p)
{
CKsf1Server *pKSer;
pKSer = (CKsf1Server *)p;
pKSer->AcceptClient();
return 0;
}
// 接收客户端连接
bool CKsf1Server::AcceptClient()
{
bool bRet = false;
//接受客户端请求
sockaddr_in addrClient;
int addrClientlen = sizeof(addrClient);
while(true)
{
// 退出信号量
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hQuit, 0))
{
ReleaseSemaphore(m_hQuit,1,NULL);
break;
}
// 阻塞模式
SOCKET newClient = accept(m_sServer,(sockaddr FAR*)&addrClient, &addrClientlen);
if(INVALID_SOCKET == newClient)
{
int lastError = WSAGetLastError();
ShowMessageInfo(m_nMsgLevel, "accept failed, lastError=%d", lastError);
Sleep(m_nInterval);
return bRet;
}
char* clientIP = inet_ntoa(addrClient.sin_addr);
ShowMessageInfo(m_nMsgLevel, "accept client success [%s]", clientIP);
// 关掉旧客户端连接
if(INVALID_SOCKET != m_sClient)
closesocket(m_sClient);
// 更新客户端连接
m_sClient = newClient;
}
return true;
}
// 接收客户端请求
bool CKsf1Server::ReceData(char *buf, int len)
{
bool bRet = false;
if (!IsConnect())
{
ShowMessageInfo(m_nMsgLevel, "ReceData failed, IsConnect is false");
return bRet;
}
ZeroMemory(buf, len);
int retVal = recv(m_sClient, buf, len, 0);
// 客户端断开连接
if (retVal <= 0)
{
ShowMessageInfo(m_nMsgLevel, "CKsf1Server ReceData failed, retVal=%d", retVal);
DisConnect();
return bRet;
}
return true;
}
// 发送数据给客户端
bool CKsf1Server::SendData(char *sendBuf, int len)
{
bool bRet = false;
if (!IsConnect())
{
ShowMessageInfo(m_nMsgLevel, "SendData failed, IsConnect is false");
return bRet;
}
if(sendBuf[0] == '\0')
{
ShowMessageInfo(m_nMsgLevel, "SendData failed, sendBuf is null");
return bRet;
}
if (SOCKET_ERROR == send(m_sClient, sendBuf, len, 0))
{
int lastError = WSAGetLastError();
ShowMessageInfo(m_nMsgLevel, "SendData failed, lastError=%d", lastError);
return bRet;
}
return true;
}
#include "Ksf1Server.h"
#include "kufdll.h"
#include <windows.h>
// 接收客户端线程
DWORD __stdcall Acceptthread(void *p);
CKsf1Server::CKsf1Server(void)
{
}
CKsf1Server::~CKsf1Server(void)
{
}
void CKsf1Server::Init(u_short port, int msgLevel, HANDLE hQuit)
{
m_nPort = port;
m_nMsgLevel = msgLevel;
m_sClient = INVALID_SOCKET;
m_sServer = INVALID_SOCKET;
m_hQuit = hQuit;
}
bool CKsf1Server::IsConnect()
{
return (m_sClient != INVALID_SOCKET);
}
void CKsf1Server::DisConnect()
{
if (INVALID_SOCKET != m_sClient)
{
closesocket(m_sClient);
m_sClient = INVALID_SOCKET;
}
}
bool CKsf1Server::Close()
{
// 关闭套接字
if (INVALID_SOCKET != m_sServer)
{
closesocket(m_sServer);
m_sServer = INVALID_SOCKET;
}
if (INVALID_SOCKET != m_sClient)
{
closesocket(m_sClient);
m_sClient = INVALID_SOCKET;
}
// 释放套接字资源
WSACleanup();
return true;
}
bool CKsf1Server::Start()
{
bool bRet = false;
//初始化套结字动态库
WSADATA wsd;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
ShowMessageInfo(m_nMsgLevel, "WSAStartup failed!");
return bRet;
}
//创建套接字
m_sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == m_sServer)
{
ShowMessageInfo(m_nMsgLevel, "socket failed!");
WSACleanup();//释放套接字资源;
return bRet;
}
//服务器套接字地址
SOCKADDR_IN addrServ;
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(m_nPort);
addrServ.sin_addr.s_addr = INADDR_ANY;
//绑定套接字
int retVal = bind(m_sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));
if(SOCKET_ERROR == retVal)
{
ShowMessageInfo(m_nMsgLevel, "bind failed!");
closesocket(m_sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return bRet;
}
//开始监听
retVal = listen(m_sServer, 1);
if(SOCKET_ERROR == retVal)
{
ShowMessageInfo(m_nMsgLevel, "listen failed!");
closesocket(m_sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return bRet;
}
// 创建线程,接收客户端连接
unsigned long pid;
HANDLE hThread = CreateThread(NULL,NULL,Acceptthread,this,CREATE_SUSPENDED,&pid);
if(NULL == hThread)
{
ShowMessageInfo(m_nMsgLevel, "创建ksf1客户端连接线程失败!");
closesocket(m_sServer); //关闭套接字
WSACleanup(); //释放套接字资源;
return bRet;
}
ResumeThread(hThread);
return true;
}
// 接收客户端线程
DWORD __stdcall Acceptthread(void *p)
{
CKsf1Server *pKSer;
pKSer = (CKsf1Server *)p;
pKSer->AcceptClient();
return 0;
}
// 接收客户端连接
bool CKsf1Server::AcceptClient()
{
bool bRet = false;
//接受客户端请求
sockaddr_in addrClient;
int addrClientlen = sizeof(addrClient);
while(true)
{
// 退出信号量
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hQuit, 0))
{
ReleaseSemaphore(m_hQuit,1,NULL);
break;
}
// 阻塞模式
SOCKET newClient = accept(m_sServer,(sockaddr FAR*)&addrClient, &addrClientlen);
if(INVALID_SOCKET == newClient)
{
int lastError = WSAGetLastError();
ShowMessageInfo(m_nMsgLevel, "accept failed, lastError=%d", lastError);
Sleep(m_nInterval);
return bRet;
}
char* clientIP = inet_ntoa(addrClient.sin_addr);
ShowMessageInfo(m_nMsgLevel, "accept client success [%s]", clientIP);
// 关掉旧客户端连接
if(INVALID_SOCKET != m_sClient)
closesocket(m_sClient);
// 更新客户端连接
m_sClient = newClient;
}
return true;
}
// 接收客户端请求
bool CKsf1Server::ReceData(char *buf, int len)
{
bool bRet = false;
if (!IsConnect())
{
ShowMessageInfo(m_nMsgLevel, "ReceData failed, IsConnect is false");
return bRet;
}
ZeroMemory(buf, len);
int retVal = recv(m_sClient, buf, len, 0);
// 客户端断开连接
if (retVal <= 0)
{
ShowMessageInfo(m_nMsgLevel, "CKsf1Server ReceData failed, retVal=%d", retVal);
DisConnect();
return bRet;
}
return true;
}
// 发送数据给客户端
bool CKsf1Server::SendData(char *sendBuf, int len)
{
bool bRet = false;
if (!IsConnect())
{
ShowMessageInfo(m_nMsgLevel, "SendData failed, IsConnect is false");
return bRet;
}
if(sendBuf[0] == '\0')
{
ShowMessageInfo(m_nMsgLevel, "SendData failed, sendBuf is null");
return bRet;
}
if (SOCKET_ERROR == send(m_sClient, sendBuf, len, 0))
{
int lastError = WSAGetLastError();
ShowMessageInfo(m_nMsgLevel, "SendData failed, lastError=%d", lastError);
return bRet;
}
return true;
}