📄 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 + -