Listener and Client Sockets/Remote Program Execution, etc.


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

This is a simple app to demonstrate listener and client Sockets/Remote program execution, and so on.


SockListener GUI

SockCommander GUI

Environment: VC6, NT4, Win9x

Hi Friends,

First of all, let me thank all the contributors to codeguru.com; your knowledge and expertise has helped me tremendously. You will see I'm using many code snippets from them in many of my applications.

I'm submitting this simple and very basic CSocket program due to the large number of requests I receive almost every day from the codeguru VC++ discussion board. Experienced programmers will find this simple and easy to develop, but I guess the beginners will get a good introduction to CSockets and its raw power.

There are two applications I'm submitting here: SockCommander (SC) and SockListener (SL). SL acts as the server and listens on port 777. This application must be run on that PC which you intend to control (well... in a way). Take down the IP address shown there and then execute SC on that PC from where you intend to control the server. Enter the IP address you got from the server in the relevant edit box in SC and press Connect. If successful, a message will be displayed both on SC and SL, saying 'Connected'. Now to the interesting part. You can now send commands/messages to the server from now onwards by just entering the command on the Command/Msg edit box and pressing the Send button.

At the moment commands available for SockCommander are:

Command Description
CMD: <your command> Sends a command thats to be executed at SockListener e.g CMD:Notepad.Exe will execute notepad at SL
FILEGET: <path of file to retrieve> Retrieves small files from SL. This is still under test stage, not to be used for big files. e.g FILEGET:c:\msdos.sys (don't forget to enter a valid filepath in the File Save as edit box)
!SHUT Shuts down SL

The possibilities of these apps are tremendous in the hands of a creative/thinking programmer!

All the best and Happy coding.


Demo and Source Code - 71 Kb


  • Why it can only work on local network but not internet

    Posted by ZJQ on 09/15/2007 01:47pm

    It works on 2 PC inside a local network. But I don't know why it can't work between 2 PC with Window-XP sp2. I close the firewall.

  • Network by Internet

    Posted by Legacy on 12/20/2003 08:00am

    Originally posted by: Wessels

    If am connected to the internet and start the programm SockListener the text in the border is 'Server' and not the IP-adress of my computer in the running internet-connection. 'Server' is the adress of my computer in the LAN.
    What I have to do ?
    The computer is running with Windows XP Prefessional.


  • Bug: Connect - Disconnect

    Posted by Legacy on 12/04/2003 08:00am

    Originally posted by: V.Srinivas

    M.Wittmann has rectified client code
    by not making client have fixed port.

    This works well.

    I have another problem:
    I changed the code, kept a "listen" & "Restart" buttons
    in the server.
    I have also created server socket object dynamically.
    when i press restart i closed the current object, and
    deallocated memory.

    but, when i again create the server object dynamically
    and made it "Listen". but it is not responding to any
    further clients.

  • How can I use socket it in a DLL project?

    Posted by Legacy on 07/15/2003 07:00am

    Originally posted by: H.Paitan


    I'm learning VC++ and I need make a DLL using Sockets in order to connect an Application server with a client, but the client should be coded in a DLL.


  • Adding Listbox to Commander?

    Posted by Legacy on 02/17/2003 08:00am

    Originally posted by: Devin

    This was a great demo, but I've run across a strange problem.

    In trying to facilitate bi-directional messaging, I overloaded OnReceive in the CCommandSocket class to read:

    void CCommandSocket::OnReceive(int nErrorCode)
    // TODO: Add your specialized code here and/or call the base class

    Then created a method in the CSockCommanderDlg class as such...

    void CSockCommanderDlg::ProcessPendingRead(CCommandSocket *pSocket)
    char *lpBuf;

    lpBuf = new char [1000];

    for(int i=0;i<1000;i++){
    *(lpBuf+i) = 0;

    pSocket->Receive( lpBuf, 1000);


    delete [] lpBuf;

    Things worked great when I just had the message popping up with a call to "AfxMessageBox", but when I tried adding it to a listbox using "CListBox::InsertString", I get Unhandled Exception 0xC0000005: Access Violation...

    If I add a string to the same listbox member variable from a different method in the same class, there's no problem, and this is almost identical to what is being done in the Listener App with no problems; I'm stumped. Am I doing something wrong, or is this a bug?

  • proxy

    Posted by Legacy on 01/23/2003 08:00am

    Originally posted by: Joan

    Is there any special consideration if any of the machines
    (client or server) is behind a proxy server??? For example,
    if my computer is served by a ADSL router with an static
    IP (so what you can see from inet is my router and not my

    a newbie ;)


  • i saw these code about 1year ago

    Posted by Legacy on 01/22/2003 08:00am

    Originally posted by: dave.cliff

    r u a cheater?

  • You can receive big file....

    Posted by Legacy on 07/30/2002 07:00am

    Originally posted by: davisHsu

    	m_ptrComSocket->Receive(&dwFlen, 4);	
    if(dwFlen == -1) return FALSE;
    gMem = GlobalAlloc(GHND, dwFlen);
    if(!gMem) {
    AfxMessageBox("Unable to alloc memory");
    byte* data;
    data = (byte*)GlobalLock(gMem);
    m_ptrComSocket->Receive(data, dwFlen);
    try this modified

    int nBytesRead=m_ptrComSocket->Receive(&dwFlen, 4);

    if(dwFlen == -1) return FALSE;

    gMem = GlobalAlloc(GHND, dwFlen);
    if(!gMem) {
    AfxMessageBox("Unable to alloc memory");
    byte* data;
    data = (byte*)GlobalLock(gMem);

    // m_ptrComSocket->Receive(data, dwFlen);
    while (dwFlen != nBytesRead) {
    int dataReadLen = m_ptrComSocket->Receive(data + nBytesRead, dwFlen - nBytesRead);
    nBytesRead += dataReadLen;
    };//end while

    Good luck!

  • remotecontrol

    Posted by Legacy on 07/16/2002 07:00am

    Originally posted by: VanTri

    Is there a way to control clients from server?

  • How can I use it through the internet?

    Posted by Legacy on 06/18/2002 07:00am

    Originally posted by: Asaf Schwarz

    Hi to all,
    I need an application like that, that can work through the internet.
    the listener will be located on a server and the commander on a regular pc. both are connected to the internet.
    does this application work on the net?
    Thanks for the attention...

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date