00001 // Filename: test_udp.cxx 00002 // Created by: drose (08Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "queuedConnectionManager.h" 00020 #include "recentConnectionReader.h" 00021 #include "connectionWriter.h" 00022 #include "netAddress.h" 00023 #include "connection.h" 00024 #include "netDatagram.h" 00025 00026 #include "datagram_ui.h" 00027 00028 int 00029 main(int argc, char *argv[]) { 00030 if (argc != 3) { 00031 nout << "test_udp host port\n"; 00032 exit(1); 00033 } 00034 00035 string hostname = argv[1]; 00036 int port = atoi(argv[2]); 00037 00038 NetAddress host; 00039 if (!host.set_host(hostname, port)) { 00040 nout << "Unknown host: " << hostname << "\n"; 00041 } 00042 00043 QueuedConnectionManager cm; 00044 PT(Connection) c = cm.open_UDP_connection(port); 00045 00046 if (c.is_null()) { 00047 nout << "No connection.\n"; 00048 exit(1); 00049 } 00050 00051 nout << "Successfully opened UDP connection on port " 00052 << c->get_address().get_port() << " and IP " 00053 << c->get_address() << "\n"; 00054 00055 RecentConnectionReader reader(&cm); 00056 reader.add_connection(c); 00057 ConnectionWriter writer(&cm, 1); 00058 00059 NetDatagram datagram; 00060 cout << "Enter a datagram.\n"; 00061 cin >> datagram; 00062 00063 bool lost_connection = false; 00064 00065 while (!cin.fail() && !lost_connection) { 00066 // Send the datagram. 00067 writer.send(datagram, c, host); 00068 00069 // Check for a lost connection. 00070 while (cm.reset_connection_available()) { 00071 PT(Connection) connection; 00072 if (cm.get_reset_connection(connection)) { 00073 nout << "Lost connection from " 00074 << connection->get_address() << "\n"; 00075 cm.close_connection(connection); 00076 if (connection == c) { 00077 lost_connection = true; 00078 } 00079 } 00080 } 00081 00082 // Now poll for new datagrams on the socket. 00083 while (reader.data_available()) { 00084 if (reader.get_data(datagram)) { 00085 nout << "Got datagram " << datagram << "from " 00086 << datagram.get_address() << "\n"; 00087 datagram.dump_hex(nout); 00088 } 00089 } 00090 00091 cout << "\nEnter a datagram.\n"; 00092 cin >> datagram; 00093 } 00094 nout << "Exiting.\n"; 00095 00096 return (0); 00097 } 00098 00099 00100 00101 00102