Flash to Max via Java

leovanderveen's icon

Hi list,

I wondered if someone ever linked Java (mxj) to Flash, I tried real
hard to establish some kind of communication but my Java knowledge is
lacking a bit. I already know about Olaf Matthes' flashserver object
but I really want to be able to connect from within mxj.

I'll post the Java code and Flash ActionScript code right in this
mail as well as a link to a zip file containing the Max patch, Java
files and the *.fla file ( http://student-kmt.hku.nl/~leo/-/flash-
java/ ).

The problem :

- connection to Flash is succesfull (using ServerSocket.accept(); )

- sending data to Flash doesn't work yet (using : output = new
PrintStream(client.getOutputStream()); ) but throws no exception

- receiving data from Flash doesnt work either. Tried using
input.readLine() but that one seems to be depricated and I can't find
its alternative. But : calling the recive function in java and
sending something from Flash causes nothing to happen untill I close
the swf, now my message appears in Java ??? why is that.

Thanks a lot,

leo@nr37.nl
www.nr37.nl

Java code :

// code by leo@nr37.nl

import com.cycling74.max.*;
import java.io.*;
import java.net.*;

public class flash extends MaxObject
{
    ServerSocket server;
    Socket client;

    PrintStream output;
    DataInputStream input;
    String inputtext;

    public flash(Atom[] args)
    {
        declareInlets(new int[]{DataTypes.ALL});
        declareOutlets(new int[]{DataTypes.ALL});    
    }

    public void create(int port) {
        try    {
            server = new ServerSocket(port);
            try {
                    client = server.accept();
                    post("mxj flash : Connected");
         } catch(IOException e){
             post(e.getMessage());
            }    
        } catch(IOException e){
             post(e.getMessage());
        }
}

    public void send(String message) {
        try {
            output = new PrintStream(client.getOutputStream());
            output.println(message);    
        } catch (IOException e) {
            post(e.getMessage());
        }
    }

    public void receive() {        
        try {
            input = new DataInputStream(client.getInputStream());
            inputtext = input.readLine();
        } catch (IOException e) {
            post(e.getMessage());
        }
        if(inputtext != null) {
            post("input = "+inputtext);
        }

    }

    public void close() {
        try{
            client.close();
            server.close();
            post("mxj flash : Connection lost...");
        }catch(Exception e) {
            post(e.getMessage());
        }
    }        
}

Flash code :

// code by leo@nr37.nl

maxServer = new XMLSocket();
maxServer.connect("127.0.0.1",7474);

maxServer.onConnect = connectHandler;
maxServer.onClose = closeHandler;
maxServer.onData = dataHandler;

function connectHandler(success) {
    if (success) {
        trace("connected");
    } else {
        trace("connection failed");
    }
}

function closeHandler() {
    trace("connection lost...");
}

function dataHandler(maxdata) {
    trace(maxdata);
}

function sendData(flashdata) {
    maxserver.send(flashdata);
}

srooney's icon

you should check out Ben Chun's Flosc utility--