#ifndef SOCKS_H #define SOCKS_H ///////////////////////////////////////////////////////////////////// // socks.h - socket client and multi-threaded server // // prototype for project #2 // // ver 1.3 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// /* Module Operations: ================== This module provides sockClt and sockSvr classes that create and use socket connections, using TCP/IP. To make connection you must create and start a server before attempting to connect a client. Public Interface - sockSvr: ============================ sockSvr server; // create socket server object server.start(); // start listing for incoming // connection requests Public Interface - sockClt: ============================ sockClt client; // create socket client object client.connectTo("127.0.0.1"); // connect to server at some IP client << "this is a message"; // send string to server */ // ///////////////////////////////////////////////////////////////////////// // Build Process for socks test stubs // ///////////////////////////////////////////////////////////////////////// // Required Files: // // socks.h, socks.cpp, sysUtils.h, sysUtils.cpp, // // lockingPtr.h, lockingPtr.cpp, threads.h, threads.cpp // // // // Compiler: // // - create console projects sockServer and sockClient // // - add the socks module files, listed above, to both projects // // add the following settings to both projects // // - Project Settings\C/C++\category=code generation // // \Use run-time library\Debug Multithreaded // // - Project Settings\Link\Object/library modules: // // add wsock32.lib to end of list, preceded by a space // // // // Add the following setting to the sockServer project: // // - Project Settings\C/C++\category=general\Processor Definitions: // // add TEST_SOCKS_SERVER to end of list, preceded by a comma // // // // Add the following setting to the sockClient project: // // - Project Settings\C/C++\category=general\Processor Definitions: // // add TEST_SOCKS_CLIENT to end of list, preceded by a comma // // // // - Set Active Project to sockServer // // - compile, link, and run // // // // - Set Active Project to sockClient // // - compile, link, and run // // // // or, from command line: // // // // msdev sockServer.dsp /make /rebuild // // msdev sockClient.dsp /make /rebuild // // debug/sockServer // // debug/sockClient // // // ///////////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.3 : 06 Nov 2001 - Added a receive thread in client to listen for server messages. This is the first working demonstration of the thread class. ver 1.2 : 04 Nov 2001 - added two-way communication between client and server to their test stub's code - added trace commands ver 1.1 : 24 Oct 2001 - added manual and maintenance pages ver 1.0 : 23 Oct 2001 - first release Planned Modifications ===================== This version supports sending strings from multiple clients to a server and back to the clients. All the server does is display the received strings in a console window. The next update will include: - a means to provide custom thread procedures in the server for each client. */ // #include #include /////////////////////////////////////////////////////////////// // report WinSock errors // /////////////////////////////////////////////////////////////// enum reportMethod { console, window, exception }; void reportError(reportMethod rm); /////////////////////////////////////////////////////////////// // socket client class declaration // /////////////////////////////////////////////////////////////// const int MAX_MSG_SIZE = 1024; class sockSvr { public: sockSvr(int port); virtual ~sockSvr(); void start(); bool good(); void clear(); protected: SOCKET _listener; int _listeningPort; const char *_clientAddress; bool _good; static unsigned int __stdcall threadProc(void *pArgs); static int send(SOCKET sock, const char *block); private: sockSvr(const sockSvr&); // will not implement sockSvr& operator=(const sockSvr&); // will not implement }; inline bool sockSvr::good() { return _good; } inline void sockSvr::clear() { _good = true; } // /////////////////////////////////////////////////////////////// // socket client class declaration // /////////////////////////////////////////////////////////////// typedef std::string address; class sockClt { public: sockClt(); virtual ~sockClt(); bool connectTo(address &addr, int port); bool connectTo(const char *s, int port); sockClt& operator<<(std::string &s); sockClt& operator<<(const char *s); sockClt& operator>>(std::string &s); void shutDownRecv(); void shutDownSend(); void shutDown(); void close(); bool good(); void clear(); protected: SOCKET _client; int _port; bool _good; int send(const char *block); private: sockClt(const sockClt&); // will not implement sockClt& operator=(const sockClt&); // will not implement }; inline bool sockClt::good() { return _good; } inline void sockClt::clear() { _good = true; } #endif