ACE - ACE_INET_Addr, ACE_SOCK_Acceptor, ACE_SOCK_Connector, ACE_SOCK_Stream

5 篇文章 1 订阅

 

ACE_SOCK_Acceptor    服务器端socket

ACE_SOCK_Connector 客户机端socket

ACE_SOCK_Stream      用来连接服务器端和客户机端, 在这儿有send和recv的接口.

 

 

/#:> cat server.C
#include "SOCK_Acceptor.h"
#include <ace/INET_Addr.h>
#include <ace/Log_Msg.h>
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
        ACE_INET_Addr port_to_listen (1234);
        ACE_SOCK_Acceptor acceptor;

        acceptor.open (port_to_listen, 1);
        cout << "Listening ..." << endl;

        while (1)
        {
                ACE_SOCK_Stream peer;
                ACE_INET_Addr peer_addr;
                ACE_Time_Value timeout (10, 0);
                if( 0 == acceptor.accept (peer, &peer_addr, &timeout, 0))
                {
                        cout << "Incoming ..." << endl;
                        char buffer[BUFSIZ];
                        ssize_t bytes_received;
                        while ((bytes_received = peer.recv (buffer, sizeof(buffer))) > 0)
                        {
                                buffer[bytes_received]='/0';
                                cout << "Receiving " << buffer << endl;
                                peer.send_n (buffer, bytes_received);
                        }
                        peer.close ();
                }
        }
        return 0;
}

 

/#:> cat client.C
#include <ace/SOCK_Connector.h>
#include <ace/INET_Addr.h>
#include <ace/Log_Msg.h>
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
        //ACE_INET_Addr remote_addr(atoi("1234"), "your_ip_address");

        ACE_INET_Addr remote_addr("0.0.0.0:1234");
        ACE_SOCK_Connector connector;
        ACE_SOCK_Stream stream;

        char buf[BUFSIZ];

        if (connector.connect(stream, remote_addr) == -1)
                cerr << "Connect to server failed ... Exit" << endl;

        sprintf(buf,"Hello World!");
        cout << "Send message ..." << endl;
        stream.send_n(buf,strlen(buf));

        stream.recv(buf, sizeof(buf));
        cout << "Receive message: " << buf << endl;

        stream.close();
}

 

 

/#:> cat Makefile


ACE_ROOT =.../vendor/Corba/ACE_TAO


CCFLAGS += -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__ -DACE_HAS_IPV

LDFLAGS =

ACE_LIBS = -lACE

all: server client

server: server.o
                CC $(CCFLAGS) -L$(ACE_ROOT)/lib/NR  $(ACE_LIBS) -o $@ $^

client: client.o
                CC $(CCFLAGS) -L$(ACE_ROOT)/lib/NR  $(ACE_LIBS) -o $@ $^

%.o: %.C
                CC -c $(CCFLAGS) -I$(ACE_ROOT)/include -I$(ACE_ROOT)/include/ace -o $@ $^

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值