如何把WINSOCK 加到atl工程去?
问题:
我编写一个短信网关,用了一个以下用了HTTP请求的BlockSock类。现要加入FTP上传功能,并编写了一个FPT(使用CSocket)类,在MFC APP程序中使用winsock运行就正常。但把头文件加到现有的ATL工程里,一创建CSocket 实例就报错。请问应如何处理呢?或是要把什么头文件加上去呢?
请各位高人帮忙。谢谢!!!!BlockSock类
typedef const struct sockaddr* LPCSOCKADDR;
//定义一个意外处理的类
class CBlockingSocketException : public CException
{
DECLARE_DYNAMIC(CBlockingSocketException)
public:
CBlockingSocketException(char* pchMessage);public:
~CBlockingSocketException() {}
virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError,
PUINT pnHelpContext = NULL);
virtual int GetErrorCode();
private:
int m_nError;
CString m_strMessage;
};
//一个地址结构的类
class CSockAddr : public sockaddr_in {
public:
//重载的一些构函数
CSockAddr()
{ sin_family = AF_INET;
  sin_port = 0;
  sin_addr.s_addr = 0; } 

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) 
{ sin_family = AF_INET;
  sin_port = htons(ushPort);
      sin_addr.s_addr = htonl(ulAddr); }

CSockAddr(const char* pchIP, const USHORT ushPort = 0) 
{ sin_family = AF_INET;
  sin_port = htons(ushPort);
  sin_addr.s_addr = inet_addr(pchIP); } 

//相关的一些操作,包含一些运算符的重载
CString DottedDecimal()
{ return inet_ntoa(sin_addr); } 
USHORT Port() const
{ return ntohs(sin_port); }
ULONG IPAddr() const
{ return ntohl(sin_addr.s_addr); }
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; }
};//一个阻塞通信的socket类
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 flags=0);
int Write(const char* pch, const int nSize, const int nSecs,int flags=0);
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);
int GetSockOpt(int level,int optname,char FAR* optval,int FAR*  optlen);
int SetSockOpt(int level,int optname,const char FAR * optval,int optlen);
operator SOCKET()
{ return m_hSocket; }
};//proxy type
#define PROXYTYPE_NOPROXY 0 //不进行代理
#define PROXYTYPE_SOCKS4 1 //SOCK4代理,不需要验证密码
#define PROXYTYPE_SOCKS5 2 //SOCK5代理,需要验证密码
class CProxySocket: public CBlockingSocket
{
DECLARE_DYNAMIC(CProxySocket)
public:
 CProxySocket();
virtual ~CProxySocket();
virtual void Connect(LPCSOCKADDR psa);public:
void SetProxy(int nProxyType,char *ProxyHost,USHORT ProxyPort,char *ProxyUser=NULL,char *ProxyPass=NULL,BOOL bUseSocks5Logon=FALSE);

protected:
int m_nProxyType;
char m_ProxyHost[128];
USHORT   m_nProxyPort;
char  m_ProxyUser[128];
char  m_ProxyPass[128];
BOOL  m_bUseSocks5Logon;
};class CHttpSocket : public CProxySocket
{
public:
DECLARE_DYNAMIC(CHttpSocket)
enum {nSizeRecv = 1000}; 
CHttpSocket();
~CHttpSocket();
int ReadHttpHeaderLine(char* pch, const int nSize, const int nSecs);
int ReadHttpResponse(char* pch, const int nSize, const int nSecs);
private:
char* m_pReadBuf; 
int m_nReadBuf; 
};FTP类#if !defined(AFX_FTPCLIENT_H__05003AE0_E234_11D2_970B_00A024EBF6AB__INCLUDED_)
#define AFX_FTPCLIENT_H__05003AE0_E234_11D2_970B_00A024EBF6AB__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CFTPclient
{
public:
CString m_retmsg;
BOOL MoveFile(CString remotefile, CString localfile,BOOL pasv,BOOL get);
void LogOffServer();
BOOL LogOnToServer(CString hostname,int hostport,CString username, CString password, CString acct, CString fwhost,CString fwusername, CString fwpassword,int fwport,int logontype);
CFTPclient();
~CFTPclient();
BOOL FTPcommand(CString command);
BOOL ReadStr();
BOOL WriteStr(CString outputstring);private:
CArchive* m_pCtrlRxarch;
CArchive* m_pCtrlTxarch;
CSocketFile* m_pCtrlsokfile;
CSocket* m_Ctrlsok;
int m_fc;
BOOL ReadStr2();
BOOL OpenControlChannel(CString serverhost,int serverport);
void CloseControlChannel();protected:};

解决方案 »

  1.   

    有人帮忙吗?
    CSocket* m_Ctrlsok;
    m_Ctrlsok->create()报错。如果不用CSocket在ATL的COM接口用还可以怎么实现FTP功能?
      

  2.   

    you should add mfc support to atl project .see 
    HOWTO: Add MFC Support to an ATL Project Q173974
    or you use socket api replace with CSocket
      

  3.   

    CSocket是MFC的东西,FTP可以不使用CSocket的,直接用WINSOCK 的API吧!!!
      

  4.   

    问题解决了。kingzai(stevenzhu) 说得差不多。我加了MFC到ATL里但在ATL中未初始化CSOCKET所以在创建时就报错BOOL CSendmsgApp::InitInstance()
    {
    WORD wVersionRequested;
    WSADATA wsaData; wVersionRequested = MAKEWORD(1,1);
    if(!AfxSocketInit())
    {
    AfxMessageBox("Could not initialize Windows Sockets!");
    return FALSE;
    } if (WSAStartup(wVersionRequested, &wsaData)) 
    {
    MessageBox(NULL,"无法启动WinSock","初始化错误",0);
    return FALSE;
    }
        _Module.Init(ObjectMap, m_hInstance, &LIBID_SENDMSGLib);
        return CWinApp::InitInstance();
    }
    谢谢各位的帮忙。