?? socketserver.cpp
字號:
// SocketServer.cpp : implementation file
#include "stdafx.h"
#include "NetPhone.h"
#include "SocketServer.h"
#include "NetPhoneDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_NC 1001
extern CSocketServer Socket_Server;
extern CSocketServer Socket_Listen;
extern CNetPhoneDlg *pDlg;
extern BOOL bBtnConnectDown;
extern BOOL bServerState;
extern BOOL bClientState;
extern BOOL bDisconnectState;
extern BOOL bMiniState;
extern CString sRemoteIP;
extern CString sAck;
extern char cAck[15];
// CSocketServer
CSocketServer::CSocketServer()
{
}
CSocketServer::~CSocketServer()
{
Close();
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CSocketServer, CAsyncSocket)
//{{AFX_MSG_MAP(CSocketServer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
// FD_ACCEPT網絡事件處理函數,收到連接請求時發生
void CSocketServer::OnAccept(int nErrorCode)
{
// Socket_Server套接字接受連接請求
Accept(Socket_Server);
// 錯誤信息處理
if (0 != nErrorCode)
{
switch( nErrorCode) //nErrorCode 為錯誤碼
{
case WSANOTINITIALISED:
AfxMessageBox("A successful AfxSocketInit must occur before using this API.\n");
break;
case WSAENETDOWN:
AfxMessageBox("The Windows Sockets implementation detected that the network subsystem failed.\n");
break;
case WSAEFAULT:
AfxMessageBox("The lpSockAddrLen argument is too small.\n");
break;
case WSAEINPROGRESS:
AfxMessageBox("A blocking Windows Sockets call is in progress.\n");
break;
case WSAEINVAL:
AfxMessageBox("Listen was not invoked prior to accept.\n");
break;
case WSAEMFILE:
AfxMessageBox("The queue is empty upon entry to accept and there are no descriptors available.\n");
break;
case WSAENOBUFS:
AfxMessageBox("No buffer space is available.\n");
break;
case WSAENOTSOCK:
AfxMessageBox("The descriptor is not a socket.\n");
break;
case WSAEOPNOTSUPP:
AfxMessageBox("The referenced socket is not a type that supports connection-oriented service.\n");
break;
case WSAEWOULDBLOCK:
AfxMessageBox("The socket is marked as nonblocking and no connections are present to be accepted. \n");
break;
default:
TCHAR szError[256];
wsprintf(szError, "OnAccept error: %d", nErrorCode);
AfxMessageBox(szError,MB_ICONINFORMATION | MB_OK,NULL);
break;
}
}
// 若是被呼叫端,“連接”按鈕未被按下,即bBtnConnectDown=FALSE
// 收到連接請求時,播放鈴聲并顯示通知信息通知被呼叫端用戶
if(bBtnConnectDown==FALSE)
{
UINT RemotePort=5000;
// 得到呼叫端IP地址及端口
Socket_Server.GetPeerName(sRemoteIP,RemotePort);
// 播放鈴聲
PlaySound("PhoneIn.wav",NULL,SND_SYNC);
// 設置各個按鈕狀態
pDlg->GetDlgItem(IDC_BUTTON_COMMUNICATE)->EnableWindow(TRUE) ;
pDlg->GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(TRUE) ;
pDlg->SetDlgItemText(IDC_BUTTON_DISCONNECT,"拒 接");
// 在對話框IDC_STATIC_INFORMATION控件中顯示通知信息
::SetDlgItemText(pDlg->m_hWnd,IDC_STATIC_INFORMATION,sRemoteIP+"有電話呼叫您");
}
// 程序是否處于最小化狀態,是的話,最大化程序窗口,通知用戶有呼叫進入
if(bMiniState==TRUE)
{
::SendMessage(pDlg->m_hWnd,WM_NC,0,WM_LBUTTONDBLCLK);
}
CAsyncSocket::OnAccept(nErrorCode);
}
// FD_READ網絡事件處理函數,有數據到達時發生
void CSocketServer::OnReceive(int nErrorCode)
{
// 錯誤信息處理
if (0 != nErrorCode)
{
switch( nErrorCode) //nErrorCode 為錯誤碼
{
case WSANOTINITIALISED:
AfxMessageBox("A successful AfxSocketInit must occur before using this API.\n");
break;
case WSAENETDOWN:
AfxMessageBox("The Windows Sockets implementation detected that the network subsystem failed.\n");
break;
case WSAENOTCONN:
AfxMessageBox("The socket is not connected.\n");
break;
case WSAEINPROGRESS:
AfxMessageBox("A blocking Windows Sockets operation is in progress.\n");
break;
case WSAENOTSOCK:
AfxMessageBox("The descriptor is not a socket.\n");
break;
case WSAEOPNOTSUPP:
AfxMessageBox("MSG_OOB was specified, but the socket is not of type SOCK_STREAM.\n");
break;
case WSAESHUTDOWN:
AfxMessageBox("The socket has been shut down. \n");
break;
case WSAEWOULDBLOCK:
AfxMessageBox("The socket is marked as nonblocking and the Receive operation would block.\n");
break;
case WSAEMSGSIZE:
AfxMessageBox("The datagram was too large to fit into the specified buffer and was truncated.\n");
break;
case WSAEINVAL:
AfxMessageBox("The socket has not been bound with Bind.\n");
break;
case WSAECONNABORTED:
AfxMessageBox("The virtual circuit was aborted due to timeout or other failure.\n");
break;
case WSAECONNRESET:
AfxMessageBox("The virtual circuit was reset by the remote side. \n");
break;
default:
TCHAR szError[256];
wsprintf(szError, "OnReceive error: %d", nErrorCode);
AfxMessageBox(szError);
break;
}
}
// 若是呼叫端,則“連接”按鈕被按下,自動進入客戶端狀態,
// 即bBtnConnectDown=TRUE且bClientState=TRUE
// 接收到來自被呼叫端的應答信息,判斷是否電話被接聽
if(bBtnConnectDown==TRUE&&bClientState==TRUE&&bServerState==FALSE)
{
// 接收15個字節信息
Receive(cAck,15);
sAck.Format("%s",cAck);
// 如果接收到被呼叫端發送的15個字節信息為“ABCDEFGHIJKLMNO”,表示電話被接聽
if(sAck=="ABCDEFGHIJKLMNO")
{
bServerState=TRUE;
::SetDlgItemText(pDlg->m_hWnd,IDC_STATIC_INFORMATION,"恭喜恭喜,電話被接聽");
::SendMessage(pDlg->m_hWnd, WM_COMMAND, IDC_BUTTON_COMMUNICATE, 0);
}
else
{
::SetDlgItemText(pDlg->m_hWnd,IDC_STATIC_INFORMATION,"不好意思,對方拒接電話");
bServerState=FALSE;
}
}
// 如果用戶同時處于通話狀態(客戶端狀態+服務器端狀態),
// 則調用對話框的OnReceive()函數播放接收到的音頻數據
if(bClientState==TRUE&&bServerState==TRUE)
{
if(bDisconnectState==FALSE)
pDlg->OnReceive();
}
CAsyncSocket::OnReceive(nErrorCode);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -