1 回答

TA貢獻1820條經驗 獲得超10個贊
初始化socket
首先需要調用AfxSocketInit()函數來初始化我們的socket環境。
為了初始化sockets,我們需要調用AfxSocketInit()函數。它通常是在MFC中的InitInstance()函數中被調用的。如果我們用程序向導來創建socket程序的話,查看“use Windows Sockets”這個選項,然后選中它。它將會自動的為我們創建這個步驟了。(如果我們沒有選中這個選項的話,我們也可以手動添加這些代碼的。)這個函數的返回值顯示這個函數的調用成功或失敗。
BOOL CServerApp::InitInstance()
{....
if( AfxSocketInit() == FALSE)
{
AfxMessageBox("Sockets Could Not Be Initialized");
return FALSE;
}
...
}
創建Server Sockets
為了創建一個Server Socket,我們需要聲明一個CAyncSocket的變量或者我們自己定制的一個從AyncSocket或是Cscket繼承來的類的類型的變量。然后調用Create()函數,同時指定監聽的端口。這個函數的返回值顯示這個函數的調用成功或失敗。
UpdateData(TRUE);
m_sListener.Create(m_port);
if(m_sListener.Listen()==FALSE)
{
AfxMessageBox("Unable to Listen on that port,please try another port");
m_sListener.Close();
return;
}
創建Client Sockets
為了創建Client socket類,我們需要聲明一個CAyncSocket的變量或者我們自己定制的一個從AyncSocket或是Cscket繼承來的類的類型的變量。然后調用Create()函數,同時指定監聽的端口。這個函數的返回值顯示這個函數的調用成功或失敗。
m_sConnected.Create();
m_sConnected.Connect("server ip",port);
監聽客戶端的連接
創建了server socket以后,我們要進行監聽。調用Listen()函數。這個函數的返回值顯示這個函數的調用成功或失敗。
if( m_sListener.Listen()== FALSE)
{
AfxMessageBox("Unable to Listen on that port,please try another port");
m_sListener.Close();
return;
}
接受連接
連接請求要被接受accept,是用另外的socket,不是正在監聽的socket。請參看代碼。
void CXXXDlg::OnAccept()
{
CString strIP;
UINT port;
if(m_sListener.Accept(m_sConnected))
{
m_sConnected.GetSockName(strIP,port); //應該是GetPeerName,獲取對方的IP和port
m_status="Client Connected,IP :"+ strIP;
m_sConnected.Send("Connected To Server",strlen("Connected To Server"));
UpdateData(FALSE);
}
else
{
AfxMessageBox("Cannoot Accept Connection");
}
}
發送數據
數據放在一個buffer中或是結構體中,調用send()函數發送。
m_sConnected.Send(pBuf,iLen);
接受數據
調用receive()接受數據。
void CXXXrDlg::OnReceive()
{
char *pBuf =new char [1025];
CString strData;
int iLen;
iLen=m_sConnected.Receive(pBuf,1024);
if(iLen == SOCKET_ERROR)
{
AfxMessageBox("Could not Recieve");
}
else
{
pBuf[iLen]=NULL;
strData=pBuf;
m_recieveddata.Insert(m_recieveddata.GetLength(),strData);
//display in server
UpdateData(FALSE);
m_sConnected.Send(pBuf,iLen); //send the data back to the Client
delete pBuf;
}
}
關閉連接
m_sConnected.ShutDown(0); 停止發送數據
m_sConnected.ShutDown(1); 停止接受數據
m_sConnected.ShutDown(2); 停止發送接受數據
m_sConnected.Close();
編寫自己的socket類
在class view中選擇添加一個新類,設置它的基類為CAsyncSocket,在類向導的幫助下添加如下的一些函數。
class MySocket : public CAsyncSocket
{ // Attributes
public:
// Operations
public:
MySocket();
virtual ~MySocket();
// Overrides
public:
void SetParentDlg(CDialog *pDlg);// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(MySocket)
public:
virtual void OnAccept(int nErrorCode);
virtual void OnClose(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnOutOfBandData(int nErrorCode);
virtual void OnReceive(int nErrorCode);
virtual void OnSend(int nErrorCode);
//}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(MySocket)
// NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG
protected:
private:
CDialog * m_pDlg;
};
設置“Parent Dialog”
調用這個socket類的SetParentDlg函數,保證當socket事件發生的時候這個窗體能接收到。
m_sListener.SetParentDlg(this);
m_sConnected.SetParentDlg(this);
建立Socket 事件和窗體成員函數之間的聯系
在這個窗體類中添加一些函數,比如void OnReceive(); void OnClose(); void OnAccept(); void OnConnect()等,它們會在我們編寫的的socket類中調用到。
void MySocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(nErrorCode==0)
{
((CServerDlg*)m_pDlg)->OnAccept();
}
CAsyncSocket::OnAccept(nErrorCode);
}
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報