DASocket.cpp
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:13k
- // DASocket.cpp: implementation of the CDASrvCnSocket class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "DASocket.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- /////////////////////////////////////////////////////////////////////////////
- //网络常用操作函数
- int GetLocalHostName(CString &sHostName)
- {
- char szHostName[256];
- int nRetCode;
- nRetCode=gethostname(szHostName,sizeof(szHostName));
- if(nRetCode!=0)
- {
- //产生错误
- sHostName=_T("没有取得本地用户名");
- return GetLastError();
- }
- sHostName=szHostName;
- return 0;
- }
- int GetIpAddress(const CString &sHostName, CString &sIpAddress)//获得本地IP
- {
- struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
- if(lpHostEnt==NULL)
- {
- //产生错误
- sIpAddress=_T("");
- return GetLastError();
- }
- //获取IP
- LPSTR lpAddr=lpHostEnt->h_addr_list[0];
- if(lpAddr)
- {
- struct in_addr inAddr;
- memmove(&inAddr,lpAddr,4);
- //转换为标准格式
- sIpAddress=inet_ntoa(inAddr);
- if(sIpAddress.IsEmpty())
- sIpAddress=_T("没有取得本地IP地址");
- }
- return 0;
- }
- int GetNamebyAddress(const CString &IpAddress,CString &sYouName)//获得对方计算机名称
- {
- unsigned long addr;
- addr=inet_addr(IpAddress);
- struct hostent FAR * lpHostEnt=gethostbyaddr((char *)&addr,4,AF_INET);
- if(lpHostEnt==NULL)
- {
- //产生错误
- sYouName=_T("");
- AfxMessageBox("连接不上");//应该取得其错误
- return -1;
- }
- CString name=lpHostEnt->h_name;
- sYouName=name;
- return 0;
- }
- CString GetNetError(DWORD error) //返回错误信息
- {
- CString strError;
- switch(error)
- {
- case WSANOTINITIALISED:
- strError="初始化错误";
- break;
- case WSAENOTCONN:
- strError="对方没有启动";
- break;
- case WSAEWOULDBLOCK :
- strError="对方已经关闭";
- break;
- case WSAECONNREFUSED:
- strError="连接的尝试被拒绝";
- break;
- case WSAENOTSOCK:
- strError="在一个非套接字上尝试了一个操作";
- break;
- case WSAEADDRINUSE:
- strError="特定的地址已在使用中";
- break;
- case WSAECONNRESET:
- strError="与主机的连接被关闭";
- break;
- default:
- strError="一般错误";
- }
- return strError;
- }
- /////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- // CDASrvCnSocket
- CDASrvCnSocket::CDASrvCnSocket(CDAServerSocket* pOwner)
- {
- m_pOwner = pOwner;
- m_pEvtReceive = NULL;
- m_pEvtSend = NULL;
- m_pEvtOutOfBandData = NULL;
- m_pEvtAccept = NULL;
- m_pEvtConnect = NULL;
- m_pEvtClose = NULL;
- }
- CDASrvCnSocket::~CDASrvCnSocket()
- {
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- #if 0
- BEGIN_MESSAGE_MAP(CDASrvCnSocket, CAsyncSocket)
- //{{AFX_MSG_MAP(CDASrvCnSocket)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- #endif // 0
- /////////////////////////////////////////////////////////////////////////////
- // CDASrvCnSocket member functions
- void CDASrvCnSocket::OnReceive(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(m_pEvtReceive)
- {
- (m_pOwner->*m_pEvtReceive)(this, nErrorCode);
- }
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnReceive(nErrorCode);
- }
- void CDASrvCnSocket::OnClose(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(m_pEvtClose)
- (m_pOwner->*m_pEvtClose)(this, nErrorCode);
- CAsyncSocket::OnClose(nErrorCode);
- }
- void CDASrvCnSocket::OnConnect(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(m_pEvtConnect)
- {
- (m_pOwner->*m_pEvtConnect)(this, nErrorCode);
- }
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnConnect(nErrorCode);
- }
- void CDASrvCnSocket::OnOutOfBandData(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(m_pEvtOutOfBandData)
- (m_pOwner->*m_pEvtOutOfBandData)(this, nErrorCode);
- CAsyncSocket::OnOutOfBandData(nErrorCode);
- }
- void CDASrvCnSocket::OnSend(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(m_pEvtSend)
- {
- (m_pOwner->*m_pEvtSend)(this, nErrorCode);
- }
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnSend(nErrorCode);
- }
- BOOL CDASrvCnSocket::SendData(char *pSendBuffer, int nBufLen, int& dwBytesSended)
- {
- if(nBufLen < 0) return FALSE;
- int iRemainBytes = nBufLen;
- int iTotalSentBytes = 0;
- int iCurSent = 0;
- while(iRemainBytes)
- {
- iCurSent = Send(pSendBuffer + iTotalSentBytes,iRemainBytes);
- if(iCurSent < 0)
- {
- TRACE("Send Error!n");
- return FALSE;
- }
- iTotalSentBytes += iCurSent;
- iRemainBytes -= iCurSent;
- }
- dwBytesSended=iTotalSentBytes;
- return TRUE;
- }
- BOOL CDASrvCnSocket::RecvData(char *pRecvBuffer, int nBufLen, int& dwBytesReaded)
- {
- if(nBufLen < 0) return FALSE;
- int iRemainBytes = nBufLen;
- int iTotalRecvBytes = 0;
- int iCurRecv = 0;
- while(iRemainBytes)
- {
- iCurRecv = Receive(pRecvBuffer + iTotalRecvBytes,iRemainBytes);
- if(iCurRecv < 0)
- {
- TRACE("Receive Error!n");
- return FALSE;
- }
- iTotalRecvBytes += iCurRecv;
- iRemainBytes -= iCurRecv;
- }
- dwBytesReaded=(DWORD)iTotalRecvBytes;
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDAServerSocket
- CDAServerSocket::CDAServerSocket(HWND pOwner)
- {
- m_hOwner = pOwner;
- m_bHasOpened = FALSE;
- m_bConnected = FALSE;
- }
- CDAServerSocket::~CDAServerSocket()
- {
- for(int i = 0; i < m_arConnections.GetSize(); i++) //释放所有连接
- {
- delete m_arConnections[i];
- }
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- #if 0
- BEGIN_MESSAGE_MAP(CDAServerSocket, CAsyncSocket)
- //{{AFX_MSG_MAP(CDAServerSocket)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- #endif // 0
- BOOL CDAServerSocket::OpenPort(UINT nSocketPort, const CString szAddr)
- {
- m_nSocketPort = nSocketPort;
- m_szAddr = szAddr;
- AfxSocketInit();
- if(!Create(nSocketPort))//, SOCK_STREAM, FD_ACCEPT))//, szAddr))
- {
- return FALSE;
- }
- if(!Listen())
- {
- Close();
- return FALSE;
- }
- m_bHasOpened = TRUE;
- return TRUE;
- }
- void CDAServerSocket::ClosePort()
- {
- CDASrvCnSocket* pSrvCnSocket = NULL;
- Close();
- m_bConnected = FALSE;
- for(int i = 0; i < m_arConnections.GetSize(); i++)
- {
- pSrvCnSocket = m_arConnections.ElementAt(i);
- delete pSrvCnSocket;
- pSrvCnSocket = NULL;
- }
- m_arConnections.RemoveAll();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDAServerSocket member functions
- void CDAServerSocket::OnAccept(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(!nErrorCode)
- {
- CDASrvCnSocket* pcnSocket;
- SOCKADDR_IN client;
- int iAddrSize=sizeof(client);
- pcnSocket = new CDASrvCnSocket(this);
- if(Accept(*pcnSocket,(SOCKADDR *)&client,&iAddrSize))
- {
- pcnSocket->AsyncSelect(FD_READ|FD_WRITE|FD_CLOSE);
- pcnSocket->m_pEvtReceive = ReceiveEvt;
- pcnSocket->m_pEvtSend = SendEvt;
- pcnSocket->m_pEvtClose = CloseEvt;
- pcnSocket->m_strRemoIP = inet_ntoa(client.sin_addr);
- pcnSocket->m_nRemoPort = client.sin_port;
- m_arConnections.Add(pcnSocket);
- m_bConnected = TRUE;
- SendMessage(m_hOwner, WM_TCP_SERVER_CONNECTED, (WPARAM)nErrorCode, (LPARAM)pcnSocket);
- }
- else
- {
- delete pcnSocket;
- pcnSocket = NULL;
- }
- }
- AsyncSelect(FD_ACCEPT|FD_CLOSE);
- CAsyncSocket::OnAccept(nErrorCode);
- }
- void CDAServerSocket::ReceiveEvt(CAsyncSocket* pSrvCnSocket, int nErrorCode)
- {
- SendMessage(m_hOwner, WM_TCP_SERVER_RECEIVE, (WPARAM)nErrorCode, (LPARAM)pSrvCnSocket);
- }
- void CDAServerSocket::SendEvt(CAsyncSocket* pSrvCnSocket, int nErrorCode)
- {
- SendMessage(m_hOwner, WM_TCP_SERVER_SEND, (WPARAM)nErrorCode, (LPARAM)pSrvCnSocket);
- }
- void CDAServerSocket::CloseEvt(CAsyncSocket* pSrvCnSocket, int nErrorCode)
- {
- SendMessage(m_hOwner, WM_TCP_SERVER_CLOSED, (WPARAM)nErrorCode, (LPARAM)pSrvCnSocket);
- for(int i = 0; i < m_arConnections.GetSize(); i++)
- {
- if(m_arConnections[i]->m_hSocket == pSrvCnSocket->m_hSocket)
- {
- delete pSrvCnSocket;
- pSrvCnSocket = NULL;
- m_arConnections.RemoveAt(i);
-
- if(m_arConnections.GetSize() == 0)
- {
- m_bConnected = FALSE;
- }
- return;
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDAClientSocket
- CDAClientSocket::CDAClientSocket(HWND pOwner)
- {
- m_pOwner = pOwner;
- m_bHasOpened = FALSE;
- m_bConnected = FALSE;
- }
- CDAClientSocket::~CDAClientSocket()
- {
- if(m_hSocket != INVALID_SOCKET) Close();
- }
- BOOL CDAClientSocket::OpenPort(UINT nHostPort, const CString szHostAddr)
- {
- m_nHostPort = nHostPort;
- m_szHostAddr = szHostAddr;
- if(m_hSocket != INVALID_SOCKET)
- {
- Close();
- }
- if(!Create(0,SOCK_STREAM,FD_READ |FD_CONNECT | FD_CLOSE))
- {
- m_bHasOpened = FALSE;
- m_bConnected = FALSE;
- return FALSE;
- }
- Connect(szHostAddr, nHostPort);
- return TRUE;
- }
- void CDAClientSocket::ClosePort()
- {
- Close();
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- #if 0
- BEGIN_MESSAGE_MAP(CDAClientSocket, CAsyncSocket)
- //{{AFX_MSG_MAP(CDAClientSocket)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- #endif // 0
- /////////////////////////////////////////////////////////////////////////////
- // CDAClientSocket member functions
- void CDAClientSocket::OnConnect(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(!nErrorCode)
- {
- m_bHasOpened = TRUE;
- m_bConnected = TRUE;
- SendMessage(m_pOwner, WM_TCP_CLIENT_CONNECTED, (WPARAM)nErrorCode, (LPARAM)this);
- }
- else
- AfxMessageBox("无法连接指定的服务器网络端口,请重试!");
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnConnect(nErrorCode);
- }
- void CDAClientSocket::OnReceive(int nErrorCode)
- {
- SendMessage(m_pOwner, WM_TCP_CLIENT_RECEIVE, (WPARAM)nErrorCode, (LPARAM)this);
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnReceive(nErrorCode);
- }
- void CDAClientSocket::OnSend(int nErrorCode)
- {
- SendMessage(m_pOwner, WM_TCP_CLIENT_SEND, (WPARAM)nErrorCode, (LPARAM)this);
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnSend(nErrorCode);
- }
- void CDAClientSocket::OnClose(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- m_bHasOpened = FALSE;
- m_bConnected = FALSE;
- SendMessage(m_pOwner, WM_TCP_CLIENT_CLOSED, (WPARAM)nErrorCode, (LPARAM)this);
- CAsyncSocket::OnClose(nErrorCode);
-
- }
- BOOL CDAClientSocket::SendData(char *pSendBuffer, int nBufLen)
- {
- if(nBufLen < 0) return FALSE;
- int iRemainBytes = nBufLen;
- int iTotalSentBytes = 0;
- int iCurSent = 0;
- while(iRemainBytes)
- {
- iCurSent = Send(pSendBuffer + iTotalSentBytes,iRemainBytes);
- if(iCurSent < 0)
- {
- TRACE("Send Error!n");
- return FALSE;
- }
- iTotalSentBytes += iCurSent;
- iRemainBytes -= iCurSent;
- }
- return TRUE;
- }
- BOOL CDAClientSocket::RecvData(char *pRecvBuffer, int nBufLen)
- {
- if(nBufLen < 0) return FALSE;
- int iRemainBytes = nBufLen;
- int iTotalRecvBytes = 0;
- int iCurRecv = 0;
- while(iRemainBytes)
- {
- iCurRecv = Receive(pRecvBuffer + iTotalRecvBytes,iRemainBytes);
- if(iCurRecv < 0)
- {
- TRACE("Receive Error!n");
- return FALSE;
- }
- iTotalRecvBytes += iCurRecv;
- iRemainBytes -= iCurRecv;
- }
- return TRUE;
- }
- ///////////////////////////////////////
- ///////////////////////////////////////
- CDAUDPSocket::CDAUDPSocket(HWND pOwner)
- {
- m_pOwner = pOwner;
- m_bHasOpened = FALSE;
- }
- CDAUDPSocket::~CDAUDPSocket()
- {
- if(m_hSocket != INVALID_SOCKET) Close();
- }
- BOOL CDAUDPSocket::OpenPort(UINT nLocalPort)
- {
- m_nLocalPort = nLocalPort;
- if(m_hSocket != INVALID_SOCKET)
- {
- Close();
- }
- if(!Create(nLocalPort,SOCK_DGRAM,FD_READ |FD_CONNECT | FD_CLOSE))
- {
- m_bHasOpened = FALSE;
- return FALSE;
- }
- m_bHasOpened = TRUE;
- return TRUE;
- }
- void CDAUDPSocket::ClosePort()
- {
- Close();
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- #if 0
- BEGIN_MESSAGE_MAP(CDAUDPSocket, CAsyncSocket)
- //{{AFX_MSG_MAP(CDAUDPSocket)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- #endif // 0
- void CDAUDPSocket::OnReceive(int nErrorCode)
- {
- SendMessage(m_pOwner, WM_UDP_RECEIVE, (WPARAM)nErrorCode, (LPARAM)this);
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnReceive(nErrorCode);
- }
- void CDAUDPSocket::OnSend(int nErrorCode)
- {
- SendMessage(m_pOwner, WM_UDP_SEND, (WPARAM)nErrorCode, (LPARAM)this);
- AsyncSelect(FD_READ | FD_CLOSE);
- CAsyncSocket::OnSend(nErrorCode);
- }
- void CDAUDPSocket::OnClose(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- m_bHasOpened = FALSE;
- SendMessage(m_pOwner, WM_UDP_CLOSED, (WPARAM)nErrorCode, (LPARAM)this);
- CAsyncSocket::OnClose(nErrorCode);
-
- }