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