www.gusucode.com > Telnet协定远端登陆VC源代码-源码程序 > Telnet协定远端登陆VC源代码-源码程序/code/TelnetChatServer/TelnetChatServer/telnetServer.cpp

    //Download by http://www.NewXing.com
#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, "");
	}
}