QQSocket.cpp
上传用户:hysujiao87
上传日期:2007-12-02
资源大小:156k
文件大小:3k
- // ServerSocket.cpp : implementation file
- //
- #include "stdafx.h"
- #include "QQSocket.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CQQSocket
- CQQSocket::CQQSocket()
- {
- }
- CString CQQSocket::IPAddrToString(const sockaddr_in &addr)
- {
- CString result;
- result.Format("%d.%d.%d.%d", addr.sin_addr.S_un.S_un_b.s_b1,
- addr.sin_addr.S_un.S_un_b.s_b2,
- addr.sin_addr.S_un.S_un_b.s_b3,
- addr.sin_addr.S_un.S_un_b.s_b4);
- return result;
- }
- HRESULT CQQSocket::stringToIPAddr(LPCTSTR serverAddr, sockaddr_in &addr)
- {
- _ASSERTE(serverAddr != NULL);
- if (serverAddr == NULL)
- return E_INVALIDARG;
- CString addrString = serverAddr;
- int pos = 0;
-
- int S_addr_b[4] = {0, 0, 0, 0};
- for(int i = 0; i < 4; i++)
- {
- int oldPos = pos;
- pos = addrString.Find(_T('.'), pos);
- if (pos == -1)
- {
- if (i < 3)
- return E_FAIL;
- else
- pos = addrString.GetLength();
- }
- CString field = addrString.Mid(oldPos, pos - oldPos);
- S_addr_b[i] = _ttoi(field);
- pos++;
- }
- ::ZeroMemory(&addr, sizeof(sockaddr_in));
- addr.sin_addr.S_un.S_un_b.s_b1 = (BYTE)S_addr_b[0];
- addr.sin_addr.S_un.S_un_b.s_b2 = (BYTE)S_addr_b[1];
- addr.sin_addr.S_un.S_un_b.s_b3 = (BYTE)S_addr_b[2];
- addr.sin_addr.S_un.S_un_b.s_b4 = (BYTE)S_addr_b[3];
- addr.sin_family = AF_INET;
-
- return S_OK;
- }
- CQQSocket::~CQQSocket()
- {
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- /*BEGIN_MESSAGE_MAP(CQQSocket, CSocket)
- //{{AFX_MSG_MAP(CQQSocket)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()*/
- /////////////////////////////////////////////////////////////////////////////
- // CQQSocket member functions
- BOOL CQQSocket::create(short port)
- {
- return CSocket::Create(port, SOCK_DGRAM);
- }
- HRESULT CQQSocket::sendToHost(SOCKADDR *addrHost, BYTE *buffer, INT_PTR bufferSize)
- {
- _ASSERTE(addrHost != NULL && buffer != NULL);
- if (addrHost == NULL || buffer == NULL)
- return E_INVALIDARG;
- int res = SendTo(buffer,
- bufferSize,
- addrHost,
- sizeof(sockaddr_in));
- if(res == SOCKET_ERROR)
- {
- int error = WSAGetLastError();
- TRACE("ERROR: Failed to send the data to the host (%d).n", error);
- return E_FAIL;
- }
- return S_OK;
- }
- BOOL CQQSocket::receive(void)
- {
- DWORD dataInBuffer = 0;
- BOOL ret = CAsyncSocket::IOCtl(FIONREAD, &dataInBuffer);
- if (ret != TRUE)
- {
- DWORD error = GetLastError();
- TRACE("IOCtl() failed (%d)n", error);
- return FALSE;
- }
- if( dataInBuffer == 0 )
- return FALSE ;
- _receivedIn.RemoveAll();
- _receivedIn.SetSize(dataInBuffer);
- BYTE *receiveBuffer = _receivedIn.GetData();
- _ASSERTE(receiveBuffer != NULL);
- if (receiveBuffer == NULL)
- return E_UNEXPECTED;
- int addrLen = sizeof(sockaddr_in);
- ::ZeroMemory(&_addrFrom, addrLen);
- int actualReceived = ReceiveFrom(receiveBuffer,
- dataInBuffer,
- (SOCKADDR*)&_addrFrom,
- &addrLen);
- if (actualReceived == SOCKET_ERROR)
- {
- DWORD error = GetLastError();
- TRACE("Failed to receive the response (%d).n", error);
- return FALSE ;
- }
- /*
- TRACE("MESSAGE: Received %d bytes from %3d.%3d.%3d.%3dn", actualReceived,
- _addrFrom.sin_addr.S_un.S_un_b.s_b1,
- _addrFrom.sin_addr.S_un.S_un_b.s_b2,
- _addrFrom.sin_addr.S_un.S_un_b.s_b3,
- _addrFrom.sin_addr.S_un.S_un_b.s_b4);
- */
- return TRUE;
- }