📄 socketclient.cpp
字号:
// SocketClient.cpp : implementation file
#include "stdafx.h"
#include "NetPhone.h"
#include "SocketClient.h"
#include "NetPhoneDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CSocketClient
CSocketClient::CSocketClient()
{
}
CSocketClient::~CSocketClient()
{
Close();
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CSocketClient, CAsyncSocket)
//{{AFX_MSG_MAP(CSocketClient)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
// FD_CONNECT网络事件处理函数,连接成功时发生
void CSocketClient::OnConnect(int nErrorCode)
{
if (0 != nErrorCode)
{
switch( nErrorCode )
{
case WSAEADDRINUSE:
AfxMessageBox("The specified address is already in use.\n");
break;
case WSAEADDRNOTAVAIL:
AfxMessageBox("The specified address is not available from the local machine.\n");
break;
case WSAEAFNOSUPPORT:
AfxMessageBox("Addresses in the specified family cannot be used with this socket.\n");
break;
case WSAECONNREFUSED:
AfxMessageBox("The attempt to connect was forcefully rejected.\n");
break;
case WSAEDESTADDRREQ:
AfxMessageBox("A destination address is required.\n");
break;
case WSAEFAULT:
AfxMessageBox("The lpSockAddrLen argument is incorrect.\n");
break;
case WSAEINVAL:
AfxMessageBox("The socket is already bound to an address.\n");
break;
case WSAEISCONN:
AfxMessageBox("The socket is already connected.\n");
break;
case WSAEMFILE:
AfxMessageBox("No more file descriptors are available.\n");
break;
case WSAENETUNREACH:
AfxMessageBox("The network cannot be reached from this host at this time.\n");
break;
case WSAENOBUFS:
AfxMessageBox("No buffer space is available. The socket cannot be connected.\n");
break;
case WSAENOTCONN:
AfxMessageBox("The socket is not connected.\n");
break;
case WSAENOTSOCK:
AfxMessageBox("The descriptor is a file, not a socket.\n");
break;
case WSAETIMEDOUT:
AfxMessageBox("The attempt to connect timed out without establishing a connection. \n");
break;
default:
TCHAR szError[256];
wsprintf(szError, "OnConnect error: %d", nErrorCode);
AfxMessageBox(szError,MB_ICONINFORMATION | MB_OK,NULL);
break;
}
}
CAsyncSocket::OnConnect(nErrorCode);
}
// FD_WRITE网络事件处理函数,有数据发送时发生
void CSocketClient::OnSend(int nErrorCode)
{
if (0 != nErrorCode)
{
switch( nErrorCode) //Send Error
{
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 WSAEACCES:
AfxMessageBox("The requested address is a broadcast address, but the appropriate flag was not set.\n");
break;
case WSAEINPROGRESS:
AfxMessageBox("A blocking Windows Sockets operation is in progress.\n");
break;
case WSAENETRESET:
AfxMessageBox("The connection must be reset because the Windows Sockets implementation dropped it.\n");
break;
case WSAENOBUFS:
AfxMessageBox("The Windows Sockets implementation reports a buffer deadlock.\n");
break;
case WSAENOTCONN:
AfxMessageBox("The socket is not connected. \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 requested operation would block.\n");
break;
case WSAEMSGSIZE:
AfxMessageBox("The socket is of type SOCK_DGRAM, and the datagram is larger than the maximum supported by the Windows Sockets implementation. \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, "OnSend error: %d", nErrorCode);
AfxMessageBox(szError,MB_ICONINFORMATION | MB_OK,NULL);
break;
}
}
CAsyncSocket::OnSend(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -