Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/net/test_tcp_client.cxx

Go to the documentation of this file.
00001 // Filename: test_tcp_client.cxx
00002 // Created by:  drose (09Feb00)
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 "queuedConnectionReader.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_tcp_client 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_TCP_client_connection(host, 5000);
00045 
00046   if (c.is_null()) {
00047     nout << "No connection.\n";
00048     exit(1);
00049   }
00050 
00051   nout << "Successfully opened TCP connection to " << hostname
00052        << " on port "
00053        << c->get_address().get_port() << " and IP "
00054        << c->get_address() << "\n";
00055 
00056   QueuedConnectionReader reader(&cm, 0);
00057   reader.add_connection(c);
00058   ConnectionWriter writer(&cm, 0);
00059 
00060   NetDatagram datagram;
00061   cout << "Enter a datagram.\n";
00062   cin >> datagram;
00063 
00064   bool lost_connection = false;
00065 
00066   while (!cin.fail() && !lost_connection) {
00067     // Send the datagram.
00068     writer.send(datagram, c);
00069 
00070     // Check for a lost connection.
00071     while (cm.reset_connection_available()) {
00072       PT(Connection) connection;
00073       if (cm.get_reset_connection(connection)) {
00074         nout << "Lost connection from "
00075              << connection->get_address() << "\n";
00076         cm.close_connection(connection);
00077         if (connection == c) {
00078           lost_connection = true;
00079         }
00080       }
00081     }
00082 
00083     // Now poll for new datagrams on the socket.
00084     while (reader.data_available()) {
00085       if (reader.get_data(datagram)) {
00086         nout << "Got datagram " << datagram << "from "
00087              << datagram.get_address() << "\n";
00088         datagram.dump_hex(nout);
00089       }
00090     }
00091 
00092     if (!lost_connection) {
00093       cout << "\nEnter a datagram.\n";
00094       cin >> datagram;
00095     }
00096   }
00097   nout << "Exiting\n";
00098 
00099   return (0);
00100 }
00101 
00102 
00103 
00104 
00105 

Generated on Fri May 2 00:40:37 2003 for Panda by doxygen1.3