Free space

OPEN SOURCE.

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  2 随笔 :: 1 文章 :: 0 评论 :: 0 Trackbacks
/***************************************************************************
server.cpp:    
    Copyright (c) 2006, All rights reserved.

Purpose:
    a ACE TCP Server sample.

Author:
    Zhao Dexiang

Creating Time:
    2006-05-19
***************************************************************************/

#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/OS_NS_stdio.h"
#include "ace/Log_Msg.h"
#include "ace/Time_Value.h"

#define SIZE_BUF 256
#define TIMEOUT_SEC 20

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

    int handle_connection();
    int accept_connections ();

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
Server::handle_connection()
{
    int byte_count=0;

    byte_count=new_stream_.recv_n (data_buf_, SIZE_BUF);
    if(byte_count == -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_));
    }

    int n_bytes = 0;
    if( ( n_bytes=new_stream_.send_n(data_buf_, byte_count))==-1)
    {
        ACE_ERROR ((LM_ERROR, "%p \n", "Error in send"));
    }

    if (new_stream_.close () == -1)
    {
        ACE_ERROR ((LM_ERROR, "%p \n", "close"));
    }

    return 0;
}

int
Server::accept_connections ()
{
    if (peer_acceptor_.get_local_addr (server_addr_) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,"%p \n","Error in get_local_addr"),1);

    while(1)
    {
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nListening server at port: %d \n"),
                            server_addr_.get_port_number ()));

        ACE_Time_Value timeout (TIMEOUT_SEC);
      int accept_t = peer_acceptor_.accept (new_stream_, &client_addr_, &timeout);
        if (accept_t == -1)
        {
            if (ACE_OS::last_error() == EINTR)
                ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Interrupted. \n")));
            else
                if (ACE_OS::last_error() == ETIMEDOUT)
                    ACE_ERROR ((LM_ERROR, "\n %p", "Timeout while accepting"));

            continue;
        }
        else
        {
            ACE_DEBUG ((LM_DEBUG,
                                "Connection established with remote %s: %d \n",
                                client_addr_.get_host_name(), client_addr_.get_port_number()));

            handle_connection();
            ACE_DEBUG ((LM_INFO, "Connection closed at host %s: %d \n",
                                client_addr_.get_host_name(), client_addr_.get_port_number()));
        }
    }
}

int main (int argc, char *argv[])
{
    static const u_short port=1009;

    Server server(port);
    server.accept_connections();

    return 0;
}


/***************************************************************************
client.cpp:    
    Copyright (c) 2006, All rights reserved.

Purpose:
    a ACE TCP Client sample.

Author:
    Zhao Dexiang

Creating Time:
    2006-05-19
***************************************************************************/
#include <iostream>
#include "ace/SOCK_Connector.h"
#include "ace/INET_Addr.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"

#define SIZE_BUF 256

class Client
{
public:
    Client(char *hostname, int port):remote_addr_(port,hostname)
    {
        data_buf_= new char[SIZE_BUF];
    }

    int connect_to_server();
    int send_to_server();
    int close();

private:
    ACE_SOCK_Stream client_stream_;
    ACE_INET_Addr remote_addr_;
    ACE_SOCK_Connector connector_;

    char *data_buf_;
};

int
Client::connect_to_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;
}

int
Client::send_to_server()
{
    std::cout << ": ";
    std::cin >> data_buf_;

    int n = 0;
    n = client_stream_.send_n (data_buf_, SIZE_BUF);
    if (n == -1) {
        ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p \n", "send_n"),0);
    }

  // recv
    char recv_buff[SIZE_BUF] = { 0 };
    n=client_stream_.recv_n (recv_buff, sizeof(recv_buff));
    if (n == -1) {
        ACE_ERROR ((LM_ERROR, "%p \n", "Error in recv"));
    }
    else {
        ACE_DEBUG((LM_DEBUG, "Client received: %s \n", recv_buff));
    }

    return 0;
}

int
Client::close()
{
    if (client_stream_.close () == -1)
        ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p \n", "close"),-1);
    else
        return 0;
}

static char* SERVER_HOST = "127.0.0.1";
static u_short SERVER_PORT = 1009;

int main (int argc, char *argv[])
{
    Client client(SERVER_HOST, SERVER_PORT);
    client.connect_to_server();
    client.send_to_server();
    ACE_OS::sleep (1);
    client.close();

    return 0;
}


############################################################################
##
## Generic Makefile for ACE Program. (sample => server | client)
##
## Author: Zhao Dexiang (zdxster AT gmail DOT com)
## Date: 2006/05/19

### Variables: ###
CC = cl.exe
RC = rc.exe
LINK = link.exe
PROGRAM = sample
OBJS = .\output
EXECFILE = $(OBJS)\$(PROGRAM).exe
ACEPATH = F:\ACE_wrappers

CMNCCFLAGS = /D "_UNICODE" /D "UNICODE" /Od /D "WIN32" /D "_CONSOLE" /EHsc /GS /W3 /nologo /c /Wp64 /TP
CMNLDFLAGS = /NOLOGO /SUBSYSTEM:CONSOLE /MACHINE:X86 /pdb:"$(OBJS)\$(PROGRAM).pdb" /out:"$(EXECFILE)"
CMNLDLIBS = kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

!IF "$(BUILD)" == ""
BUILD = Debug
!ENDIF

!IF "$(BUILD)" == "Release"
CCFLAGS = $(CMNCCFLAGS) /MD /FD /Zi /D "NDEBUG"
EXTRAFLAGS = /I $(ACEPATH) /I.
LDFLAGS = $(CMNLDFLAGS) /INCREMENTAL:NO
LDPATH = $(ACEPATH)\lib
LDLIBS = $(CMNLDLIBS) ACE.lib

!ELSEIF "$(BUILD)" == "Debug"
CCFLAGS = $(CMNCCFLAGS) /Gm /RTC1 /MDd /ZI /D "_DEBUG"
EXTRAFLAGS = /I $(ACEPATH) /I.
LDFLAGS = $(CMNLDFLAGS) /incremental:yes /debug
LDPATH = $(ACEPATH)\lib
LDLIBS = $(CMNLDLIBS) ACEd.lib
!ENDIF

!IF "$(BUILD)" == "Release" || "$(BUILD)" == "Debug"
ALL_OBJECTS = \
        $(OBJS)\sample.obj    \

### Conditionally setting: ###
all: $(OBJS)
$(OBJS):
    -if not exist $(OBJS) mkdir $(OBJS)

### Targets: ###
all: $(EXECFILE) data

clean:
    -if exist $(OBJS)\*.obj del $(OBJS)\*.obj
    -if exist $(OBJS)\*.res del $(OBJS)\*.res
    -if exist $(OBJS)\*.pch del $(OBJS)\*.pch
    -if exist $(OBJS)\*.ilk del $(OBJS)\*.ilk
    -if exist $(OBJS)\*.pdb del $(OBJS)\*.pdb
    -if exist $(OBJS)\$(PROGRAM).exe del $(OBJS)\$(PROGRAM).exe

$(EXECFILE): $(ALL_OBJECTS)
     $(LINK) /OUT:$@ $(LDFLAGS) $** /LIBPATH:$(LDPATH) $(LDLIBS)

data:
$(OBJS)\sample.obj: .\sample.cpp
    $(CC) $(CCFLAGS) /Fo$@ $(EXTRAFLAGS) $**

!ENDIF

### End of the Makefile ## All rights reserved ###
##################################################

posted on 2006-05-19 16:18 Free space 阅读(1781) 评论(0)  编辑 收藏 引用 所属分类: ACE

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理