急,在线等!!!

zyqwk2003 2003-08-30 11:28:48
我是VC++的处学者,想用SOCHET实现网络通信问题,可是一开始就碰到问题,请问
1.怎样以Public方式继承CAsyncSocket类,生成新类MySock;

2.怎样为MySock类添加虚函数OnReceive、OnConnect、OnSend
请好心人具体给我写一写,不胜感激!!!

...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyqwk2003 2003-08-30
  • 打赏
  • 举报
回复
谢谢你写了这么多,但是我想应该不用这么麻烦的把,你的很多代码我都看不懂,不过还是非常感谢!!你能说说你学VC++的体会吗?
wangjidh 2003-08-30
  • 打赏
  • 举报
回复
// blocksock.cpp (CBlockingSocketException, CBlockingSocket, CHttpBlockingSocket)
#include <stdafx.h>
#include "blocksock.h"

// Class CBlockingSocketException
IMPLEMENT_DYNAMIC(CBlockingSocketException, CException)

CBlockingSocketException::CBlockingSocketException(char* pchMessage)
{
m_strMessage = pchMessage;
m_nError = WSAGetLastError();
}

BOOL CBlockingSocketException::GetErrorMessage(LPTSTR lpstrError, UINT nMaxError,
PUINT pnHelpContext /*= NULL*/)
{

char text[200];
if(m_nError == 0) {
wsprintf(text, "%s error", (const char*) m_strMessage);
}
else {
wsprintf(text, "%s error #%d", (const char*) m_strMessage, m_nError);
}
strncpy(lpstrError, text, nMaxError - 1);
return TRUE;
}

// Class CBlockingSocket
IMPLEMENT_DYNAMIC(CBlockingSocket, CObject)

void CBlockingSocket::Cleanup()
{
// doesn't throw an exception because it's called in a catch block
if(m_hSocket == NULL) return;
VERIFY(closesocket(m_hSocket) != SOCKET_ERROR);
m_hSocket = NULL;
}

void CBlockingSocket::Create(int nType /* = SOCK_STREAM */)
{
ASSERT(m_hSocket == NULL);
if((m_hSocket = socket(AF_INET, nType, 0)) == INVALID_SOCKET) {
throw new CBlockingSocketException("Create");
}
}

void CBlockingSocket::Bind(LPCSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
if(bind(m_hSocket, psa, sizeof(SOCKADDR)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Bind");
}
}

void CBlockingSocket::Listen()
{
ASSERT(m_hSocket != NULL);
if(listen(m_hSocket, 5) == SOCKET_ERROR) {
throw new CBlockingSocketException("Listen");
}
}

BOOL CBlockingSocket::Accept(CBlockingSocket& sConnect, LPSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
ASSERT(sConnect.m_hSocket == NULL);
int nLengthAddr = sizeof(SOCKADDR);
sConnect.m_hSocket = accept(m_hSocket, psa, &nLengthAddr);
if(sConnect == INVALID_SOCKET) {
// no exception if the listen was canceled
if(WSAGetLastError() != WSAEINTR) {
throw new CBlockingSocketException("Accept");
}
return FALSE;
}
return TRUE;
}

void CBlockingSocket::Close()
{
if (NULL == m_hSocket)
return;

if(closesocket(m_hSocket) == SOCKET_ERROR) {
// should be OK to close if closed already
throw new CBlockingSocketException("Close");
}
m_hSocket = NULL;
}

void CBlockingSocket::Connect(LPCSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
// should timeout by itself
if(connect(m_hSocket, psa, sizeof(SOCKADDR)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Connect");
}
}

int CBlockingSocket::Write(const char* pch, const int nSize, const int nSecs)
{
int nBytesSent = 0;
int nBytesThisTime;
const char* pch1 = pch;
do {
nBytesThisTime = Send(pch1, nSize - nBytesSent, nSecs);
nBytesSent += nBytesThisTime;
pch1 += nBytesThisTime;
} while(nBytesSent < nSize);
return nBytesSent;
}

int CBlockingSocket::Send(const char* pch, const int nSize, const int nSecs)
{
ASSERT(m_hSocket != NULL);
// returned value will be less than nSize if client cancels the reading
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, NULL, &fd, NULL, &tv) == 0) {
throw new CBlockingSocketException("Send timeout");
}
int nBytesSent;
if((nBytesSent = send(m_hSocket, pch, nSize, 0)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Send");
}
return nBytesSent;
}

int CBlockingSocket::Receive(char* pch, const int nSize, const int nSecs)
{
ASSERT(m_hSocket != NULL);
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, &fd, NULL, NULL, &tv) == 0) {
throw new CBlockingSocketException("Receive timeout");
}

int nBytesReceived;
if((nBytesReceived = recv(m_hSocket, pch, nSize, 0)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Receive");
}
return nBytesReceived;
}

int CBlockingSocket::ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa, const int nSecs)
{
ASSERT(m_hSocket != NULL);
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, &fd, NULL, NULL, &tv) == 0) {
throw new CBlockingSocketException("Receive timeout");
}

// input buffer should be big enough for the entire datagram
int nFromSize = sizeof(SOCKADDR);
int nBytesReceived = recvfrom(m_hSocket, pch, nSize, 0, psa, &nFromSize);
if(nBytesReceived == SOCKET_ERROR) {
throw new CBlockingSocketException("ReceiveDatagram");
}
return nBytesReceived;
}

int CBlockingSocket::SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa, const int nSecs)
{
ASSERT(m_hSocket != NULL);
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, NULL, &fd, NULL, &tv) == 0) {
throw new CBlockingSocketException("Send timeout");
}

int nBytesSent = sendto(m_hSocket, pch, nSize, 0, psa, sizeof(SOCKADDR));
if(nBytesSent == SOCKET_ERROR) {
throw new CBlockingSocketException("SendDatagram");
}
return nBytesSent;
}

void CBlockingSocket::GetPeerAddr(LPSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
// gets the address of the socket at the other end
int nLengthAddr = sizeof(SOCKADDR);
if(getpeername(m_hSocket, psa, &nLengthAddr) == SOCKET_ERROR) {
throw new CBlockingSocketException("GetPeerName");
}
}

void CBlockingSocket::GetSockAddr(LPSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
// gets the address of the socket at this end
int nLengthAddr = sizeof(SOCKADDR);
if(getsockname(m_hSocket, psa, &nLengthAddr) == SOCKET_ERROR) {
throw new CBlockingSocketException("GetSockName");
}
}

//static
CSockAddr CBlockingSocket::GetHostByName(const char* pchName, const USHORT ushPort /* = 0 */)
{
hostent* pHostEnt = gethostbyname(pchName);
if(pHostEnt == NULL) {
throw new CBlockingSocketException("GetHostByName");
}
ULONG* pulAddr = (ULONG*) pHostEnt->h_addr_list[0];
SOCKADDR_IN sockTemp;
sockTemp.sin_family = AF_INET;
sockTemp.sin_port = htons(ushPort);
sockTemp.sin_addr.s_addr = *pulAddr; // address is already in network byte order
return sockTemp;
}

//static
const char* CBlockingSocket::GetHostByAddr(LPCSOCKADDR psa)
{
hostent* pHostEnt = gethostbyaddr((char*) &((LPSOCKADDR_IN) psa)
->sin_addr.s_addr, 4, PF_INET);
if(pHostEnt == NULL) {
throw new CBlockingSocketException("GetHostByAddr");
}
return pHostEnt->h_name; // caller shouldn't delete this memory
}


wangjidh 2003-08-30
  • 打赏
  • 举报
回复
// blocksock.h

// needs winsock.h in the precompiled headers

typedef const struct sockaddr* LPCSOCKADDR;

class CBlockingSocketException : public CException
{
DECLARE_DYNAMIC(CBlockingSocketException)
public:
// Constructor
CBlockingSocketException(char* pchMessage);

public:
~CBlockingSocketException() {}
virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError,
PUINT pnHelpContext = NULL);
int m_nError;
private:
CString m_strMessage;
};

extern void LogBlockingSocketException(LPVOID pParam, char* pch, CBlockingSocketException* pe);

class CSockAddr : public sockaddr_in {
public:
// constructors
CSockAddr()
{ sin_family = AF_INET;
sin_port = 0;
sin_addr.s_addr = 0; } // Default
CSockAddr(const SOCKADDR& sa) { memcpy(this, &sa, sizeof(SOCKADDR)); }
CSockAddr(const SOCKADDR_IN& sin) { memcpy(this, &sin, sizeof(SOCKADDR_IN)); }
CSockAddr(const ULONG ulAddr, const USHORT ushPort = 0) // parms are host byte ordered
{ sin_family = AF_INET;
sin_port = htons(ushPort);
sin_addr.s_addr = htonl(ulAddr); }
CSockAddr(const char* pchIP, const USHORT ushPort = 0) // dotted IP addr string
{ sin_family = AF_INET;
sin_port = htons(ushPort);
sin_addr.s_addr = inet_addr(pchIP); } // already network byte ordered
// Return the address in dotted-decimal format
CString DottedDecimal()
{ return inet_ntoa(sin_addr); } // constructs a new CString object
// Get port and address (even though they're public)
USHORT Port() const
{ return ntohs(sin_port); }
ULONG IPAddr() const
{ return ntohl(sin_addr.s_addr); }
// operators added for efficiency
const CSockAddr& operator=(const SOCKADDR& sa)
{ memcpy(this, &sa, sizeof(SOCKADDR));
return *this; }
const CSockAddr& operator=(const SOCKADDR_IN& sin)
{ memcpy(this, &sin, sizeof(SOCKADDR_IN));
return *this; }
operator SOCKADDR()
{ return *((LPSOCKADDR) this); }
operator LPSOCKADDR()
{ return (LPSOCKADDR) this; }
operator LPSOCKADDR_IN()
{ return (LPSOCKADDR_IN) this; }
};

// member functions truly block and must not be used in UI threads
// use this class as an alternative to the MFC CSocket class
class CBlockingSocket : public CObject
{
DECLARE_DYNAMIC(CBlockingSocket)
public:
SOCKET m_hSocket;
CBlockingSocket() { m_hSocket = NULL; }
void Cleanup();
void Create(int nType = SOCK_STREAM);
void Close();
void Bind(LPCSOCKADDR psa);
void Listen();
void Connect(LPCSOCKADDR psa);
BOOL Accept(CBlockingSocket& s, LPSOCKADDR psa);
int Send(const char* pch, const int nSize, const int nSecs);
int Write(const char* pch, const int nSize, const int nSecs);
int Receive(char* pch, const int nSize, const int nSecs);
int SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa,
const int nSecs);
int ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa,
const int nSecs);
void GetPeerAddr(LPSOCKADDR psa);
void GetSockAddr(LPSOCKADDR psa);
static CSockAddr GetHostByName(const char* pchName,
const USHORT ushPort = 0);
static const char* GetHostByAddr(LPCSOCKADDR psa);
operator SOCKET()
{ return m_hSocket; }
};

18,355

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧