⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用于网络游戏医学图像网关的高qos要求.更详细的内容可阅读相应的材料
💻 CPP
字号:
// server.cpp,v 1.9 2003/11/09 20:44:18 dhinton Exp

/* The actual datagram operations here are exactly the same as those
   used in the previous tutorial.  What we've added is some logic that
   will prevent this server from responding to just any old datagram.
   I'll limit my comments to those pieces of code.  */

#include "ace/Log_Msg.h"
#include "ace/SOCK_Dgram.h"
#include "ace/INET_Addr.h"
#include "ace/OS_NS_string.h"

static const u_short PORT = ACE_DEFAULT_SERVER_PORT;

/* In order to be more selective, our server will be started with a
   "signature".  If none is given, we'll use the one here instead.  */
static const char *default_signature = "Hello World!";

int
main (int argc, char *argv[])
{
  ACE_INET_Addr local (PORT);
  ACE_SOCK_Dgram dgram;

  if (dgram.open (local) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  char buf[BUFSIZ];
  ACE_INET_Addr remote;

  while (dgram.recv (buf,
                     sizeof (buf),
                     remote) != -1)
    {
      /* What did the client say?  */
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) Received (%s) from (%s)\n",
                  buf,
                  remote.get_host_name ()));

      /* Use a simple string-op to decide if the client is one of our
        own.  Of course, you could have sent numeric values or even a
        struct of data. For this simple exercise, however, strings are
        just fine.  */
      if (ACE_OS::strcmp (buf,
                          argc > 1 ? argv[1] : default_signature))
        {
          /* If the client didn't say something we like then log it
           * and move on.
           */
          ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Client query does not match our signature (%s).  Response not sent.\n",
              argc > 1 ? argv[1] : default_signature));
        }
      else
        {
          /* As before, we respond to the client's query.  */

          ACE_INET_Addr local ((u_short) 0);
          ACE_SOCK_Dgram peer;
          if (peer.open (local) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "%p\n",
                               "response open"),
                              -1);
          sprintf (buf,
                   "I am here");
          if (peer.send (buf,
                         ACE_OS::strlen (buf) + 1,
                         remote) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "%p\n",
                               "response send"),
                              -1);
        }
    }

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -