ACE教程003 创建一个简单的客户端

现在我们已经看到了如何创建服务器,让我们花一点时间建立一个客户端。 由于这很容易,我将在这一页中完成所有这些操作。

Kirthika说,“这是一个单页客户端应用程序的一段摘要:”

  • 服务器是一个ACE_SOCK_Stream类型的Stream对象。 ACE_Sock_Connector负责主动与侦听服务器建立连接。 它使用存储在ACE_INET_Addr对象中的server_host和端口号来实现。一旦建立连接,客户端就开始与服务器交互并用消息轰炸它。 注意:send_n()调用的使用,因为此调用会查看网络缓冲问题并可靠地将数据传递到服务器。 此外,如果服务器在事务完成之前死亡,则设置超时值以提供容错。 一旦在Event_Handler :: handle_input()调用期间读取零字节,服务器就会调用close()方法。 这证明客户已切断其连接。 然后服务器关闭。
// page01.html,v 1.12 2000/11/27 17:56:42 othman Exp

/* To establish a socket connection to a server, we'll need an
  ACE_SOCK_Connector.  */
#include "ace/SOCK_Connector.h"
#include "ace/Log_Msg.h"

/* Unlike the previous two tutorials, we're going to allow the user to
  provide command line options this time.  Still, we need defaults in
  case that isn't done.  */
static u_short SERVER_PORT = ACE_DEFAULT_SERVER_PORT;
static const char *const SERVER_HOST = ACE_DEFAULT_SERVER_HOST;
static const int MAX_ITERATIONS = 4;

int
main (int argc, char *argv[])
{
  /* Accept the users's choice of hosts or use the default.  Then do
    the same for the TCP/IP port at which the server is listening as
    well as the number of iterations to perform.  */
  const char *server_host = argc > 1 ? argv[1] : SERVER_HOST;
  u_short server_port = argc > 2 ? ACE_OS::atoi (argv[2]) : SERVER_PORT;
  int max_iterations = argc > 3 ? ACE_OS::atoi (argv[3]) : MAX_ITERATIONS;

  /* Build ourselves a Stream socket. This is a connected socket that
    provides reliable end-to-end communications. We will use the
    server object to send data to the server we connect to.  */
  ACE_SOCK_Stream server;

  /* And we need a connector object to establish that connection. The
    ACE_SOCK_Connector object provides all of the tools we need to
    establish a connection once we know the server's network
    address...  */
  ACE_SOCK_Connector connector;

  /* Which we create with an ACE_INET_Addr object. This object is
    given the TCP/IP port and hostname of the server we want to
    connect to.  */
  ACE_INET_Addr addr (server_port,
                      server_host);

  /* So, we feed the Addr object and the Stream object to the
    connector's connect() member function. Given this information, it
    will establish the network connection to the server and attach
    that connection to the server object.  */
  if (connector.connect (server, addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  /* Just for grins, we'll send the server several messages.  */
  for (int i = 0; i < max_iterations; i++)
    {
      char buf[BUFSIZ];

      /* Create our message with the message number */
      ACE_OS::sprintf (buf,
                       "message = %d\n",
                       i + 1);
      /* Send the message to the server.  We use the server object's
        send_n() function to send all of the data at once. There is
        also a send() function but it may not send all of the
        data. That is due to network buffer availability and such. If
        the send() doesn't send all of the data, it is up to you to
        program things such that it will keep trying until all of the
        data is sent or simply give up. The send_n() function already
        does the "keep trying" option for us, so we use it.

        Like the send() method used in the servers we've seen, there
        are two additional parameters you can use on the send() and
        send_n() method calls.  The timeout parameter limits the
        amount of time the system will attempt to send the data to the
        peer.  The flags parameter is passed directly to the OS send()
        system call.  See send(2) for the valid flags values.  */
      if (server.send_n (buf,
                         ACE_OS::strlen (buf)) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p\n",
                           "send"),
                          -1);
      else
        /* Pause for a second.  */
        ACE_OS::sleep (1);
    }

  /* Close the connection to the server.  The servers we've created so
    far all are based on the ACE_Reactor.  When we close(), the
    server's reactor will see activity for the registered event
    handler and invoke handle_input().  That, in turn, will try to
    read from the socket but get back zero bytes.  At that point, the
    server will know that we've closed from our side.  */
  if (server.close () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close"),
                      -1);
  return 0;
}

好的,这很容易。 更容易的是将所有连接包装在一个对象中,并重载一些基本操作以减少以网络为中心的开发。 也许我们会在另一个教程中看到它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值