// ------------------------------- // // -------- Start of File -------- // // ------------------------------- // // ----------------------------------------------------------- // // C++ Source Code File Name: client.cpp // C++ Compiler Used: MSVC40, HP CPP 10.24, egcs-2.90.29 // Produced By: Doug Gaer // File Creation Date: 09/20/1999 // Date Last Modified: 06/21/2000 // Copyright (c) 2000 Douglas M. Gaer // ----------------------------------------------------------- // // ------------- Program Description and Details ------------- // // ----------------------------------------------------------- // /* The VBS C++ classes are copyright (c) 2000, by Douglas M. Gaer. All those who put this code or its derivatives in a commercial product MUST mention this copyright in their documentation for users of the products in which this code or its derivative classes are used. Otherwise, you have the freedom to redistribute verbatim copies of this source code, adapt it to your specific needs, or improve the code and release your improvements to the public provided that the modified files carry prominent notices stating that you changed the files and the date of any change. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE, YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. This program is used to the vbDatagram class. */ // ----------------------------------------------------------- // #include <iostream.h> #include <stdlib.h> #include <string.h> #include "vbdatagm.h" const char *test_block = "The quick brown fox jumps over the lazy dog \ 0123456789\n"; void SkipToEol(istream &s) // Used to clear istream { char c; s.clear(); while(s.get(c) && c != '\n') { ; } } void PrgMenu() { cout << endl; cout << "(a, A) Add a variable block" << endl; cout << "(c, C) Change a variable block" << endl; cout << "(d, D) Delete a variable block" << endl; cout << "(l, L) Send a client close command" << endl; cout << "(h, H, ?) Help (prints this menu)" << endl; cout << "(k, K) Shutdown the server" << endl; cout << "(q, Q) Quit this program" << endl; cout << "(r, R) Request a variable block" << endl; cout << "(s, S) Send a raw block of data" << endl; cout << "(w, W) Send an acknowledgment block" << endl; cout << endl; } void Acknowledge(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Requesting acknowledgment block from the server" << endl; if(client.WriteAckBlock() != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } if(client.ReadAckBlock() != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Received an acknowledgment from the server " << endl; client.Close(); } void CloseCommand(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Closing the connection..." << endl; if(client.CloseConnection() != 0) cout << client.SocketExceptionMessage() << endl; client.Close(); } void DeleteBlock(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Sending a request to delete a remote block" << endl; char *request = "DOG"; // Arbitary block ID vbBlockHeader vb; vb.Length = strlen(request); if(client.DeleteBlock((char *)request, vb) != 0) cout << client.SocketExceptionMessage() << endl; client.Close(); } void AddBlock(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Sending a request to add a remote block" << endl; vbBlockHeader vb; vb.Length = strlen(test_block); if(client.AddBlock((char *)test_block, vb) != 0) cout << client.SocketExceptionMessage() << endl; client.Close(); } void ChangeBlock(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Sending a request to change a remote block" << endl; char *request = "CAT"; // Arbitary block ID vbBlockHeader request_header; request_header.Length = strlen(request); vbBlockHeader block_header; block_header.Length = strlen(test_block); if(client.ChangeBlock((char *)request, (char *)test_block, request_header, block_header) != 0) cout << client.SocketExceptionMessage() << endl; client.Close(); } void ShutDownServer(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Shutting down the server..." << endl; if(client.TerminateConnection() != 0) cout << client.SocketExceptionMessage() << endl; client.Close(); } void RequestBlock(int port, char *host) { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Requesting a block from the server" << endl; char *request = "FOX"; // Arbitary block ID vbBlockHeader request_header; request_header.Length = strlen(request); vbBlockHeader requested_block_header; request_header.Length = strlen(request); void *block = client.RequestBlock((char *)request, request_header, requested_block_header); if(!block) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Received requested block" << endl; cout.write((char *)block, requested_block_header.Length); client.Close(); } void SendBlock(int port, char *host) // Send a block of data { vbDatagram client; if(client.DatagramClient(port, host) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } cout << "Sending a block " << strlen(test_block) << " bytes long..." << endl; if(client.WriteBlock((char *)test_block, strlen(test_block)) != 0) { cout << client.SocketExceptionMessage() << endl; client.Close(); return; } client.Close(); } int main(int argc, char **argv) { if(argc < 3) { cerr << "Usage: " << argv[0] << " hostname port" << endl; return 1; } unsigned short port = (unsigned short) atoi(argv[2]); char *host = argv[1]; char key; PrgMenu(); int rv = 1; while(rv) { if (!cin) { // Input is in fail state SkipToEol(cin); // Go to end of line if (!cin) { // Can't fix cout << "Input stream is broken" << endl; return 0; } } cout << '>'; cin >> key; if (!cin) continue; // Fix at top of loop switch(key) { case '?' : PrgMenu(); break; case 'h' : case 'H' : PrgMenu(); break; case 'q' : case 'Q' : rv = 0; break; case 'a' : case 'A' : AddBlock(port, host); break; case 'c' : case 'C' : ChangeBlock(port, host); break; case 'd' : case 'D' : DeleteBlock(port, host); break; case 'k' : case 'K' : ShutDownServer(port, host); break; case 'r' : case 'R' : RequestBlock(port, host); break; case 's' : case 'S' : SendBlock(port, host); break; case 'l' : case 'L' : CloseCommand(port, host); break; case 'w' : case 'W' : Acknowledge(port, host); break; default: cout << "Unrecognized command" << endl; } } return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //