SimpleServer - Help

  1. The Main Page
  2. The Batch File Page
  3. The Programming Page

Programming

You don't have to use any programming to use SimpleServer. All it could do is launch a program for a set time period, then close the program it launched. That itself has some uses. The intended use of SimpleServer is to "talk" to it from a web page. You will have to use a programming language like PHP, perl, java, ASP, etc. to communicate with SimpleServer, or use the included program, SimpleClient.exe, without the need of a web page or any programming. Included are some PHP code examples below and full example web pages in the programs folder, that you are free to use and modify. SimpleClient.exe is also located in the programs folder. It was written to easily test SimpleServer's ability to pass a string of text, or arguments, to the program it launches, mainly batch files and Microsoft Windows Script files.
NOTE: Pressing SimpleServer's "Start Now" button will launch the program without passing any arguments.
[ top ]

A test batch file...

Let's write a batch file named, "test1.bat", that takes an argument. To make it simple, put the batch file in the same directory that "SimpleServer.exe" is. Refer to the batch file page on how to create a batch file. Copy the text in the box below to the batch file. in this case we will be passing the one word argument, "restart". The batch file will find it in the "%1" variable. Had we passed it the string "one two", "%1" would contain "one" and "%2" would contain "two".

test1.bat
@echo off
rem title test
if "%1" == "" goto error
if "%1" == "exit" goto exit
if "%1" == "restart" goto restart

:error
@echo Syntax error! Expecting: "test.bat exit" or "test.bat restart"
goto end

:exit
@echo Exiting Windows...
goto end

:restart
@echo Restarting Windows...


:end
pause

When SimpleServer runs "test2.bat", the calculator or notepad can be launched depending on what argument is passed to it.

test2.bat
@echo off
if "%1" == "calc" goto calc
if "%1" == "notepad" goto notepad
goto error

:calc
Start calc
exit

:notepad
Start notepad
exit

:error
@echo Syntax error! Expecting: "calc" or "notepad" on the command line!
pause
exit
[ top ]
A test ".vbs" file...

Let's write a ".vbs" file named, "test.vbs", that takes arguments. To make it simple, put the file in the same directory that "SimpleServer.exe" is. When "test1.vbs" is run, all it does is display the arguments passed to it.

test1.vbs
Dim arg

If WScript.Arguments.Count = 0 Then
   WScript.Echo  "no argument on the command line."
Else
   For each arg in WScript.Arguments  
      WScript.Echo "arg : "  & arg 
   Next
End If

When SimpleServer runs "test2.vbs", the calculator or notepad can be launched depending on what argument is passed to it.

test2.vbs
Dim WshShell, oExec, arg

If WScript.Arguments.Count = 1 Then
    Set WshShell = CreateObject("WScript.Shell")
    Set arg = WScript.Arguments

    If arg.Item(0) = "calc" Then
        Set oExec = WshShell.Exec("calc")
        WScript.Quit(0)
    Else 
        If arg.Item(0) = "notepad" Then
            Set oExec = WshShell.Exec("notepad")
            WScript.Quit(0)
        End If
    End If
End If

WScript.Echo  "One argument is required on the command line." & vbCrLf &_
"Expecting: 'calc' or 'notepad'"
[ top ]
SimpleServer.ini...

If SimpleServer is running, shut it down before proceeding. To set SimpleServer to use an argument, you can edit its ".ini" file, "SimpleServer.ini", or check the checkbox on the config dialog. If you would prefer to make the changes manually, follow these instructions... Don't change the "[Data]" line or anything left of the "=" characters. If you really mess up the ".ini" file, delete it. The next time you run SimpleServer, press the "Config" button. When you press the "Save" button, a "SimpleServer.ini" file will be created.

Let's look at the ".ini" file line by line.

  1. Skip the "[Data]" line.
  2. Set the "Port=" value to any free port (1-65535).
  3. Set the "password=" to "password" Normally it would be a word or combination of words, numbers, special characters, etc.
  4. Set the "Caption=" to "test". Normally it would be the exact wording in the title bar of the main window of the program that is launched.
  5. Set the "Path=" line to the complete path to batch file you just created. If the batch file is in the same folder as SimpleServer, just use it's name, "test.bat".
  6. Set the "Program Name/Type=" to "batch file" to describe what we are running.
  7. Set the "Show alert=" to "true" to pop up an alert when it launches the batch file, or "false" to not.
  8. Set the "Duration in minutes=" value to "1". Normally it should be how long you want the program launched to run before it is closed.
  9. Set the "Logfile=" line to be "true" to keep a record of the communication with SimpleServer (timestamp, originator's IP, etc), or "false" to not.
  10. Set the "Argument=" line to be "true". This has to be set to "true", or the arguments will not be passed. This is an "advanced" feature and is now, since version 1.1, editable from SimpleServer's config screen.

[ top ] 

Now for "SimpleClient.exe"...

  1. Run "SimpleClient.exe"
  2. Type in "password", enter, then "restart".
  3. Type in the port you chose to use.
  4. The IP Address should be correct, if not, use "127.0.0.1".
  5. It should look similar to the picture below.
SimpleServer.ini
[Data]
Port=12345
Password=password  
Caption=test
Path=test.bat
Program Name/Type=batch file   
Show Alert=true
Duration in minutes=1
Logfile=true
Argument=true

Restart SimpleServer. It will now be set to send the second line of text it receives to the program it launches, in this case, our batch file. If it errors and complains about another program is listening on the port you chose, press the "Config" button and change it to another number. Then change SimpleClient to that port. Make sure SimpleServer is listening on the same port that SimpleClient is set to send to.

We are ready to test it now. Press SimpleClient's "Send" button. The batch file should run.

Troubleshooting...

  1. Make sure SimpleServer is listening on the same port that SimpleClient is set to send to.
  2. Set SimpleClient's "IP Address" to "127.0.0.1".
  3. Make sure "Argument=true" is in the "SimpleServer.ini" file, or its corresponding checkbox is checked in the "Config" dialog.
  4. If you edit "SimpleServer.ini", you will have to restart "SimpleServer.exe".
  5. If SimpleServer says "...Check program path.", correct the path to the batch file.
[ top ]
PHP

SimpleServer is listening on a user defined port on your computer. To communicate with it, you need to open a TCP socket connection to your computer's IP address at that port. Below is the simplest PHP code to accomplish this:

connect.php
<?
$ip 
"192.168.1.2";      // IP address to connect to
$port 12345;            // Port that SimpleServer is listening on. Between 1 and 65535
$to 2;                  // Timeout in seconds to try to open the socket
$pass "password";       // Password to send to SimpleServer
$argument "restart";    // Any extra arguments

if ($con @fsockopen($ip$port, &$errorNum, &$errorDesc$to))
{
    
// Success, connected.

    // Send password.
    
fputs($con$pass);

    
// Or, with argument
    // fputs($con, "$pass\n$argument");

    // Close socket
    
fclose($con);
}
else
{
    
// Error, no connection
}

?>

If you want the easiest way to communicate with SimpleServer from a web page, look at the sample form page. Other code examples and sample web pages can be found in the programs folder.
[ top ]

 

[ << The start page. | The batch file page >> ]