SocketServer.cpp

Go to the documentation of this file.
00001 
00014 // SocketServer.cpp
00015 
00016 #pragma warning( disable: 4786 )
00017 #include "networkApp.h"
00018 #include <iostream>
00019 #include <string>
00020 
00022 // InitNetwork
00023 
00024 // Initializes windows socket libraries.  This should be a part of the framework, but
00025 // I haven't decided a good way to do it yet.
00026 
00027 #ifdef WIN32
00028 #include <process.h>
00029 int initSocketLayer()
00030 {
00031     int retval = 0;
00032         // Initialize the winsock environment
00033         WORD ver_request = MAKEWORD( 2, 2 );
00034         WSADATA wsa_data;
00035 
00036         // Initialize the winsock environment.
00037         if ( WSAStartup( ver_request, &wsa_data ) )
00038     {
00039         // Failed to startup WinSock
00040                 return -1;
00041     }
00042         // Confirm the winsock environment supports at least version 2.2.
00043     long nMajor = LOBYTE( wsa_data.wVersion );
00044     long nMinor = HIBYTE( wsa_data.wVersion );
00045         if ( 2 < nMajor )
00046     {
00047         // We can't support anything with a major value under 2.  Goodbye.
00048         retval = -1;
00049     }
00050     else if ( 2 == nMajor )
00051     {
00052         // Make sure that the minor value is at least 2
00053         if ( 2 > nMinor )
00054         {
00055             retval = -1;
00056         }
00057     }
00058     // All other versions should work.  Let's hope that future versions don't break
00059     // the application.
00060     else
00061     {
00062         std::cerr << "The version of winsock is newer than what we expected." << std::endl;
00063     }
00064     if ( 0 < retval )
00065     {
00066                 WSACleanup();
00067     }
00068     return retval;
00069 }
00070 
00071 void closeSocketLayer()
00072 {
00073     WSACleanup();
00074 }
00075 
00076 #else
00077 // cop out on all other systems
00078 
00079 int initSocketLayer()
00080 {
00081         return 0;
00082 }
00083 void closeSocketLayer()
00084 {
00085 }
00086 #endif // OS specific socket init
00087 
00088 
00090 // main c++ program entry point
00091 
00092 int NetworkStart(int argc, char* argv[], int (networkAppMain)( int argc, char* argv[] ))
00093 {
00094     int retval = 0;
00095     retval = initSocketLayer();
00096 
00097     if( 0 == retval )
00098     {
00099             // load local information
00100             Host::getLocalhost();
00101             IpAddress::getLocalIpAddress();
00102         
00103         retval = networkAppMain( argc, argv );
00104 
00105         closeSocketLayer();
00106     }
00107    
00108     return retval;
00109 }

Generated on Tue Mar 28 09:10:15 2006 for Simple SOAP by  doxygen 1.4.6