/////////////////////////////////////////////////////////////////////////// // Client.cpp - Test DLL Packaging, using Sockets module // // version 1.0 // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2006 // /////////////////////////////////////////////////////////////////////////// #include #include "ISockets.h" void main() { std::cout << "\n Testing DLL Packaging of Socket, and SocketListener classes"; std::cout << "\n =============================================================\n"; ///////////////////////////////////////////////////////// // load DllPackage.dll library HMODULE hmod = LoadLibrary(L"DllPackage"); if(!hmod) { std::cout << "\n library load failed"; return; } ///////////////////////////////////////////////////////// // find ISocketSystem factory function Create() typedef ISocketSystem* (*SSFF)(); SSFF CreateSS = (SSFF)GetProcAddress(hmod,"?Create@ISocketSystem@@SAPAV1@XZ"); if(!CreateSS) { std::cout << "\n GetProcAddress failed for SocketSystem::Create"; return; } ISocketSystem* pSocketSystem = CreateSS(); ///////////////////////////////////////////////////////// // find ISocket factory function Create() typedef ISocket* (*SFF)(); SFF CreateS = (SFF)GetProcAddress(hmod,"?Create@ISocket@@SAPAV1@XZ"); if(!CreateS) { std::cout << "\n GetProcAddress failed for Socket::Create"; return; } ISocket* pSendr = CreateS(); // ///////////////////////////////////////////////////////// // find ISocketListener factory function Create() typedef ISocketListener* (*SLFF)(); SLFF CreateSL = (SLFF)GetProcAddress(hmod,"?Create@ISocketListener@@SAPAV1@XZ"); if(!CreateSL) { std::cout << "\n GetProcAddress failed for SocketListener::Create"; return; } ISocketListener* pSocketListener = CreateSL(); try { ISocketSystem* pSocketSystem = CreateSS(); std::string host = pSocketSystem->getHostName(); std::cout << "\n host machine name: " << host.c_str(); std::string ip = pSocketSystem->getIpFromName(host); std::cout << "\n IP Address of machine: " << ip.c_str(); std::string name = pSocketSystem->getNameFromIp(ip); std::cout << "\n DNS name of machine from ip: " << name.c_str() << '\n'; std::cout << "\n connecting to listener"; std::cout << "\n ------------------------"; pSocketListener->attach(2048); if(!pSendr->connect(name,2048)) // can use ip addr, e.g., 127.0.0.1 { std::cout << "\n can't connect to " << name.c_str() << "\n\n"; return; } ISocket* pRecvr = pSocketListener->waitForConnect(); std::cout << "\n remote ip is: " << (pRecvr->getRemoteIP()).c_str(); std::cout << ", remote port is: " << pRecvr->getRemotePort(); std::cout << "\n local ip is: " << (pRecvr->getLocalIP()).c_str(); std::cout << ", local port is: " << pRecvr->getLocalPort() << '\n'; pRecvr->setTerminator(""); std::cout << "\n sending from connector to listener"; std::cout << "\n ------------------------------------"; pSendr->send("this is a messageand another message"); pSendr->send("and a final message"); pSendr->send("quit"); while(true) { std::string msg = pRecvr->recv(); std::cout << "\n received \"" << msg.c_str(); if(msg.compare("quit") == 0) break; } std::cout << "\n"; // // demonstrating full duplex operation std::cout << "\n sending from listener back to connector"; std::cout << "\n -----------------------------------------"; pSendr->setTerminator("\n"); pRecvr->send("sending message back\nquit\n"); while(true) { std::string msg = pSendr->stripTerminator(pSendr->recv()); std::cout << "\n received \"" << msg.c_str(); if(msg.compare("quit") == 0) break; } pSendr->disconnect(); pRecvr->disconnect(); std::cout << "\n\n"; delete pSocketSystem; delete pSocketListener; delete pSendr; delete pRecvr; FreeLibrary(hmod); } catch(std::exception& e) { std::cout << "\n " << e.what() << "\n\n"; FreeLibrary(hmod); } }