This short article is written for coding
client-server applications with help of the 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.
Analysis of sample scenario
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.
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.
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.
Server side coding
To develop socket server application with
SocketPro, please take a time to read the short article, Fundamentals
about data communication using socket with SocketPro. You need to do the
- 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.
#define HOST_DNS_SVS_SOCKET 0xA00100DD #define HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYNAME 1 #define HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYADDR 2 #define HOST_DNS_SVS_METHOD_ID_GET_COUNT_CLIENTS 3 #define HOST_DNS_SVS_METHOD_ID_GET_CLIENTS 4
class CHostDnsSvs : public CSockWithThread
virtual bool GetClients();
virtual bool GetCountClients();
virtual bool QuickProcess(SockMethodID nMethodID);
static HRESULT GetHostByNameProc(SOCKET hSocket, SockMethodID nMethodID,
void *pBuffer, DWORD nBufferLen,
static HRESULT GetHostByIPAddrProc(SOCKET hSocket, SockMethodID nMethodID,
void *pBuffer, DWORD nBufferLen,
Additionally, see how to set thread/message
queue structures in the following at the file HostDnsSvs2.cpp:
Finally, see how to overwrite the function
QuickProcess. Especially, pay attention to how to differentiate quick and lengthy actions.
bool CHostDnsSvs::QuickProcess(SockMethodID nMethodID)
return GetCountClients(); break;
- Code functions to process
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.
- 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
In-process ATL COM component
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
if((lEvents & FD_READ) != FD_READ). If a call is non-blocking, you should
handle returned binary results inside the function call
CDNS::OnDataAvailable(long hSocket, long lBytes, long lError).
Sample VB client application
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.
Advantages of the sample clinet-server system
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!
All of applications included with
this particular package are free with the following limitations:
- 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.
- The file ClientSocket.dll is hard-coded to support 2 socket connections only from one
- The client COM dlls, SBroker.dl and DResolver.dll, are totally free to you.
- The client COM dll, SockFile.dll, is free to you except that four functions,
ISocketFile::SendFile, and ISocketFile::WriteText and
ISocketFile::WriteChunk, are disabled after 60 days.
- 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
If you need us to do something for you, give us your message using email email@example.com. Service is available now!