Executing exe using Flex (socket)

Update Post, only for Windows

Java socket code:

import java.io.*;
import java.net.*;

class SimpleServer
{
private static SimpleServer server;
ServerSocket socket;
Socket incoming;
BufferedReader readerIn;
PrintStream printOut;
public static void main(String[] args)
{
int port = Integer.parseInt(args[0]);
server = new SimpleServer(port);
}
private SimpleServer(int port)
{
System.out.println(“>> Starting SimpleServer”);
try
{
socket = new ServerSocket(port);
for( ;; )
{
incoming = socket.accept();
readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
printOut = new PrintStream(incoming.getOutputStream());
Runtime.getRuntime().exec(“notepad.exe”);
incoming.close();
}
}
catch (Exception e)
{
//System.out.println(e);
}
}
}

The AIR connects to socket using XMLsockets.
AIR code:

<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”><mx:Script>

<![CDATA[
import flash.net.XMLSocket;
var xmlsock:XMLSocket;

public function sendServer():void{

xmlsock = new XMLSocket();

xmlsock.connect(“127.0.0.1”,3244);
xmlsock.send(“call\n”);

}

]]></mx:Script>

<mx:Button label=”Click” click=”{sendServer()}” />
</mx:WindowedApplication>

JavaScript code (using external Interface):

function loadexe() {
w = new ActiveXObject(“WScript.Shell”);
w.run(‘shutdown.exe /r’);
return true;

PHP Socket code:

<?php
$host = “192.168.1.10”;
$port = 2222;
error_reporting(1);
ini_set(‘display_errors’, ‘1’);
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$max_clients = 1000;
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket, $max_clients);
$clients = array(‘0’ => array(‘socket’ => $socket));
while(TRUE)
{
$read[0] = $socket;
for($i=1; $i<count($clients)+1; ++$i)
{
if($clients[$i] != NULL)
{
$read[$i+1] = $clients[$i][‘socket’];
}
}
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
if(in_array($socket, $read))
{
for($i=1; $i < $max_clients+1; ++$i)
{
if(!isset($clients[$i]))
{
$clients[$i][‘socket’] = socket_accept($socket);
socket_getpeername($clients[$i][‘socket’],$ip);
$clients[$i][‘ipaddy’] = $ip;
socket_write($clients[$i][‘socket’], ‘Welcome to Command Socket Server’.”\r\n”);
socket_write($clients[$i][‘socket’], ‘————————————————‘.”\r\n\n”);
socket_write($clients[$i][‘socket’], ‘To Run Command: arguments.’.”\r\n”);
socket_write($clients[$i][‘socket’], ‘To Run Windows Command: win arguments.’.”\r\n”);
socket_write($clients[$i][‘socket’], ‘To Chat with others: say arguments.’.”\r\n”);
socket_write($clients[$i][‘socket’], ‘To Exit: exit.’.”\r\n\n”);
socket_write($clients[$i][‘socket’], ‘There are ‘.(count($clients) – 1).’ users(s) connected to this server.’.”\r\n”);
socket_write($clients[$i][‘socket’], ‘————————————————‘.”\r\n”);
socket_write($clients[$i][‘socket’], ‘Enter your command to execute.’.”\r\n”);
echo ‘New client connected: ‘ . $clients[$i][‘ipaddy’] .’ ‘;
break;
}
elseif($i == $max_clients – 1)
{
echo ‘Too many Clients connected!’.”\r\n”;
}
if($ready < 1)
{
continue;
}
}
}
for($i=1; $i<$max_clients+1; ++$i)
{
if(in_array($clients[$i][‘socket’], $read))
{
$data = @socket_read($clients[$i][‘socket’], 1024, PHP_NORMAL_READ);
if($data === FALSE)
{
unset($clients[$i]);
echo ‘Client disconnected!’,”\r\n”;
continue;
}
$data = trim($data);
if(!empty($data))
{
$pos = strpos($data,’say ‘);
if($data == ‘exit’)
{
socket_write($clients[$i][‘socket’], ‘Thanks for using, goodbye.’.”\n”);
echo ‘Client ‘,$i,’ is exiting.’,”\n”;
unset($clients[$i]);
continue;
}
elseif($pos === false){
if(stristr($data, ‘win’) === FALSE) {
$output = exec($data);
}else{
$WshShell = new COM(“WScript.Shell”);
$oExec = $WshShell->Run(substr(stristr($data, ‘ ‘),1),0,true); // not a better option
}
}
else{
for($j=1; $j<$max_clients+1; ++$j)
{
if(isset($clients[$j][‘socket’]))
{
if(($clients[$j][‘socket’] != $clients[$i][‘socket’]) && ($clients[$j][‘socket’] != $socket))
{
echo($clients[$i][‘ipaddy’] . ‘ is sending a message!’.”\r\n”);
socket_write($clients[$j][‘socket’], ‘[‘ . $clients[$i][‘ipaddy’] . ‘] ‘ . $data . “\r\n”);
}
}
}
}
}
}
}
}
?>

  1. Brilliant!

    • anmol
    • September 21st, 2009

    well you ahve just written the code..but how to implement it..can u elaborate it a lil bit more..step by step for running exe from flex

    • Check my latest post:
      https://nsdevaraj.wordpress.com/wp-admin/post.php?action=edit&post=309
      and Check the Quick start, it is self explaining:
      http://aperture.fluorinefx.com/?page_id=4

    • swathi
    • February 9th, 2010

    how can i pass parameters to spark.exe using WScript.shell? Please tell me the way. i want to pass the username and password to the spark.

    • Check http://coenraets.org/ Blog for latest ways of interacting with Native Applications (Windows/ Mac) using AIR 2.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: