📄 telnetserver.cpp
字号:
#include "TelnetServer.h"
// 构造函数:缺省 (PORT: 23) (MAX_CONNECTS: 100) (CONNECT_MSG: "已连接。") (SERVER_FULL_MSG: "服务器满...")
TelnetServer::TelnetServer()
{
setServer(23, 100, "已连接。", "服务器满...");
}
// 设置服务器参数
TelnetServer::setServer(int nPort, int nMaxConnects, char connectMsg[9999], char serverFullMsg[9999])
{
int lcv;
// 限制用户数在1-100范围
if (nMaxConnects < 1) { nMaxConnects = 1; }
if (nMaxConnects > 100) { nMaxConnects = 100; }
PORT = nPort;
MAX_CONNECTS = nMaxConnects;
CONNECT_MSG[0] = 0;
SERVER_FULL_MSG[0] = 0;
sServer = INVALID_SOCKET;
if (connectMsg[0] != 0)
{
strcat(&CONNECT_MSG[0], &connectMsg[0]);
}
if (serverFullMsg[0] != 0)
{
strcat(&SERVER_FULL_MSG[0], &serverFullMsg[0]);
}
user = new USER[MAX_CONNECTS + 5];
for (lcv = 0; lcv < (MAX_CONNECTS + 5); lcv++)
{
mMsg[lcv].cMsg[0] = 0;
mMsg[lcv].nUser = -1;
}
for (lcv = 0; lcv < (MAX_CONNECTS + 5); lcv++)
{
user[lcv].cName[0] = 0;
user[lcv].cInput[0] = 0;
user[lcv].sUser = INVALID_SOCKET;
user[lcv].user_sin_len = sizeof(user[lcv].user_sin);
}
}
TelnetServer::~TelnetServer()
{
//关闭所有的客户端口
for (int lcv = 0; lcv < (MAX_CONNECTS + 5); lcv++);
{
closeClientSocket(lcv);
}
stopListen();
}
// 从队列中回收到来的消息
MESSAGE TelnetServer::getMessage()
{
MESSAGE mMessage;
char *cMsg;
mMessage.nUser = mMsg[0].nUser;
mMessage.cMsg[0] = 0;
if (mMsg[0].cMsg[0] != 0)
{
cMsg = &mMessage.cMsg[0];
strcat(cMsg, &mMsg[0].cMsg[0]);
}
for (int lcv = 0; lcv < 99; lcv++)
{
mMsg[lcv] = mMsg[lcv + 1];
}
return mMessage;
}
// 开始监听连接
int TelnetServer::startListen()
{
if (WSAStartup(MAKEWORD(1,1), &WSAData) != 0) { return -1; }
if ((sServer = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { return -1; }
server_sin.sin_family = AF_INET;
server_sin.sin_port = htons (PORT);
server_sin.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(sServer, (struct sockaddr *) &server_sin, sizeof(server_sin)) == SOCKET_ERROR)
{
stopListen();
return -1;
}
if (listen(sServer, MAX_CONNECTS + 5) == SOCKET_ERROR)
{
stopListen();
return -1;
}
return 0;
}
// 停止监听连接
void TelnetServer::stopListen()
{
closesocket(sServer);
sServer = INVALID_SOCKET;
}
// 关闭通信socket
void TelnetServer::closeClientSocket(int nUser)
{
shutdown(user[nUser].sUser, 0x02);
closesocket(user[nUser].sUser);
user[nUser].sUser = INVALID_SOCKET;
user[nUser].cName[0] = 0;
user[nUser].cNote[0] = 0;
}
// 接受连接
void TelnetServer::acceptConnects()
{
int iConn, lcv, lcv2;
fd_set fds;
timeval tTime;
FD_ZERO(&fds);
FD_SET(sServer, &fds);
tTime.tv_sec = 0;
tTime.tv_usec = 0;
iConn = select(0, &fds, NULL, NULL, &tTime);
lcv2 = MAX_CONNECTS + 5;
while ((iConn) && (lcv2))
{
lcv = 0;
while ((user[lcv].sUser != INVALID_SOCKET) && (lcv < (MAX_CONNECTS + 5))) { lcv++; }
if (lcv < MAX_CONNECTS)
{
user[lcv].sUser = accept(sServer, (struct sockaddr *) &user[lcv].user_sin, (int *) &user[lcv].user_sin_len);
if (user[lcv].sUser != INVALID_SOCKET)
{
sendUser(lcv, &CONNECT_MSG[0]);
}
} else {
user[lcv].sUser = accept(sServer, (struct sockaddr *) &user[lcv].user_sin, (int *) &user[lcv].user_sin_len);
if (user[lcv].sUser != INVALID_SOCKET)
{
sendUser(lcv, &SERVER_FULL_MSG[0]);
closeClientSocket(lcv);
}
}
iConn = select(0, &fds, NULL, NULL, &tTime);
lcv2--;
}
}
// 添加到来的消息到消息队列中
void TelnetServer::acceptMessages()
{
int nMsg, lcv, lcv2, lcv3, nAtoI;
u_long lMsg;
char cMsg[255], cMsg2[2];
char *cInput;
char *ccMsg;
for (lcv = 0; lcv < MAX_CONNECTS; lcv++)
{
nMsg = ioctlsocket(user[lcv].sUser, FIONREAD, &lMsg);
if ((nMsg == 0) && (lMsg > 0))
{
nMsg = recv(user[lcv].sUser, cMsg, 255, 0);
if ((nMsg != SOCKET_ERROR) && (nMsg != 0))
{
for (lcv3 = 0; lcv3 < ((int) strlen(cMsg)); lcv3++)
{
nAtoI = (int) cMsg[lcv3];
if ((cMsg[0] != 0) && (strlen(user[lcv].cInput) < 255) && (nAtoI >31) && (nAtoI < 127))
{
cInput = &user[lcv].cInput[0];
cMsg2[0] = cMsg[lcv3];
cMsg2[1] = 0;
strcat(cInput, &cMsg2[0]);
}
if (nAtoI == 13)
{
lcv2 = 0;
while (mMsg[lcv2].cMsg[0] != 0) { lcv++; }
mMsg[lcv2].nUser = lcv;
ccMsg = &mMsg[lcv2].cMsg[0];
strcat(ccMsg, &user[lcv].cInput[0]);
user[lcv].cInput[0] = 0;
}
if (nAtoI == 8)
{
user[lcv].cInput[(int) strlen(user[lcv].cInput) - 1] = 0;
}
}
}
}
}
}
// 从用户数组中得到用户信息
USER TelnetServer::getUserInfo(int nUser)
{
return user[nUser];
}
// 设置用户名
void TelnetServer::setUserName(int nUser, char cName[19])
{
user[nUser].cName[0] = 0;
if (cName[0] != 0)
{
strcat(&user[nUser].cName[0], &cName[0]);
}
}
// 设置用户通知
void TelnetServer::setUserNote(int nUser, char cNote[256])
{
user[nUser].cNote[0] = 0;
if (cNote[0] != 0)
{
strcat(&user[nUser].cNote[0], &cNote[0]);
}
}
// 发送消息到具体的客户
int TelnetServer::sendUser(int nUser, char cSend[9999])
{
int nSend;
nSend = send(user[nUser].sUser, &cSend[0], strlen(cSend) + 1, 0);
if (nSend == SOCKET_ERROR)
{
closeClientSocket(nUser);
}
return nSend;
}
// 发送消息到所有的客户
void TelnetServer::sendAll(char cSend[9999])
{
for (int lcv = 0; lcv < MAX_CONNECTS; lcv++)
{
sendUser(lcv, &cSend[0]);
}
}
// 关闭空连接
void TelnetServer::closeEmptySockets()
{
for (int lcv = 0; lcv < (MAX_CONNECTS + 5); lcv++)
{
sendUser(lcv, "");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -