<!-- page03.html,v 1.11 2000/11/27 17:56:43 othman Exp --> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (X11; I; Linux 2.0.32 i486) [Netscape]"> <META NAME="Author" CONTENT="James CE Johnson"> <TITLE>ACE Tutorial 009</TITLE> </HEAD> <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> <CENTER><B><FONT SIZE=+2>ACE Tutorial 009</FONT></B></CENTER> <CENTER><B><FONT SIZE=+2>Sending and receiving datagrams again</FONT></B></CENTER> <P> <HR WIDTH="100%"> <P>Our new <A HREF="directed_client.cpp">directed_client.cpp</A> is very much like our previous one. The primary difference is the addition of a timeout to the recv() call so that we can exit somewhat gracefully if the server doesn't like what we have to say. <P> <HR WIDTH="100%"> <PRE> <font color=red>// page03.html,v 1.11 2000/11/27 17:56:43 othman Exp</font> <font color=blue>#include</font> "<A HREF="../../../ace/Log_Msg.h">ace/Log_Msg.h</A>" <font color=blue>#include</font> "<A HREF="../../../ace/SOCK_Dgram.h">ace/SOCK_Dgram.h</A>" <font color=blue>#include</font> "<A HREF="../../../ace/INET_Addr.h">ace/INET_Addr.h</A>" static const u_short PORT = ACE_DEFAULT_SERVER_PORT; int main (int argc, char *argv[]) { ACE_INET_Addr local ((u_short) 0); ACE_INET_Addr remote (PORT, argc > 1 ? argv[1] : "<font color=green>localhost</font>"); ACE_SOCK_Dgram dgram; if (dgram.open (local) == -1) ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>open</font>"), -1); char buf[BUFSIZ]; <font color=red>/* In order to conform to the "<font color=green>protocol</font>" required by the server, we allow the user to specify a signature. A default matching the server's default is also available. */</font> sprintf (buf, argc > 2 ? argv[2] : "<font color=green>Hello World!</font>"); if (dgram.send (buf, <font color=#008888>ACE_OS::strlen</font> (buf) + 1, remote) == -1) ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>send</font>"), -1); <font color=red>/* Because we may have sent a signature that the server doesn't honor, we have to have some way to get out of the recv(). Most ACE objects that have potential for infinite blocking give you the option of providing a timeout. recv() is no exception. Here, we construct an ACE_Time_Value representing two seconds and no micro-seconds. If recv() fails to get a response within the two seconds, it will return -1. */</font> ACE_Time_Value timeout (2, 0); if (dgram.recv (buf, sizeof (buf), remote, 0, &timeout) == -1) ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>recv</font>"), -1); <font color=red>/* Note: The fourth parameter to recv() is for flags. These flags are passed directly to the underlying recv() or recvfrom() system call. For Linux, resonable values are: MSG_OOB process out-of-band data MSG_PEEK peek at incoming message (but leave it in the OS buffers) MSG_WAITALL wait for full request or error See your system documentation for the gory details. */</font> ACE_DEBUG ((LM_DEBUG, "<font color=green>(%P|%t) The server said (%s)\n</font>", buf)); return 0; } </PRE> <HR WIDTH="100%"> <P>On the next page, we see that the directed_client gets similar upgrades. <P><HR WIDTH="100%"> <CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page04.html">Continue This Tutorial</A>]</CENTER>