/*\brief Receive single datagram \note The function employes dgram_port and dgram_recv_timeout variables \retval -1 if not received, \retval 0 received a datagrams */ int run_receiver () { ACE_DEBUG ((LM_INFO, ACE_TEXT ("Receiving datagrams from port %d with timeout %d ms\n"), dgram_port, dgram_recv_timeout.msec ())); ACE_SOCK_Dgram socket; ACE_INET_Addr remote ; static char dgram_buffer[BUFSIZ]; if (socket.open (ACE_INET_Addr (dgram_port)) != -1) if (socket.recv (dgram_buffer, sizeof (dgram_buffer), remote, 0, &dgram_recv_timeout) > 0) { ACE_DEBUG ((LM_INFO, ACE_TEXT ("%C received\n"), dgram_buffer)); return 0; } else { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Cannot receive datagrams")), -1); } else { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p: %d\n"), ACE_TEXT ("Cannot open broadcast socket on port"), dgram_port), -1); } }
// Listing 2 code/ch09 void echo_dgram (void) { ACE_INET_Addr my_addr (ACE_static_cast (u_short, 10102)); ACE_INET_Addr your_addr; ACE_SOCK_Dgram udp (my_addr); char buff[BUFSIZ]; size_t buflen = sizeof (buff); ssize_t recv_cnt = udp.recv (buff, buflen, your_addr); if (recv_cnt > 0) udp.send (buff, ACE_static_cast (size_t, buflen), your_addr); udp.close (); return; }
int DgramHandler::handle_input (ACE_HANDLE) { int recvBuffer; ACE_INET_Addr peerAddress; int result = peer_.recv (&recvBuffer, sizeof (recvBuffer) , peerAddress); if (0 >= result) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%P) %p\n"), ACE_TEXT ("While reading datagram from socket")) , -1); else ++dgramsReceived_; return 0; }
/* Our goal here is to develop a client that can send a datagram to a server running on a known host. We'll use a command-line argument to specify the hostname instead of hard-coding it. */ int main (int argc,char *argv[]) { /* All datagrams must have a point of origin. Since we intend to transmit instead of receive, we initialize an address with zero and let the OS choose a port for us. We could have chosen our own value between 1025 and 65535 as long as it isn't already in use. The biggest difference between client and server when datagrams are used is the fact that servers tend to have a known/fixed address at which they listen and clients tend to have arbitrary addresses assigned by the OS. */ ACE_INET_Addr local((u_short) 0); /* And here is our datagram object. */ ACE_SOCK_Dgram dgram; /* Notice that this looks a lot like the server application. There's no difference in creating server datagrams an client datagrams. You can even use a zero-constructed address for your server datagram as long as you tell the client where you're listening (eg -- by writting into a file or some such). */ if (dgram.open (local) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "datagram open"), -1); /* Yep. We've seen this before too... */ char buf[BUFSIZ]; /* Ok, now we're doing something different. */ sprintf (buf, "Hello World!"); /* Just like sending a telegram, we have to address our datagram. Here, we create an address object at the desired port on the chosen host. To keep us from crashing, we'll provide a default host name if we aren't given one. */ ACE_INET_Addr remote (PORT, argc > 1 ? argv[1] : "localhost"); ACE_DEBUG ((LM_DEBUG, "(%P|%t) Sending (%s) to the server.\n", buf)); /* Now we send our buffer of stuff to the remote address. This is just exactly what the server did after receiving a client message. Datagrams are rather orthogonal that way: they don't generally make much of a fuss about being either client or server. */ if (dgram.send (buf, ACE_OS::strlen (buf) + 1, remote) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send"), -1); /* Now we've turned around and put ourselves into "server mode" by invoking the recv() method. We know our server is going to send us something, so we hang out here and wait for it. Because we know datagrams are unreliable, there is a chance that the server will respond but we won't hear. You might consider providing a timeout on the recv() in that case. If recv() fails due to timeout it will return -1 and you can then resend your query and attempt the recv() again. Like the server application, we have to give the recv() an uninitialized addr object so that we can find out who is talking back to us. */ if (dgram.recv (buf, sizeof (buf), remote) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "recv"), -1); /* Find out what the server had to say. */ ACE_DEBUG ((LM_DEBUG, "(%P|%t) The server said: %s\n", buf)); /* Using the "remote" object instance, find out where the server lives. We could then save this address and use directed datagrams to chat with the server for a while. */ ACE_DEBUG ((LM_DEBUG, "(%P|%t) The server can be found at: (%s:%d)\n", remote.get_host_name(), PORT)); return 0; }
int run_main (int argc, ACE_TCHAR *argv[]) { parse_args (argc, argv); // Child process code. if (child_nr >= 0) { ACE_TCHAR lognm[MAXPATHLEN]; int mypid (ACE_OS::getpid ()); ACE_OS::sprintf(lognm, ACE_TEXT ("RW_Process_Mutex_Test-child-%d"), (int)mypid); ACE_START_TEST (lognm); if (child_nr == 0) writer (); else reader (child_nr); ACE_END_LOG; } else { ACE_START_TEST (ACE_TEXT ("RW_Process_Mutex_Test")); // Although it should be safe for each process to construct and // destruct the rw lock, this can disturb other process still // using the lock. This is not really correct, and should be // looked at, but it gets things moving. // Also see Process_Mutex_Test.cpp for similar issue. ACE_RW_Process_Mutex mutex (mutex_name.c_str ()); // Make sure the constructor succeeded if (ACE_LOG_MSG->op_status () != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Parent, mutex %s %p\n"), mutex_name.c_str (), ACE_TEXT ("ctor"))); } #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR) static const ACE_TCHAR* format = ACE_TEXT ("%ls -c %d -p %u -n %ls"); #else static const ACE_TCHAR* format = ACE_TEXT ("%s -c %d -p %u -n %s"); #endif /* !ACE_WIN32 && ACE_USES_WCHAR */ // The parent process reads time ranges sent from the children via // UDP. Grab an unused UDP port to tell the children to send to. ACE_INET_Addr me; ACE_SOCK_Dgram sock; if (sock.open (ACE_Addr::sap_any, PF_INET) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Socket %p\n"), ACE_TEXT ("open")), -1); sock.get_local_addr (me); ACE_TCHAR me_str[80]; me.addr_to_string (me_str, 80); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Receiving on %s\n"), me_str)); // Spawn 1 writer and 3 reader processes that will contend for the // lock. Child writer; Child readers[Nr_Processes - 1]; int i; for (i = 0; i < Nr_Processes; i++) { Child *child = (i == 0 ? &writer : &readers[i-1]); ACE_Process_Options options; options.command_line (format, argc > 0 ? argv[0] : ACE_TEXT ("RW_Process_Mutex_Test"), i, (unsigned int)me.get_port_number (), mutex_name.c_str ()); if (child->spawn (options) == -1) { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("spawn of child %d %p\n"), i, ACE_TEXT ("failed")), -1); } else { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Child process %d has pid = %d.\n"), i, (int)(child->getpid ()))); } } // Keep reading time ranges reported from the children until all the // children have exited. Alternate between checking for a range and // checking for exits. int processes = Nr_Processes; Child *children[Nr_Processes]; for (i = 0; i < Nr_Processes; i++) children[i] = (i == 0 ? &writer : &readers[i-1]); Range_Report report; ACE_Time_Value poll (0); ACE_INET_Addr from; ssize_t bytes; while (processes > 0) { ACE_Time_Value limit (10); bytes = sock.recv (&report, sizeof (report), from, 0, &limit); if (bytes > 0) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Report from child %d; %b bytes\n"), report.child_, bytes)); if (report.child_ == 0) writer.add_range (report.range_); else { if (report.child_ >= 1 && report.child_ < Nr_Processes) readers[report.child_ - 1].add_range (report.range_); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("Report from out-of-range child #%d\n"), report.child_)); } } else { if (errno == ETIME) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("UDP time out; check child exits\n"))); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("UDP recv"))); } for (i = 0; i < Nr_Processes; i++) { if (children[i] == 0) continue; ACE_exitcode child_status; // See if the child has exited. int wait_result = children[i]->wait (poll, &child_status); if (wait_result == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("Wait for child %d, %p\n"), i, ACE_TEXT ("error"))); else if (wait_result != 0) { if (child_status == 0) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Child %d finished ok\n"), (int)(children[i]->getpid ()))); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("Child %d finished with status %d\n"), (int)(children[i]->getpid ()), child_status)); children[i] = 0; --processes; } } } sock.close (); if (0 != mutex.remove ()) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("mutex remove"))); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Comparing time ranges...\n"))); // The writer should never overlap any readers bool writer_overlap = false; for (i = 0; i < Nr_Processes - 1; ++i) { if (writer.any_overlaps (readers[i])) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Writer overlaps reader %d\n"), i+1)); writer_overlap = true; } } if (!writer_overlap) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer does not overlap with readers; Ok\n"))); // And there should be some overlap between readers. bool reader_overlap = false; for (i = 0; i < Nr_Processes - 1; ++i) { // Just compare to those higher, else it compares the same ones, // only in reverse. for (int j = i + 1; j < Nr_Processes - 1; ++j) { if (readers[i].any_overlaps (readers[j])) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reader %d overlaps reader %d; Ok\n"), i + 1, j + 1)); reader_overlap = true; } } } if (!reader_overlap) ACE_ERROR ((LM_ERROR, ACE_TEXT ("No readers overlapped!\n"))); ACE_END_TEST; } return 0; }