CCTelnetSever是用于建立Telnet侦听,并处理Socket相关事件的模块。简单说,就是一个Socket Server的实现,并不难。代码是基于WinSocket实现的。
#include <stdio.h>
#include <time.h>
#include <winsock2.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include "CCDataType.h"
#include "CCCmd.h"
#include "CCOutput.h"
#include "CCTelnetServer.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_AddClient(SOCKET s, const struct sockaddr_in * addr)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int index = 0;
if(cc_Global.telnet.count + 1 > cc_Global.telnet.max)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_TelnetServer_ClientWriteText(s, "Sorry, the number of connection has exceeded the limit.\n");
closesocket(s);
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
struct _cc_telnet_clientdata * tmp = cc_Global.telnet.client;
struct _cc_telnet_clientdata * client = (struct _cc_telnet_clientdata*)malloc(sizeof(struct _cc_telnet_clientdata));
client->sock = s;
strncpy(client->ip, inet_ntoa(addr->sin_addr), sizeof(client->ip));
client->port = addr->sin_port;
client->updated = time(NULL);
client->recv = 0;
client->next = NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(tmp == NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
client->index = 0;
cc_Global.telnet.client = client;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
index = tmp->index;
while(tmp->next != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(index < tmp->index)
index = tmp->index;
tmp = tmp->next;
}
client->index = index + 1;
tmp->next = client;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
++ cc_Global.telnet.count;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CC_LOG_OUTPUT("INFO", "Client %d connected.\n", client->index);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cc_TelnetServer_ClientWritePrompt(s);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_RemoveClient(struct _cc_telnet_clientdata* client)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
struct _cc_telnet_clientdata* tmp = cc_Global.telnet.client;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(tmp->next == NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_Global.telnet.client = NULL;
}
else if(tmp == client)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_Global.telnet.client = tmp->next;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while(tmp->next != client)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = tmp->next;
}
tmp->next = client->next;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
closesocket(client->sock);
-- cc_Global.telnet.count;
free(client);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void cc_TelnetServer_RemoveAllClients()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
struct _cc_telnet_clientdata* client = cc_Global.telnet.client, *tmp = NULL;
while(client != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = client;
client = client->next;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
closesocket(tmp->sock);
free(tmp);
-- cc_Global.telnet.count;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_SetFd(fd_set* fd)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int ret = cc_Global.telnet.sock;
struct _cc_telnet_clientdata* client = cc_Global.telnet.client;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
FD_ZERO(fd);
FD_SET(cc_Global.telnet.sock, fd);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(client != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FD_SET(client->sock, fd);
if(client->sock > ret)
ret = client->sock;
client = client->next;
}
return ret;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_CheckFd(fd_set* fd)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
SOCKET cs = INVALID_SOCKET;
struct sockaddr_in addr;
int len = sizeof(struct sockaddr_in);
struct _cc_telnet_clientdata* client = cc_Global.telnet.client;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(FD_ISSET(cc_Global.telnet.sock, fd) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cs = accept(cc_Global.telnet.sock, &addr, &len);
if(cs != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_TelnetServer_AddClient(cs, &addr);
}
}
while(client != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(FD_ISSET(client->sock, fd) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_TelnetServer_OnClientRead(client);
break;
}
client = client->next;
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//int cc_TelnetServer_Socket2File(SOCKET s, FILE** file)
//{
// *file = fdopen(s, "w");
// if((*file) == NULL)
// return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**///// setlinebuf((*file));
//
// return 0;
//}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* ------------------------------- */
int cc_TelnetServer_ClientWrite(SOCKET s, const char* buf, int size)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
char output[CC_SIZE_CMD];
int len = 0, i = 0;
while(i < size)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(buf[i] == '\n')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(i > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(buf[i - 1] != '\r')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
output[len ++] = '\r';
}
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
output[len ++] = '\r';
}
}
output[len ++ ] = buf[i];
++ i;
}
send(s, output, len, 0);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_ClientWriteText(SOCKET s, const char* text)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return cc_TelnetServer_ClientWrite(s, text, strlen(text));
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_ClientWritePrompt(SOCKET s)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return cc_TelnetServer_ClientWriteText(s, cc_Global.telnet.prompt);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_ClientBufferProc(struct _cc_telnet_clientdata* client, const char* input, int size)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
struct _cc_cmddata* cmd = NULL;
int argc = -1;
char argv[CC_SIZE_CMD/2][CC_SIZE_CMD];
int pos = -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
memset(argv, 0, sizeof(argv));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(client->recv + size > CC_SIZE_CMD)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
client->recv = 0;
cc_TelnetServer_ClientWriteText(client->sock, "command is too long.\r\n");
cc_TelnetServer_ClientWritePrompt(client->sock);
return 0;
}
memcpy(client->buf + client->recv, input, size);
pos = client->recv;
client->recv += size;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(pos < client->recv)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(client->buf[pos] == 0x08)//bs
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(pos > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
memcpy(client->buf + pos - 1, client->buf + pos + 2, (client->recv - pos - 1));
client->recv -= 2;
}
else if(pos == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
memcpy(client->buf, client->buf + 1, (client->recv - 1));
client->recv -= 1;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return -1;
}
break;
}
//else if(client->buf[pos] == 0x1b)//esc
//{
// client->recv = 0;
// cc_TelnetServer_ClientWriteText(client->sock, "\r\n");
// cc_TelnetServer_ClientWritePrompt(client->sock);
//}
else if(client->buf[pos] == '\r' || client->buf[pos] == '\n')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(client->recv > 2)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(cc_CmdParser_Parser(client, &cmd, &argc, argv) == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if((cmd->callback)(client, argc, argv) != 0)
return -1;
}
}
client->recv = 0;
cc_TelnetServer_ClientWritePrompt(client->sock);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
++ pos;
}
return 0;
//
//
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//if (input[0] == ' ' ||
// input[0] == ',' ||
// input[0] == '!' ||
// input[0] == '.' ||
// input[0] == '/' ||
// input[0] == '<' ||
// input[0] == '>' ||
// input[0] == '?' ||
// input[0] == '_' ||
// (input[0] >= '0' && input[0] <= '9') ||
// (input[0] >= 'A' && input[0] <= 'Z') ||
// (input[0] >= 'a' && input[0] <= 'z'))
// {
// memcpy(client->buf + client->recv, input, size);
// client->recv += size;
// cc_TelnetServer_ClientWrite(client->sock, input, size);
// }
// else if(input[0] == 0x08)//bs
// {
// if(client->recv > 0)
// {
// -- client->recv;
// cc_TelnetServer_ClientWrite(client->sock, input, size);
// }
// }
// else if(input[0] == 0x1b && input[1] == 0)
// {
// client->recv = 0;
// cc_TelnetServer_ClientWrite(client->sock, "\r\n", 2);
// }
// else if(input[0] == '\r')
// {
// if(client->recv > 0)
// {
// cc_TelnetServer_ClientWrite(client->sock, "\r\n", 2);
// //if(cc_CmdParser_Parser(client->buf, client->recv, &cmd, &argc, &argv) == 0)
// //{
// // return cmd->callback(client, argc, argv);
// //}
// //else
// //{
// // return -1;
// //}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// cc_TelnetServer_ClientWrite(client->sock, "\r\n", 2);
// client->recv = 0;
// cc_TelnetServer_ClientWrite(client->sock, ">", 1);
// }
// }
// return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_OnClientRead(struct _cc_telnet_clientdata* client)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int rc = -1;
char input[64];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
rc = recv(client->sock, input, sizeof(input), 0);
if(rc > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(cc_TelnetServer_ClientBufferProc(client, input, rc) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("INFO", "Client %d request to disconnect.\n", client->index);
cc_TelnetServer_RemoveClient(client);
}
}
else if(rc == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("INFO", "Client %d disconnect.\n", client->index);
cc_TelnetServer_RemoveClient(client);
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "Client %d read failed.\n", client->index);
cc_TelnetServer_RemoveClient(client);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* -------------------------- */
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_Init()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int flag = 1;
struct sockaddr_in addr;
WSADATA wsa;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int ret = WSAStartup(MAKEWORD(2, 2), &wsa);
if(ret != NO_ERROR)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "WSAStartup() failed.\n");
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cc_Global.telnet.sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(cc_Global.telnet.sock == INVALID_SOCKET)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "socket() failed.\n");
return -1;
}
if(ioctlsocket(cc_Global.telnet.sock, FIONBIO, &flag) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "socket() failed.\n");
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(cc_Global.telnet.ip);
addr.sin_port = htons(cc_Global.telnet.port);
if(bind(cc_Global.telnet.sock, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "bind() failed.\n");
return -1;
}
if(listen(cc_Global.telnet.sock, SOMAXCONN) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "listen() failed.\n");
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_Final()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
cc_TelnetServer_RemoveAllClients();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(cc_Global.telnet.sock != INVALID_SOCKET)
closesocket(cc_Global.telnet.sock);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
WSACleanup();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_Run()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
struct timeval val;
fd_set rd;
int maxfd = -1;
int ret = 1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
val.tv_sec = 0;
val.tv_usec = 10;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cc_Global.telnet.run = 1;
while(cc_Global.telnet.run)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
maxfd = cc_TelnetServer_SetFd(&rd);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ret = select(maxfd + 1, &rd, NULL, NULL, &val);
if(ret > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_TelnetServer_CheckFd(&rd);
}
else if(ret < 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "select() failed.\n");
break;
}
}
cc_Global.telnet.run = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cc_TelnetServer_Create()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(cc_TelnetServer_Init() == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cc_TelnetServer_Run();
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CC_LOG_OUTPUT("ERROR", "Create failed.\n");
}
cc_TelnetServer_Final();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}