• <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>
            隨筆 - 96  文章 - 255  trackbacks - 0
            <2008年4月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            E-mail:zbln426@163.com QQ:85132383 長期尋找對戰略游戲感興趣的合作伙伴。

            常用鏈接

            留言簿(21)

            隨筆分類

            隨筆檔案

            SDL相關網站

            我的個人網頁

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 492138
            • 排名 - 38

            最新評論

            閱讀排行榜

            評論排行榜

            //Filename: TcpServerClass.hpp

            #ifndef TCPSERVERCLASS_HPP_INCLUDED
            #define TCPSERVERCLASS_HPP_INCLUDED

            #include 
            <unistd.h>
            #include 
            <iostream>
            #include 
            <sys/socket.h>
            #include 
            <arpa/inet.h>

            class TcpServer
            {
            private:
                
            int listenSock;
                
            int communicationSock;
                sockaddr_in servAddr;
                sockaddr_in clntAddr;
            public:
                TcpServer(
            int listen_port);
                
            bool isAccept();
                
            void handleEcho();
            };


            #endif // TCPSERVERCLASS_HPP_INCLUDED

            //Filename: TcpServerClass.cpp

            #include 
            "TcpServerClass.hpp"

            TcpServer::TcpServer(
            int listen_port)
            {
                
            if ( (listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ) {
                    
            throw "socket() failed";
                }

                memset(
            &servAddr, 0sizeof(servAddr));
                servAddr.sin_family 
            = AF_INET;
                servAddr.sin_addr.s_addr 
            = htonl(INADDR_ANY);
                servAddr.sin_port 
            = htons(listen_port);

                
            if ( bind(listenSock, (sockaddr*)&servAddr, sizeof(servAddr)) < 0 ) {
                    
            throw "bind() failed";
                }

                
            if ( listen(listenSock, 10< 0 ) {
                    
            throw "listen() failed";
                }
            }

            bool TcpServer::isAccept()
            {
                unsigned 
            int clntAddrLen = sizeof(clntAddr);

                
            if ( (communicationSock = accept(listenSock, (sockaddr*)&clntAddr, &clntAddrLen)) < 0 ) {
                    
            return false;
                } 
            else {
                    std::cout 
            << "Client(IP: " << inet_ntoa(clntAddr.sin_addr) << ") connected.\n";
                    
            return true;
                }
            }

            void TcpServer::handleEcho()
            {
                
            const int BUFFERSIZE = 32;
                
            char buffer[BUFFERSIZE];
                
            int recvMsgSize;
                
            bool goon = true;

                
            while ( goon == true ) {
                    
            if ( (recvMsgSize = recv(communicationSock, buffer, BUFFERSIZE, 0)) < 0 ) {
                        
            throw "recv() failed";
                    } 
            else if ( recvMsgSize == 0 ) {
                        goon 
            = false;
                    } 
            else {
                        
            if ( send(communicationSock, buffer, recvMsgSize, 0!= recvMsgSize ) {
                            
            throw "send() failed";
                        }
                    }
                }

                close(communicationSock);
            }

            演示程序:
            //Filename: main.cpp
            //Tcp Server C++ style, single work

            #include 
            <iostream>
            #include 
            "TcpServerClass.hpp"

            int echo_server(int argc, char* argv[]);

            int main(int argc, char* argv[])
            {
                
            int mainRtn = 0;
                
            try {
                    mainRtn 
            = echo_server(argc, argv);
                }
                
            catch ( const char* s ) {
                    perror(s);
                    exit(EXIT_FAILURE);
                }

                
            return mainRtn;
            }

            int echo_server(int argc, char* argv[])
            {
                
            int port;
                
            if ( argc == 2 ) {
                    port 
            = atoi(argv[1]);
                } 
            else {
                    port 
            = 5000;
                }

                TcpServer myServ(port);

                
            while ( true ) {
                    
            if ( myServ.isAccept() == true ) {
                        myServ.handleEcho();
                    }
                }

                
            return 0;
            }


            posted on 2008-07-16 12:57 lf426 閱讀(8458) 評論(3)  編輯 收藏 引用 所屬分類: SDL入門教程Linux與C++socket 編程入門教程

            FeedBack:
            # re: Linux socket 編程入門(一)TCP server 端:8、本章的完整源代碼 2009-10-31 11:09 Austin
            不能編譯通過ubuntu server 9.04
            GCC 4.33

            TcpServerClass.o: In function `__static_initialization_and_destruction_0(int, int)':
            TcpServerClass.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
            TcpServerClass.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
            TcpServerClass.o: In function `TcpServer::handleEcho()':
            TcpServerClass.cpp:(.text+0xbd): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0xd7): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0xdf): undefined reference to `__cxa_throw'
            TcpServerClass.cpp:(.text+0x125): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x13f): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x147): undefined reference to `__cxa_throw'
            TcpServerClass.o: In function `TcpServer::isAccept()':
            TcpServerClass.cpp:(.text+0x1da): undefined reference to `std::cout'
            TcpServerClass.cpp:(.text+0x1df): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
            TcpServerClass.cpp:(.text+0x1eb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
            TcpServerClass.cpp:(.text+0x1fb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
            TcpServerClass.o: In function `TcpServer::TcpServer(int)':
            TcpServerClass.cpp:(.text+0x24b): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x265): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x26d): undefined reference to `__cxa_throw'
            TcpServerClass.cpp:(.text+0x2f3): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x30d): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x315): undefined reference to `__cxa_throw'
            TcpServerClass.cpp:(.text+0x33d): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x357): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x35f): undefined reference to `__cxa_throw'
            TcpServerClass.o: In function `TcpServer::TcpServer(int)':
            TcpServerClass.cpp:(.text+0x3a3): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x3bd): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x3c5): undefined reference to `__cxa_throw'
            TcpServerClass.cpp:(.text+0x44b): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x465): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x46d): undefined reference to `__cxa_throw'
            TcpServerClass.cpp:(.text+0x495): undefined reference to `__cxa_allocate_exception'
            TcpServerClass.cpp:(.text+0x4af): undefined reference to `typeinfo for char const*'
            TcpServerClass.cpp:(.text+0x4b7): undefined reference to `__cxa_throw'
            TcpServerClass.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
            main.o: In function `__static_initialization_and_destruction_0(int, int)':
            main.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
            main.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
            main.o: In function `main':
            main.cpp:(.text+0x11d): undefined reference to `__cxa_begin_catch'
            main.cpp:(.text+0x148): undefined reference to `__cxa_end_catch'
            main.o:(.gcc_except_table+0x18): undefined reference to `typeinfo for char const*'
            main.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
            collect2: ld returned 1 exit status
            make: *** [main] Error 1  回復  更多評論
              
            # re: socket 編程入門教程(一)TCP server 端:8、本章的完整源代碼 2010-10-20 11:04 12345678
            @Austin

            用g++ 編譯!


              回復  更多評論
              
            # re: socket 編程入門教程(一)TCP server 端:8、本章的完整源代碼 2015-08-29 12:55 xxz
            你好我將你三個程序分別復制到三個文件中,然后編譯main.cpp,用g++ 編譯通不過
            main.cpp: In function ‘int main(int, char**)’:
            main.cpp:16:17: error: ‘perror’ was not declared in this scope
            perror(s);
            ^
            main.cpp:17:14: error: ‘EXIT_FAILURE’ was not declared in this scope
            exit(EXIT_FAILURE);
            ^
            main.cpp:17:26: error: ‘exit’ was not declared in this scope
            exit(EXIT_FAILURE);
            ^
            main.cpp: In function ‘int echo_server(int, char**)’:
            main.cpp:27:28: error: ‘atoi’ was not declared in this scope
            port = atoi(argv[1]);  回復  更多評論
              
            国产成人精品久久亚洲| 亚洲中文字幕无码久久精品1| 亚洲AV无码1区2区久久| 色8久久人人97超碰香蕉987| 久久国产免费观看精品| 久久久久97国产精华液好用吗| 最新久久免费视频| 97久久精品午夜一区二区| 久久精品国产WWW456C0M| 7777精品伊人久久久大香线蕉| 国产成人精品免费久久久久| 久久久久亚洲精品男人的天堂| 久久精品国产亚洲AV影院| 久久精品国产精品青草app| 香蕉久久夜色精品国产尤物| 国产精品一久久香蕉国产线看| 日韩亚洲国产综合久久久| 99久久人妻无码精品系列 | 一本一本久久A久久综合精品 | 久久九九免费高清视频| 日韩精品久久无码人妻中文字幕| 国内精品久久久久久不卡影院| 精品久久久久久久久午夜福利| 亚洲人成网站999久久久综合 | 996久久国产精品线观看| 麻豆精品久久久久久久99蜜桃| 久久精品成人一区二区三区| 潮喷大喷水系列无码久久精品| 久久精品国产久精国产一老狼| 国产精品免费久久| 精品久久人人做人人爽综合| 久久综合综合久久狠狠狠97色88| 国产一区二区三区久久精品| 亚洲∧v久久久无码精品| 国产色综合久久无码有码| 国产精品久久久久久久app| 思思久久99热只有频精品66| 一本大道久久东京热无码AV| 国产69精品久久久久APP下载| 色婷婷久久久SWAG精品| 97精品伊人久久大香线蕉|