VC6 下编译 ACE5.5

本文提供了使用ACE库进行网络编程的示例代码,包括服务端和客户端程序。介绍了如何设置开发环境并链接ACE库,适合初学者入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    重新下了一个不带 TAO 的 ACE 源代码, 在VC6下编译,顺利通过。

    使用 《ACE程序员教程》 中的例子试了一下,还可以。

    服务端的程序如下:

#include "ace/SOCK_Acceptor.h"
#include 
"ace/SOCK_Stream.h"
#include 
"ace/os.h"
#include 
"ace/log_msg.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 ""Error in recv"));
            
else
            
{
                data_buf_[byte_count]
=0;
                ACE_DEBUG((LM_DEBUG,
"Server received %s  ",data_buf_));
            }

        }

        
// Close new endpoint
        if (new_stream_.close () == -1)
            ACE_ERROR ((LM_ERROR, 
"%p ""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 ","Error in get_local_addr"),1);
        ACE_DEBUG ((LM_DEBUG,
"Starting server at port %d ",
            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 ""accept"));
                
continue;
            }

            
else
            
{
                ACE_DEBUG((LM_DEBUG,
                    
"Connection established with remote %s:%d ",
                    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);
    }

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


 

   客户端代码如下

 

#include "ace/SOCK_Connector.h"
#include 
"ace/INET_Addr.h"
#include 
"ace/os.h"
#include 
"ace/log_msg.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 ",
            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 ","connection failed"),-1);
        
else
            ACE_DEBUG ((LM_DEBUG,
"(%P|%t) connected to %s ",
            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_)
+10== -1)
            
{
                ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) %p ","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 ","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> ", 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;
}

需要注意的是:

1。需要设定头文件和库文件的搜索路径,在 tools->options 下的 include files 和 library files,我的分别是

E:/ACE_wrappers 和 E:/ACE_wrappers/lib

2。使用多线程版的运行时库

3。project setting 下的 link  页 input 中导入之前编译好的ACE库,比如 aced.lib

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值