检查一下:
1. 包含头文件:#include <afxsock.h> // MFC socket extensions
2. 初始化:
在应用程序的::InitInstance()里加入如下代码进行初始化
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
1. 包含头文件:#include <afxsock.h> // MFC socket extensions
2. 初始化:
在应用程序的::InitInstance()里加入如下代码进行初始化
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
比如自定义类如下:
class CListenSocket : public CSocket
{
// Attributes
public:
CPtrList m_connectedsocket;// Operations
public:
CListenSocket();
virtual ~CListenSocket();
public:
void CloseSocket(CReposeSocket *m_pSocket);
void CloseAllSockets();
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CListenSocket)
public:
virtual void OnAccept(int nErrorCode);
//}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(CListenSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG// Implementation
protected:
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LISTENSOCKET_H__C99FDD6F_2B00_11D6_9AFD_00E04C485B42__INCLUDED_)再定义一个类的实例
public:
CListenSocket m_listensock;创建socket
if(!m_listensock.Create(1200)) //端口号可自己定义
{
AfxMessageBox("create the socket error!");
return FALSE;
}
m_listensock.Listen(5); //开始帧听
//本例创建了一个服务器socket
这是我得源代码;
CUseSocket的头文件:class CInterfaceDoc;
class CUseSocket : public CSocket
{
// Attributes
public:// Operations
public:
CUseSocket();
virtual ~CUseSocket();// Overrides
public:
void WorkWithArchive();
struct SendPartOS
{
unsigned int hi_point;
unsigned int lo_point;
int dc_value;
};
SendPartOS m_StructSendOS;
BOOL SendContrl(SendPartOS Contrl);
CInterfaceDoc * m_pDoc;
CArchive * m_pArchiveOut; //发送
CArchive * m_pArchiveIn; //接收
CSocketFile * m_pFile; //
//发送内容
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUseSocket)
//}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(CUseSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG实现文件:
// Implementation
protected:
};
CUseSocket::CUseSocket()
{
m_pFile = NULL;
m_pArchiveIn = NULL;
m_pArchiveOut = NULL;
}CUseSocket::~CUseSocket()
{
if(m_pFile != NULL) delete m_pFile;
if(m_pArchiveIn != NULL) delete m_pArchiveIn;
if(m_pArchiveOut != NULL) delete m_pArchiveOut;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CUseSocket, CSocket)
//{{AFX_MSG_MAP(CUseSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/////////////////////////////////////////////////////////////////////////////
// CUseSocket member functionsBOOL CUseSocket::SendContrl(CUseSocket::SendPartOS Contrl)
{
*(m_pArchiveIn) << Contrl.dc_value <<Contrl.hi_point <<Contrl.lo_point;
m_pArchiveIn->Flush();
return true;
}void CUseSocket::WorkWithArchive()
{
m_pFile = new CSocketFile(this);
m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
m_pArchiveOut= new CArchive(m_pFile,CArchive::store);
}
在别的类中使用它(已经建初始化加入到了::InitInstance()中了。
m_pSocket = new CUseSocket();
m_pSocket->m_pDoc = this;
/* m_StrcutSend.dc_value = value;
m_StrcutSend.hi_point = hi_point;
m_StrcutSend.lo_point = lo_point;*/
if(! m_pSocket->Create(1984))
{
int tem ;
if(GetLastError() == WSANOTINITIALISED)tem = 1;
else tem = 0;
delete m_pSocket;
m_pSocket = NULL;
sprintf(tbuf,"%2d",tem);
AfxMessageBox(tbuf);
//AfxMessageBox("创建Socket失败");
}
谢谢
是在不行发给我看看。[email protected]
活干到一半;老板材告诉我用WinSock来做
WSADATA wsaData;
int Status = WSAStartup(0x101,&wsaData);
if(Status != 0)
{
MessageBox("WSAStartup Failed.");
return;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)
{
MessageBox("Incorrect version of Windows Sockets DLL.");
WSACleanup();
return;
}