• <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>

            寶杉的博客

            UNIX/LINUX;ACE;SNMP;C++
            posts - 33, comments - 23, trackbacks - 0, articles - 0

            [原]ACE流--初學(xué)ACE

            Posted on 2007-07-25 10:57 寶杉 閱讀(1417) 評論(2)  編輯 收藏 引用 所屬分類: ACE/CORBA

                  ACE中的流包裝提供面向連接的通信。流數(shù)據(jù)傳輸包裝類包括ACE_SOCK_Stream和ACE_LSOCK_Stream,它們分別包裝TCP/IP和UNIX域socket協(xié)議數(shù)據(jù)傳輸功能。連接建立類包括針對TCP/IP的ACE_SOCK_Connector和ACE_SOCK_Acceptor,以及針對UNIX域socket的ACE_LSOCK_Connector和ACE_LSOCK_Acceptor。
                  Acceptor類用于被動(dòng)地接受連接(使用BSD accept()調(diào)用),而Connector類用于主動(dòng)地建立連接(使用BSD connect()調(diào)用)。
                  下面的例子演示接收器和連接器是怎樣用于建立連接的。該連接隨后將用于使用流數(shù)據(jù)傳輸類來傳輸數(shù)據(jù)。

            server端代碼:
            // 環(huán)境VC6.0+ACE5.4.2
            // proj:ACE stream server
            // date:07-7-24
            // robin
            //

            #include "stdafx.h"

            #include "ace/Log_Msg.h"
            #include "ace/Time_Value.h"
            #include "ace/OS.h"


            #include "ace/SOCK_Acceptor.h"
            #include "ace/SOCK_Stream.h"

            #define SIZE_DATA 18
            #define SIZE_BUF 1024
            #define NO_ITERATIONS 5

            class Server
            {
            public:
             Server (int port) : server_addr_(port),peer_acceptor_(server_addr_)
             {
              data_buf_ = new char[SIZE_BUF];
             }

             //Handle the connection once it has been established. Here the
             //connection is handled by reading SIZE_DATA amount of data from the
             //remote and then closing the connection stream down.

             int handle_connection()
             {
              // Read data from client
              for(int i=0;i<NO_ITERATIONS;i++)
              {
               int byte_count=0;
               if( (byte_count = new_stream_.recv_n(data_buf_, SIZE_DATA, 0) ) == -1 )
                ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv"));
               else
               {
                data_buf_[byte_count]=0;
                ACE_DEBUG((LM_DEBUG,"Server received %s \n",data_buf_));
               }
              }

              // Close new endpoint
              if (new_stream_.close () == -1)
               ACE_ERROR ((LM_ERROR, "%p\n", "close"));
              return 0;
             }


             //Use the acceptor component peer_acceptor_ to accept the connection
             //into the underlying stream new_stream_. After the connection has been
             //established call the handle_connection() method.
             
             int accept_connections ()
             {
              if (peer_acceptor_.get_local_addr (server_addr_) == -1)
               ACE_ERROR_RETURN ((LM_ERROR,"%p\n","Error in get_local_addr"),1);
              ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n",
               server_addr_.get_port_number ()));
              
              // Performs the iterative server activities.

              while(1)
              {
               ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
               if (peer_acceptor_.accept(new_stream_, &client_addr_, &timeout)== -1)
               {
                ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
                continue;
               }
               else
               {
                ACE_DEBUG((LM_DEBUG,
                 "Connection established with remote %s:%d\n",
                 client_addr_.get_host_name(),client_addr_.get_port_number()));

                //Handle the connection
                handle_connection();
               }
              }
             }

            private:
             char *data_buf_;
             ACE_INET_Addr server_addr_;
             ACE_INET_Addr client_addr_;
             ACE_SOCK_Acceptor peer_acceptor_;
             ACE_SOCK_Stream new_stream_;
            };


            int main (int argc, char *argv[])
            {
             if(argc<2)
             {
              ACE_ERROR((LM_ERROR,"Usage %s <port_num>", argv[0]));
              ACE_OS::exit(1);
             }

            // char *ip;
            // ip = new char[strlen("192.168.1.160")];
            // Server server(ACE_OS::atoi(ip));  //argv[1])

             Server server(ACE_OS::atoi(argv[1]));
             server.accept_connections();
             return 0;
            }

            client端:
            // proj:ACE stream client
            // client
            // date:7-24
            // robin

            #include "stdafx.h"

            //******additional*******//
            #include "ace/Log_Msg.h"  //ACE_ERROR ACE_DEBUG
            #include "ace/Time_Value.h"  // ACE_Time_Value
            #include "ace/OS.h"    // ACE_OS::atoi exit
            //******additional*******//

            #include "ace/SOCK_Connector.h"
            #include "ace/INET_Addr.h"

            #define SIZE_BUF 128
            #define NO_ITERATIONS 5


            class Client
            {
            public:
             Client(char *hostname, int port):remote_addr_(port,hostname)
             {
              data_buf_="Hello from Client";
             }

             //Uses a connector component `connector_’ to connect to a
             //remote machine and pass the connection into a stream
             //component client_stream_
             int connect_to_server()
             {
              // Initiate blocking connection with server.
              ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%d\n",
               remote_addr_.get_host_name(),remote_addr_.get_port_number()));
              if (connector_.connect (client_stream_, remote_addr_) == -1)
               ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","connection failed"),-1);
              else
               ACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %s\n",
               remote_addr_.get_host_name ()));
              return 0;
             }


             //Uses a stream component to send data to the remote host.
             int send_to_server()
             {
              // Send data to server
              for(int i=0;i<NO_ITERATIONS; i++)
              {
               if (client_stream_.send_n (data_buf_,
                ACE_OS::strlen(data_buf_)+1, 0) == -1)
               {
                ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","send_n"),0);
                break;
               }
              }
              //Close down the connection
              close();
             }

             //Close down the connection properly.
             int close()
             {
              if (client_stream_.close () == -1)
               ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","close"),-1);
              else
               return 0;
             }
            private:
             ACE_SOCK_Stream client_stream_;
             ACE_INET_Addr remote_addr_;
             ACE_SOCK_Connector connector_;
             char *data_buf_;
            };


            int main(int argc, char* argv[])
            {
             if(argc<3)
             {
              ACE_DEBUG((LM_DEBUG,"Usage %s <hostname> <port_number>\n", argv[0]));
              ACE_OS::exit(1);
             }

             Client client(argv[1],ACE_OS::atoi(argv[2]));
             client.connect_to_server();
             client.send_to_server();
             return 0;
            }


            運(yùn)行結(jié)果:
            cmd里到exe目錄下,先啟動(dòng)服務(wù)端server.exe 192.168.1.160
            如圖1:

            正在輪詢等待。

            再到client服務(wù)端,同樣的方法
            運(yùn)行命令行參數(shù)格式 client.exe 192.168.1.160 192
             解析:ACE_DEBUG((LM_DEBUG,"Usage %s <hostname> <port_number>\n", argv[0]));
                     命令行參數(shù)為 <hostname><port> = <192.168.1.160><192>
                     端口是由圖1的第二行顯示,不是自己設(shè)定的。
            如圖2:

            只是照搬書上的例子,后面打算自己分析一下。

            Feedback

            # re: [原]ACE流--初學(xué)ACE  回復(fù)  更多評論   

            2007-07-25 17:46 by 夢在天涯
            en ,有時(shí)間俺也學(xué)ACE,沒有用過,也沒有看過!以后就靠大家的學(xué)習(xí)心得了

            # re: [原]ACE流--初學(xué)ACE  回復(fù)  更多評論   

            2007-07-27 09:59 by 寶杉
            @夢在天涯
            是啊 好書很難找 ACE自帶的例子有點(diǎn)難
            我有兩本推薦
            C++網(wǎng)絡(luò)編程 卷1 運(yùn)用ACE和模式消除復(fù)雜性
            中篇:ACE程序員教程
            和一個(gè)ACE的查詢網(wǎng)址:http://www.cs.wustl.edu/~schmidt/research.html
            久久无码人妻精品一区二区三区| 久久久久久国产a免费观看不卡 | 久久久精品国产sm调教网站 | 久久国产精品成人片免费| 99久久人妻无码精品系列 | 999久久久免费国产精品播放| 开心久久婷婷综合中文字幕| 欧美久久一级内射wwwwww.| 国产韩国精品一区二区三区久久| 色天使久久综合网天天| 一本一道久久综合狠狠老| 久久久久婷婷| 久久天堂AV综合合色蜜桃网| 亚洲性久久久影院| 亚洲&#228;v永久无码精品天堂久久| 国产免费久久精品99re丫y| 91精品婷婷国产综合久久| 99精品国产免费久久久久久下载| 国产午夜福利精品久久2021 | 热re99久久精品国99热| 国产精品九九久久免费视频| 国产精品一久久香蕉产线看| 久久福利资源国产精品999| 色综合久久精品中文字幕首页| 99久久成人国产精品免费| 久久婷婷国产剧情内射白浆| 久久亚洲AV无码精品色午夜麻豆| 久久成人精品视频| 国产午夜免费高清久久影院| 久久久久久国产精品无码超碰| 久久亚洲精品无码VA大香大香| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 人妻无码精品久久亚瑟影视| 久久国产精品免费一区| 久久精品视屏| 狠狠精品久久久无码中文字幕| 国产成人综合久久综合| 天天综合久久久网| 久久精品成人国产午夜| 久久久精品免费国产四虎| 2021精品国产综合久久|