// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// C++ Source Code File Name: server.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.

Simple datagram server demo using the vbSocket class.
*/
// ----------------------------------------------------------- // 
#include <iostream.h>
#include <stdlib.h>
#include "vbsocket.h"

int main(int argc, char **argv)
{
  // Check arguments. Should be only one: the port number to bind to.
  if(argc != 2) {
    cerr << "Usage: " << argv[0] << " port" << endl;
    return 1;
  }

  unsigned short port = (unsigned short) atoi(argv[1]);

  cout << "Construcing datagram server..." << endl;
  vbSocket server(SOCK_DGRAM, port);
  if(!server) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }
  
  // Get the host name assigned to this machine (statistical info only)
  char hostname[vbsMAX_NAME_LEN];
  int rv = server.GetHostName(hostname);
  if(rv < 0) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }

  cout << "Opening stream server on host " << hostname << endl;
  cout << "Listening on port " << port << endl;

  // Bind the name to the socket
  rv = server.Bind();
  if(rv < 0) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }
 
  const int buf_len = 255; // Receive buffer size
  char sbuf[buf_len];
  rv = server.RawReadFrom(sbuf, buf_len); // Non-blocking read
  if(rv < 0) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }
  
  // Get the client info (statistical info only)
  char client_name[vbsMAX_NAME_LEN]; int r_port = -1;
  server.GetClientInfo(client_name, r_port);
  cout << "Receiving data from " << client_name 
       << " remote port " << r_port << endl << flush;

  cout << "Received " << rv << " bytes" << endl;

  // Print the buffer contents to the stdout for visual conformation
  sbuf[rv] = 0; // Null termainate the buffer
  cout << sbuf;

  cout << "Exiting..." << endl;
  server.Close(); // Close the socket connection
  server.ReleaseSocketLibrary();
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //