summaryrefslogtreecommitdiff
path: root/docs/tutorials/009/server.cpp
blob: f73c7fd4ad3b20603e41a27617328bc1da34d871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

// $Id$

/*
   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/SOCK_Dgram.h"
#include "ace/INET_Addr.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[512];
  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 client;
      if (client.open (local) == -1)
      {
         ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "response open"), -1);
      }

      sprintf (buf, "I am here");
      if (client.send (buf, strlen (buf) + 1, remote) == -1)
      {
         ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "response send"), -1);
      }
    }
  }

  return (0);
}