<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>Sample tutorial about application of SocketPro for server side development</title>
</head>
<body>
<h3 align="center">Sample tutorial about application of SocketPro for client and server
sides development</h3>
<ol>
<li>Purpose<br>
This short article is written for coding
client-server applications with help of SocketPro package. You are expected to be familiar
with C++, COM and VB. This sample contains three parts of code, a server socket
application, an in-process ATL COM component, and a VB test client application. <br>
</li>
<li>Analysis of sample scenario<br>
The sample server socket application
provides multiple services to multiple clients. These services include: 1) exchange of
files between client and server, 2) remote management of files, 3) use of server
proxy/broker on behalf of a client for executing various commands, and 4) host resolving.
The tutorial provides detailed steps to C++ code for hosting resolving.<br>
The in-process ATL COM component is a wrapper
written from a base ATL COM component, ClientSocket.dll, with C++. You can use these
COM-based components with any languages supporting COM technology.<br>
The sample VB test application builds upto two
connections to the sample server socket application. When clicking a button, two sets of
server names and ip addresses are sent to the sample server application in batch. The two
connections run asynchronously, independently and in parallel. Whenever a result comes
from the server, the VB application display it. <br>
</li>
<li>Server side coding<br>
To develop socket server application with
SocketPro, please take a time to read the short article, <a href="fundamentals.htm"><em>Fundamentals
about data communication using socket with SocketPro</em></a>. You need to do the
following work:<br>
<br>
A. Define class and method
ids, and tell SocketPro what requests are lengthy actions and what requests are quick
actions, and set up one (at most cases) or more (at extreme cases) structures. See the
decalarations of the following code at file HostDnsSvs.h, and pay attention to comments.<br>
<br>
<font color="#0000FF"> #define</font>
HOST_DNS_SVS_SOCKET
0xA00100DD
<font color="#008000"> //unique</font><br>
<font color="#0000FF">#define</font>
HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYNAME 1
<font color="#008000">//slow action</font><br>
<font color="#0000FF">#define</font>
HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYADDR 2
<font color="#008000">//slow action</font><br>
<font color="#0000FF">#define</font>
HOST_DNS_SVS_METHOD_ID_GET_COUNT_CLIENTS 3
<font color="#008000">//slow
or fast action at run time</font><br>
<font color="#0000FF">#define</font>
HOST_DNS_SVS_METHOD_ID_GET_CLIENTS
4
<font color="#008000">//fast action</font><br>
<br>
<font color="#0000FF">class</font> CHostDnsSvs
: public CSockWithThread <br>
{<br>
<font color="#0000FF">public:</font><br>
CHostDnsSvs();<br>
<br>
<font color="#0000FF">private:</font><br>
<font color="#008040">//Three lengthy actions invloved</font><br>
SockMethodID m_pMethodIDs[3];
<br>
CSockThreadInfo m_pHostThreadInfo[2];<br>
<br>
<font color="#0000FF">protected:</font><br>
<font
color="#008000">//for processing the request HOST_DNS_SVS_METHOD_ID_GET_CLIENTS <br>
//using the main thread/message queue if not many clients are available</font><br>
<font color="#0000FF">virtual bool</font> GetClients();<br>
<br>
<font color="#008040">//for processing the request
HOST_DNS_SVS_METHOD_ID_GET_COUNT_CLIENTS</font><br>
<font color="#0000FF">virtual bool</font> GetCountClients();<br>
<br>
<font color="#008000">//must be overwrite</font><br>
<font color="#0000FF">virtual bool</font> QuickProcess(SockMethodID nMethodID);<br>
<br>
<font color="#0000FF">private:</font><br>
<font color="#008000">//used by two threads/message queues for processing lengthy actions</font><br>
static HRESULT GetHostByNameProc(SOCKET hSocket, SockMethodID nMethodID, void *pBuffer,
DWORD nBufferLen, void*
pSockWithThread);<br>
static HRESULT GetHostByIPAddrProc(SOCKET hSocket, SockMethodID nMethodID, void *pBuffer,
DWORD nBufferLen, void*
pSockWithThread);<br>
};<br>
<br>
Additionally, see how to set thread/message
queue structures in the following at the file HostDnsSvs2.cpp:<br>
<br>
CHostDnsSvs::CHostDnsSvs()<br>
{<br>
<font color="#008000">//The class used by the main thread/message queue to dynamically
create the class objects<br>
//once required from a client</font><br>
m_nClassID=HOST_DNS_SVS_SOCKET;<br>
<br>
<font color="#008000">//3 lengthy requests will be processed using worker thread/message
queues</font><br>
m_pMethodIDs[0]=HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYNAME;<br>
m_pMethodIDs[1]=HOST_DNS_SVS_METHOD_ID_GET_CLIENTS;<br>
m_pMethodIDs[2]=HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYADDR;<br>
<br>
<font color="#008000">//The first thread/queue uses the function, GetHostByNameProc, <br>
//to process two requests, HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYNAME <br>
//and HOST_DNS_SVS_METHOD_ID_GET_CLIENTS</font><br>
m_pHostThreadInfo[0].m_pSlowProcess=GetHostByNameProc;<br>
m_pHostThreadInfo[0].m_pMethodIDs=m_pMethodIDs;<br>
m_pHostThreadInfo[0].m_nCountOfMethods=2;<br>
<br>
<font color="#008000">//The 2nd thread/message queue uses the function
GetHostByIPAddrProc,<br>
//to process one request, HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYADDR only</font><br>
m_pHostThreadInfo[1].m_pSlowProcess=GetHostByIPAddrProc;<br>
m_pHostThreadInfo[1].m_nCountOfMethods=1;<br>
m_pHostThreadInfo[1].m_pMethodIDs=(m_pMethodIDs+2);<br>
<br>
<font color="#008000">//set the pointer m_pSockThreadInfo to the 2 thread info structues</font><br>
m_pSockThreadInfo=m_pHostThreadInfo;<br>
<br>
<font color="#008000">//2 worker thread/message queues will be involved at most</font><br>
m_nCountOfThreadInfos=2; <br>
}<br>
<br>
At last, see how to overwrite the function
QuickProcess. Especially, pay attention to how to differentiate quick and lengthy actions.<br>
<br>
<font color="#0000FF">bool</font>
CHostDnsSvs::QuickProcess(SockMethodID nMethodID)<br>
{<br>
<font color="#0000FF">switch</font> (nMethodID)<br>
{<br>
<font color="#0000FF">case</font> HOST_DNS_SVS_METHOD_ID_GET_COUNT_CLIENTS:<br>
<font color="#0000FF">return</font> GetCountClients(); <font color="#008000">//always
true</font><br>
<font color="#0000FF">break</font>;<br>
<font color="#0000FF">case</font> HOST_DNS_SVS_METHOD_ID_GET_CLIENTS:<br>
<font color="#008000">//true or fase<br>
//if true, the request is processed using main thread/queue<br>
//if false, it is processed using worker thread/message queue</font><br>
<font color="#0000FF">return</font> GetClients(); <br>
<font color="#0000FF">break</font>;<br>
<font color="#0000FF">default</font>:<br>
<font color="#0000FF">break</font>;<br>
}<br>
<font color="#0000FF">return false</font>;<br>
}<br>
<br>
B. Code functions to process
your requests.<br>
It is not difficult to finish functions. As you
can see, your code doesn't deal with any thread/message queue side-effects, data
synchronization, dead lock and various message routing etc. The complete implementation is
inside the file HostDnsSvs2.cpp.<br>
<br>
Last finish-up step. See the files
MultipleStartSvs.h, MultipleStartSvs.cpp, MySocketSvr.h, MySocketSvr.cpp and MainProg.cpp
(or MultiSvs.h and MultiSvs.cpp from the NT service). You should pay attention to how
SocketPro can provide multiple services. The secret is the defined class id. For all of
SocketPro server application, you should use the same pattern code to finish up it with no
exception.<br>
<br>
</li>
<li>In-process ATL COM component<br>
This sample shows you how to write a COM object
wrapper from a base COM object, clientsocket.dll. The complete code and project
(DResolver) can be found inside the folder sample. Most of codes are associated with COM.
In regards to code related with host resolving, you need to pay attention to how to handle
both non-blocking and blocking calls. To determine if a call is blocking one, you need to
use <em>if((lEvents & FD_READ) != FD_READ)</em>. If a call is non-blocking, you should
handle returned binary results inside the function call HRESULT __stdcall
CDNS::OnDataAvailable(long hSocket, long lBytes, long lError).<br>
</li>
<li>Sample VB client application<br>
The VB sample project and code are placed in
the folder of samples\client\dnssvs\. Look at the code, you will see two socket
connections are built once clicking the button Connect. Both of the two connections run in
non-blocking mode. When clicking button Batch calls 1 or button Batch calls 2, you send a
batch of requests to the above SocketPro Server to process in parallel. The server knows
how to assign requests onto different threads/message queues. When clicking the button All
running in parallel, two sets of batch calls are sent to the SocketPro server. In the
server side, five threads/message queues work togather to process these requests
concurrently. It is notified that the SocketPro server threads will be killed about 60
second after all of these requests are processed. Certainly, you can click the button All
running in parallel again, and send another two sets of batch calls to the SocketPro
server for processing.<br>
</li>
<li>Advantages of the sample clinet-server system<br>
The system is designed with many advantages.
Among them, you could see BIG four advantages. They are speed, non-blocking/blocking,
parallel computation and no worker threads involved with your code at both client and
server side. In the server side, SocketPro handles all of threads, message queues and
others for you. The server code can't be simpler, and just can't be simpler! <br>
</li>
<li>Notes<p> All of applications included with
this particular package are free with the following limitations: <ul>
<li>The NetBaseR.dll enables you to build only two client socket connections to a server
from a client application. A SocketPro server application only supports accepting 32
client connections only. </li>
<li>The file ClientSocket.dll is hard-coded to support 2 socket connections only from one
client application. </li>
<li>The client COM dlls, SBroker.dl and DResolver.dll, are totally free to you. </li>
<li>The client COM dll, SockFile.dll, is free to you except that four functions,
ISocketFile::GetFile, ISocketFile::SendFile, and ISocketFile::WriteText and
ISocketFile::WriteChunk, are disabled after 60 days. </li>
<li>Authentication of both server applications, MultipleSvs.exe and MultiSvs.exe, are
disabled for your easy setup and testing. You should pay enough attention to security. If
not, someone may access your server machine files and manage them through the server
application. <br>
</li>
</ul>
<p>If you need us to do something for you, give us your message using email <a
href="yekerui@yahoo.com">yekerui@yahoo.com</a>. Service is available now!</p>
</li>
</ol>
</body>
</html>