• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Codejie's C++ Space

            Using C++

            輪子:Ubuntu上也能用的CCTelnetServer.c

                不說啥了,貼代碼。

            #include <stdio.h>
            #include 
            <time.h>
            #include 
            <string.h>

            #if defined(WIN32)

            #include 
            <winsock2.h>

            #else
            #include 
            <unistd.h>
            #include 
            <sys/socket.h>
            #include 
            <netinet/in.h>
            #include 
            <arpa/inet.h>
            #include 
            <sys/ioctl.h>
            #include 
            <pthread.h>
            #endif


            #include 
            "CCDataType.h"
            #include 
            "CCCmd.h"
            #include 
            "CCOutput.h"
            #include 
            "CCTelnetServer.h"

            int cc_TelnetServer_AddClient(SOCKET s, const struct sockaddr * addr)
            {
                
            int index = 0;
                
                
            if(cc_Global.telnet.count + 1 > cc_Global.telnet.max)
                
            {
                    cc_TelnetServer_ClientWriteText(s, 
            "Sorry, the number of connection has exceeded the limit.\n");
            #if defined(WIN32)
                    closesocket(s);
            #else
                    close(s);
            #endif
                }

                
            else
                
            {
                    
            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(((const struct sockaddr_in*)addr)->sin_addr), sizeof(client->ip));
                    client
            ->port = ((const struct sockaddr_in*)addr)->sin_port;
                    client
            ->updated = time(NULL);
                    client
            ->recv = 0;
                    client
            ->next = NULL;

                    
            if(tmp == NULL)
                    
            {
                        client
            ->index = 0;
                        cc_Global.telnet.client 
            = client;
                    }

                    
            else
                    
            {
                        index 
            = tmp->index;
                        
            while(tmp->next != NULL)
                        
            {
                            
            if(index < tmp->index)
                                index 
            = tmp->index;
                            tmp 
            = tmp->next;
                        }

                        client
            ->index = index + 1;
                        tmp
            ->next = client;
                    }


                    
            ++ cc_Global.telnet.count;

                    CC_LOG_OUTPUT(
            "INFO""Client %d connected.\n", client->index);

                    cc_TelnetServer_ClientWritePrompt(s);
                }


                
            return 0;
            }


            int cc_TelnetServer_RemoveClient(struct _cc_telnet_clientdata* client)
            {
                
            struct _cc_telnet_clientdata* tmp = cc_Global.telnet.client;

                
            if(tmp->next == NULL)
                
            {
                    cc_Global.telnet.client 
            = NULL;        
                }

                
            else if(tmp == client)
                
            {
                    cc_Global.telnet.client 
            = tmp->next;
                }

                
            else
                
            {
                    
            while(tmp->next != client)
                    
            {
                        tmp 
            = tmp->next;
                    }

                    tmp
            ->next = client->next;
                }

            #if defined(WIN32)
                closesocket(client
            ->sock);
            #else
                close(client
            ->sock);
            #endif
                
            -- cc_Global.telnet.count;
                free(client);

                
            return 0;
            }


            void cc_TelnetServer_RemoveAllClients()
            {
                
            struct _cc_telnet_clientdata* client = cc_Global.telnet.client, *tmp = NULL;
                
            while(client != NULL)
                
            {
                    tmp 
            = client;
                    client 
            = client->next;

                
            #if defined(WIN32)
                    closesocket(tmp
            ->sock);
                
            #else
                    close(tmp
            ->sock);
                
            #endif

                    free(tmp);
                    
            -- cc_Global.telnet.count;
                }

            }


            int cc_TelnetServer_SetFd(fd_set* fd)
            {
                
            int ret = cc_Global.telnet.sock;
                
            struct _cc_telnet_clientdata* client = cc_Global.telnet.client;

                FD_ZERO(fd);
                FD_SET(cc_Global.telnet.sock, fd);

                
            while(client != NULL)
                
            {
                    FD_SET(client
            ->sock, fd);
                    
            if(client->sock > ret)
                        ret 
            = client->sock;
                    client 
            = client->next;
                }

                
            return ret;
            }


            int cc_TelnetServer_CheckFd(fd_set* fd)
            {
                SOCKET cs 
            = INVALID_SOCKET;
                
            struct sockaddr addr;
            #if defined(WIN32)
                
            int len = sizeof(struct sockaddr);
            #else
                socklen_t len 
            = sizeof(struct sockaddr);
            #endif
                
            struct _cc_telnet_clientdata* client = cc_Global.telnet.client;

                
            if(FD_ISSET(cc_Global.telnet.sock, fd) != 0)
                
            {
                    cs 
            = accept(cc_Global.telnet.sock, &addr, &len);
                    
            if(cs != 0)
                    
            {
                        cc_TelnetServer_AddClient(cs, 
            &addr);
                    }

                }

                
                
            while(client != NULL)
                
            {
                    
            if(FD_ISSET(client->sock, fd) != 0)
                    
            {
                        cc_TelnetServer_OnClientRead(client);
                        
            break;
                    }

                    client 
            = client->next;
                }

                
            return 0;
            }



            //int cc_TelnetServer_Socket2File(SOCKET s, FILE** file)
            //{
            //    *file = fdopen(s, "w");
            //    if((*file) == NULL)
            //        return -1;
            ////    setlinebuf((*file));
            //
            //    return 0;
            //}

            /* ------------------------------- */
            int cc_TelnetServer_ClientWrite(SOCKET s, const char* buf, int size)
            {
                
            char output[CC_SIZE_CMD];
                
            int len = 0, i = 0;
                
            while(i < size)
                
            {
                    
            if(buf[i] == '\n')
                    
            {
                        
            if(i > 0)
                        
            {
                            
            if(buf[i - 1!= '\r')
                            
            {
                                output[len 
            ++= '\r';
                            }

                        }

                        
            else
                        
            {
                            output[len 
            ++= '\r';
                        }

                    }

                    output[len 
            ++ ] = buf[i];
                    
            ++ i;
                }

                
                send(s, output, len, 
            0);

                
            return 0;
            }


            int cc_TelnetServer_ClientWriteText(SOCKET s, const char* text)
            {
                
            return cc_TelnetServer_ClientWrite(s, text, strlen(text));
            }


            int cc_TelnetServer_ClientWritePrompt(SOCKET s)
            {
                
            return cc_TelnetServer_ClientWriteText(s, cc_Global.telnet.prompt);
            }


            int cc_TelnetServer_ClientBufferProc(struct _cc_telnet_clientdata* client, const char* input, int size)
            {
                
            struct _cc_cmddata* cmd = NULL;
                
            int argc = -1;
                
            char argv[CC_SIZE_CMD/2][CC_SIZE_CMD];
                
            int pos = -1;

                memset(argv, 
            0sizeof(argv));

                
            if(client->recv + size > CC_SIZE_CMD)
                
            {
                    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;

                
            while(pos < client->recv)
                
            {
                    
            if(client->buf[pos] == 0x08)//bs
                    {
                        
            if(pos > 0)
                        
            {
                            memcpy(client
            ->buf + pos - 1, client->buf + pos + 2, (client->recv - pos - 1));
                            client
            ->recv -= 2;
                        }

                        
            else if(pos == 0)
                        
            {
                            memcpy(client
            ->buf, client->buf + 1, (client->recv - 1)); 
                            client
            ->recv -= 1;
                        }

                        
            else
                        
            {
                            
            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')
                    
            {
                        
            if(client->recv > 2)
                        
            {
                            
            if(cc_CmdParser_Parser(client, &cmd, &argc, argv) == 0)
                            
            {
                                
            if((cmd->callback)(client, argc, (const char (*)[CC_SIZE_CMD])argv) != 0)
                                    
            return -1;
                            }

                        }

                        client
            ->recv = 0;
                        cc_TelnetServer_ClientWritePrompt(client
            ->sock);
                    }


                    
            ++ pos;
                }

                
            return 0;
             
            //  
             
            //   

                
            //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;
             
            //           //}

             
            //           cc_TelnetServer_ClientWrite(client->sock, "\r\n", 2);
             
            //           client->recv = 0;
             
            //           cc_TelnetServer_ClientWrite(client->sock, ">", 1);
             
            //       }
             
            //   }
             
            //   return 0;
            }


            int cc_TelnetServer_OnClientRead(struct _cc_telnet_clientdata* client)
            {
                
            int rc = -1;
                
            char input[64];

                rc 
            = recv(client->sock, input, sizeof(input), 0);
                
            if(rc > 0)
                
            {
                    
            if(cc_TelnetServer_ClientBufferProc(client, input, rc) != 0)
                    
            {
                        CC_LOG_OUTPUT(
            "INFO""Client %d request to disconnect.\n", client->index);
                        cc_TelnetServer_RemoveClient(client);
                    }

                }

                
            else if(rc == 0)
                
            {
                    CC_LOG_OUTPUT(
            "INFO""Client %d disconnect.\n", client->index);
                    cc_TelnetServer_RemoveClient(client);
                }

                
            else
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""Client %d read failed.\n", client->index);
                    cc_TelnetServer_RemoveClient(client);
                }


                
            return 0;
            }


            /* -------------------------- */

            int cc_TelnetServer_Init()
            {
                
            int flag = 1;
                
            struct sockaddr_in addr;

            #if defined(WIN32)
                WSADATA wsa;

                
            int ret = WSAStartup(MAKEWORD(22), &wsa);
                
            if(ret != NO_ERROR)
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""WSAStartup() failed.\n");
                    
            return -1;
                }

            #endif

                cc_Global.telnet.sock 
            = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                
            if(cc_Global.telnet.sock == INVALID_SOCKET)
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""socket() failed.\n");
                    
            return -1;
                }

            #if defined(WIN32)    
                
            if(ioctlsocket(cc_Global.telnet.sock, FIONBIO, &flag) != 0)
            #else
                
            if(ioctl(cc_Global.telnet.sock, FIONBIO, &flag) != 0)
            #endif
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""ioctl() failed.\n");
                    
            return -1;
                }


                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)
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""bind() failed.\n");
                    
            return -1;
                }

                
                
            if(listen(cc_Global.telnet.sock, SOMAXCONN) != 0)
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""listen() failed.\n");
                    
            return -1;
                }


                
            return 0;
            }


            int cc_TelnetServer_Final()
            {
                cc_TelnetServer_RemoveAllClients();

                
            if(cc_Global.telnet.sock != INVALID_SOCKET)
            #if defined(WIN32)
                    closesocket(cc_Global.telnet.sock);
            #else
                    close(cc_Global.telnet.sock);
            #endif

            #if defined(WIN32)
                WSACleanup();
            #endif

                
            return 0;
            }


            int cc_TelnetServer_Run()
            {
                
            struct timeval val;
                fd_set rd;
                
            int maxfd = -1;
                
            int ret = 1;

                val.tv_sec 
            = 0;
                val.tv_usec 
            = 100;

                cc_Global.telnet.run 
            = 1;
                
            while(cc_Global.telnet.run)
                
            {
                    maxfd 
            = cc_TelnetServer_SetFd(&rd);  

            //        CC_LOG_OUTPUT("DEBUG", "maxfd:%d\n", maxfd);
                    ret = select(maxfd + 1&rd, NULL, NULL, &val);
                    
            if(ret > 0)
                    
            {
                        CC_LOG_OUTPUT(
            "DEBUG""OnRead\n");
                        cc_TelnetServer_CheckFd(
            &rd);
                    }

                    
            else if(ret < 0)
                    
            {
                        CC_LOG_OUTPUT(
            "ERROR""select() failed.\n");
                        
            break;
                    }

                    
            else
                    
            {
            //            CC_LOG_OUTPUT("INFO", "OnTimeout\n");
                    }

                }

                cc_Global.telnet.run 
            = 0;

            #if defined(WIN32)

            #else
                pthread_exit(NULL);
            #endif

                
            return 0;
            }


            int cc_TelnetServer_Create()
            {
                CC_LOG_OUTPUT(
            "INFO""Create telnet server thread\n");

                
            if(cc_TelnetServer_Init() == 0)
                
            {
                    cc_TelnetServer_Run();
                }

                
            else
                
            {
                    CC_LOG_OUTPUT(
            "ERROR""Create failed.\n");
                }

                cc_TelnetServer_Final();

                CC_LOG_OUTPUT(
            "INFO""Telnet server thread terminate.\n");

                
            return 0;
            }


                空了再添加一些代碼,比如socket的reuse,還有UNIX下的Socket到FILE*,這樣就應該可以和工作上的代碼無縫連接了~

            posted on 2009-12-23 17:51 codejie 閱讀(287) 評論(0)  編輯 收藏 引用 所屬分類: C++輪子精神

            公告

            Using C++

            導航

            統計

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久老熟女一区二区三区| 亚洲色欲久久久综合网| 国产欧美久久久精品| 久久久精品人妻一区二区三区蜜桃| 久久精品极品盛宴观看| 要久久爱在线免费观看| 精品多毛少妇人妻AV免费久久| 狠狠色噜噜色狠狠狠综合久久| 精品国产乱码久久久久久人妻 | 色综合色天天久久婷婷基地| 国产亚洲精久久久久久无码| 日本精品久久久久中文字幕| 久久久无码精品午夜| 久久这里只有精品首页| 久久精品国产亚洲77777| 日韩欧美亚洲综合久久影院d3| 亚洲国产精品无码久久青草 | 99久久精品国产一区二区蜜芽| 国产叼嘿久久精品久久| 亚洲国产成人久久综合一区77| 亚洲精品无码专区久久久| 18岁日韩内射颜射午夜久久成人 | 99久久亚洲综合精品网站| 久久亚洲2019中文字幕| 久久久无码精品亚洲日韩蜜臀浪潮 | 一级女性全黄久久生活片免费 | 亚洲国产一成久久精品国产成人综合| 久久青青草视频| 99久久精品国产麻豆| 亚洲精品无码久久毛片| 国内精品久久国产大陆| 亚洲精品NV久久久久久久久久| 99久久免费国产特黄| 久久久无码精品亚洲日韩蜜臀浪潮 | 91精品国产91热久久久久福利| 久久精品久久久久观看99水蜜桃| 88久久精品无码一区二区毛片 | 久久天天躁狠狠躁夜夜avapp| 9191精品国产免费久久| 久久99精品久久久久久久久久| 亚洲欧美国产日韩综合久久|