📄 demo.cpp
字号:
// demo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "demo.h"
#include "spSocketServer.h"
#include <conio.h>
#include <ctype.h>
#include <afxsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
/*
下面是为了使用服务功能添加的代码
*/
using namespace spBase;
//定义服务器名称,不同的程序必须定义不同的名称,
//相同名称的服务器不能在同一主机运行
#define MY_SERVER_NAME "my_svr"
class CMyDisplay : public CFileOutputDisplay
{//显示类
public:
CMyDisplay(LPCSTR pszServerName,LPCSTR pszFileName):CFileOutputDisplay(pszServerName,pszFileName)
{};
~CMyDisplay(){};
public:
BOOL PutLine(ErrorLevel eLevel,LPCSTR pszOutput)
{
char szDesc[20];
GetErrorLevelString(eLevel,szDesc);
printf("%d %s %s\n",(int)eLevel,szDesc,pszOutput);
return CFileOutputDisplay::PutLine(eLevel,pszOutput);
};
};
void MyCallback(CServerStatus* pS,CSocketChildThread* pC)
{//回调函数,功能:直接返回对方发送来的数据
int iRead,iWrote;
if(pS->GetParentThreadStatus() != CServerStatus::Running)
{//判断当前服务器是否为运行状态
char szRet[]="当前未运行";
pC->m_sockComm.Send(strlen(szRet) +1,(BYTE*)szRet,iWrote);
return ;
}
while(1)
{
BYTE b;
if(SP_ERR_SUCCESS == pC->m_sockComm.Recv(1,&b,iRead))
pC->m_sockComm.Send(1,&b,iWrote);
else
break;
}
}
/*
代码结束
*/
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
return 1;
}
if(!AfxSocketInit())
{
cerr << _T("socket init error") << endl;
return 2;
}
/*
下面是程序控制代码
建立一个监听套接口在本地 9988 端口等待连接
可以使用telnet localhost 9988命令进行测试
*/
//启动服务器
GlobalOpenUniqueProcess(MY_SERVER_NAME);//保证当前只有一个实例在运行
CMyDisplay myDisplay(MY_SERVER_NAME,"LOG_FILE");
CSocketGlobalDataStorage store(MY_SERVER_NAME,MyCallback,&myDisplay);//创建文件输出对象
store.OpenListenSocket(NULL,9988);
CSocketParentThread parent(&store);
store.GetServerStatus()->SetRunning();
parent.CreateNewThread(TRUE);//启动监听线程
char ch;
while(1)
{//接受命令
printf("\n\n输入命令\n1:启动父线程\n2:暂停\n3:恢复\n4:请求退出父线程\n5:得到当前连接数\n6:得到当前运行状态\nQ:退出\n\n\n");
ch = _getch();
ch = toupper( ch );
switch(ch)
{
case '1':
if(store.GetServerStatus()->GetParentThreadStatus() == CServerStatus::NotRun)
{
store.OpenListenSocket(NULL,9988);
store.GetServerStatus()->SetRunning();
parent.CreateNewThread();
}
break;
case '2':
if(store.GetServerStatus()->GetParentThreadStatus() == CServerStatus::Running)
store.GetServerStatus()->SetPause();
break;
case '3':
if(store.GetServerStatus()->GetParentThreadStatus() == CServerStatus::Pause)
store.GetServerStatus()->SetResume();
break;
case '4':
if(store.GetServerStatus()->GetParentThreadStatus() != CServerStatus::NotRun)
{
store.GetServerStatus()->SetAsk2Quit();
store.CloseListenSocket();
}
break;
case '5':
printf("当前连接数 %d\n",store.GetServerStatus()->GetChildThreadNumber());
break;
case '6':
printf("当前运行状态 %d\n",store.GetServerStatus()->GetParentThreadStatus());
break;
case 'Q':
if(store.GetServerStatus()->GetParentThreadStatus() == CServerStatus::NotRun && store.GetServerStatus()->GetChildThreadNumber() == 0)
goto out_exit;
else
printf("必须在停止运行,而且当前没有连接时才可以退出");
break;
}
}
/*
控制代码完
*/
out_exit:
printf("退出\n\n");
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -