// Callback.h
#if !defined(CALLBACK_H)
#define CALLBACK_H
class cCallback
{
public:
virtual bool Execute(void *Param) const =0;
};
template <class cInstance>
class TCallback : public cCallback // Inheriting
{
public:
TCallback() // constructor
{
// Important : zeroing the pointer so we later can check for errors
pFunction = 0;
}
// (You can change the callback to take more parameters or to return something)
typedef bool (cInstance::*tFunction)(void *Param);
// Execute the Callback
virtual bool Execute(void *Param) const
{
if (pFunction) return (cInst->*pFunction)(Param);
else printf("ERROR : the callback function has not been defined !!!!");
// an undefined callback function will crash the program if you don't check here !
return false;
}
// Set or change the Callback
void SetCallback (cInstance *cInstancePointer,
tFunction pFunctionPointer)
{
cInst = cInstancePointer;
pFunction = pFunctionPointer;
}
private:
cInstance *cInst;
tFunction pFunction;
};
#endif // CALLBACK_H
-------------------------------------------------------
#pragma once
#include "CallBack.h"
class CWork
{
public:
CWork(void);
~CWork(void);
bool CallbackOutput(void *Param);
TCallback<CWork> Str_CallbackOutput;
};
------------------
#include "stdafx.h"
#include "Work.h"
CWork::CWork(void)
{
Str_CallbackOutput.SetCallback(this, &CWork::CallbackOutput);
}
CWork::~CWork(void)
{
}
bool CWork::CallbackOutput(void *Param)
{
char s8_Out[200];
sprintf(s8_Out, " ***** Some stars ***** %s/n", (char*) Param);
printf(s8_Out);
return true;
}
-----
int InitAndConnet(HWND hwnd,unsigned int WM_WINSOCK,UINT port,CString ip,cCallback *p_CallbackOutput);
cCallback *m_CallbackOutput;
--
int CMySocketClient::InitAndConnet(HWND hwnd,unsigned int WM_WINSOCK,UINT port,CString ip,cCallback *p_CallbackOutput)
{
m_CallbackOutput=p_CallbackOutput;
if (this->InitSocket()!=0)
{
return -1;
}
m_hWnd=hwnd;
m_uPort=port;
m_ip=ip;
m_WM_WINSOCK=WM_WINSOCK;
SOCKET m_Socket = socket(AF_INET,SOCK_STREAM,0);
m_hSocket=m_Socket;
//准备服务器的信息,这里需要指定服务器的地址
m_hostAddr.sin_addr.S_un.S_addr = inet_addr(m_ip); //inet_addr("127.0.0.1");
m_hostAddr.sin_family = AF_INET;
m_hostAddr.sin_port = htons(m_uPort);
//这里主动连接服务器,该过程将等待一定时间
int ret =connect(m_hSocket,(sockaddr*)&m_hostAddr,sizeof(sockaddr));
if(ret == SOCKET_ERROR)
{//连接失败
if(GetLastError()!=WSAEWOULDBLOCK)
{
AfxMessageBox(_T("请确认服务器确实已经打开并工作在同样的端口!"));
return -1;
}
}
if(WSAAsyncSelect(m_hSocket,m_hWnd,m_WM_WINSOCK,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
{
return -1;//AfxMessageBox("Error in select");
}
return 0;
}
//
//int CMySocketClient::InitWSAAsyncSelect()
//{
// if(WSAAsyncSelect(m_hSocket,m_hWnd,m_WM_WINSOCK,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
// {
// return -1 ;//AfxMessageBox("Error in select");
// }
// return 0;
//}
int CMySocketClient::SendString(CString str)
{
if(send(m_hSocket,str.GetBuffer(0),str.GetLength(),0)==SOCKET_ERROR)
{
return -1 ; //AfxMessageBox("Client Send data error");
}
return 0;
}
m_CallbackOutput->Execute((void*)TempPackBytes);
-------------------------
CWork *pwork=new CWork();
m_aa=100;
//m_MySocketClient.InitAndConnet(m_hWnd,WM_CLIENT_READCOLOSE,8888,"127.0.0.1");
m_MySocketClient.InitAndConnet(m_hWnd,WM_CLIENT_READCOLOSE,8888,"127.0.0.1",&pwork->Str_CallbackOutput);