Click here to Skip to main content
13,248,562 members (88,352 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 28 Jan 2002

Sample tutorial about application of SocketPro for client and server sides development

, 23 Feb 2002
Rate this:
Please Sign up or sign in to vote.
Development guide with SocketPro


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 following work:

  1. 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.
  2. #define     HOST_DNS_SVS_SOCKET                0xA00100DD   // unique
    #define     HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYNAME    1   //slow action
    #define     HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYADDR    2   //slow action
    #define     HOST_DNS_SVS_METHOD_ID_GET_COUNT_CLIENTS    3   //slow or fast at run time
    #define     HOST_DNS_SVS_METHOD_ID_GET_CLIENTS          4   //fast action
    class CHostDnsSvs : public CSockWithThread
        //Three lengthy actions invloved
        SockMethodID m_pMethodIDs[3];   
        CSockThreadInfo m_pHostThreadInfo[2];
           //for processing the request HOST_DNS_SVS_METHOD_ID_GET_CLIENTS
        //using the main thread/message queue if not many clients are available
        virtual bool GetClients();
        //for processing the request HOST_DNS_SVS_METHOD_ID_GET_COUNT_CLIENTS
        virtual bool GetCountClients();
        //must be overwrite
        virtual bool QuickProcess(SockMethodID nMethodID);
        //used by two threads/message queues for processing lengthy actions
        static HRESULT GetHostByNameProc(SOCKET hSocket, SockMethodID nMethodID, 
                 void *pBuffer, DWORD nBufferLen, 
                 void*       pSockWithThread);
        static HRESULT GetHostByIPAddrProc(SOCKET hSocket, SockMethodID nMethodID, 
                 void *pBuffer, DWORD nBufferLen, 
                 void*      pSockWithThread);

    Additionally, see how to set thread/message queue structures in the following at the file HostDnsSvs2.cpp:

        //The class used by the main thread/message queue to dynamically create the class objects
        //once required from a client
        //3 lengthy requests will be processed using worker thread/message queues
        //The first thread/queue uses the function, GetHostByNameProc,
        //to process two requests, HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYNAME
        //The 2nd thread/message queue uses the function GetHostByIPAddrProc,
        //to process one request, HOST_DNS_SVS_METHOD_ID_GETDNSNAME_BYADDR only
        //set the pointer m_pSockThreadInfo to the 2 thread info structues
        //2 worker thread/message queues will be involved at most

    Finally, see how to overwrite the function QuickProcess. Especially, pay attention to how to differentiate quick and lengthy actions.

    bool CHostDnsSvs::QuickProcess(SockMethodID nMethodID)
        switch (nMethodID)
                return GetCountClients();   //always true
                //true or fase
                //if true, the request is processed using main thread/queue
                //if false, it is processed using worker thread/message queue
                return GetClients();   
        return false;

  3. Code functions to process your requests.
    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.
  4. 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.

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 use if((lEvents & FD_READ) != FD_READ). 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)

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 client application.
  • 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::GetFile, 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 application.

If you need us to do something for you, give us your message using email <a href=""> Service is available now!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Yuancai (Charlie) Ye
Software Developer (Senior)
United States United States
Yuancai (Charlie) Ye, an experienced C/C++ software engineer, lives in Atlanta, Georgia. He is an expert at continuous inline request/result batching, real-time stream processing, asynchronous data transferring and parallel computation for the best communication throughput and latency. He has been working at SocketPro ( for more than fifteen years.

You may also be interested in...


Comments and Discussions

GeneralSocketPro updated with doc and more samples Pin
Yuancai (Charlie) Ye5-Apr-02 6:30
memberYuancai (Charlie) Ye5-Apr-02 6:30 
Questionwhy not open source? Pin
Bruise22-Feb-02 0:51
memberBruise22-Feb-02 0:51 
AnswerRe: why not open source? Pin
Yuancai (Charlie) Ye22-Feb-02 17:20
memberYuancai (Charlie) Ye22-Feb-02 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 24 Feb 2002
Article Copyright 2002 by Yuancai (Charlie) Ye
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid