Java and Flash Communications

Source: Internet
Author: User
Tags rar sleep sleep function socket thread tostring client

Socket (socket technology) is a simple and easy to learn technology, especially in the current, such as Java-oriented, commercial development of the language support, the realization is very convenient.

Flash we all know that in the network image, the transmission of animation has a strong advantage of the thick, Flash player and its plug-ins high penetration rate. Plus a very powerful OO script AS2, as well as its lost graphics system, SWF cross-platform and other advantages. In the short term, I am afraid that opponents can contend with it, and there are more and more good trends in development.

However, there is a limit to the functionality of Flash. For example, basic applications such as random read and write files will not be able to implement the functionality. In response to large-scale data input, the "biased" database access system with flash is difficult to guarantee both development efficiency and operational efficiency.

And now the mainstream of commercialization, networked OO technology, such as Java, in the processing of background data to be a good thing. So, if you can use a program written in Java and other main languages to process data, through the transfer between the data, so that flash in front of a platform to do a display level of functionality, you will be able to open an unprecedented, whether in the outside or in the first class directly to the network-oriented software.

Some of the things I've tried to do today have been done, but I'm here to share with you the experiences I've tried:

1. The first thing to know is that you can do the problem, Java does not have to say. In Flash, there is a xmlsocket class that can be used to communicate with the application software of a built server. The form of communication has two sides, one is using XML node information, for example:
<xmlnode attribute1= "HelloWorld"/>
In today's widely used XML technology, this format is more alive, and XML can be seen frequently in the application of Java EE level.
There is also a relatively simple, direct transmission of strings. It will be more practical in small scale programs.
Of course, in the final analysis is the word stream.
In the above two ways, please do not forget, in the Flash and Java end, you have to add a byte throttle Terminator:
Flash to the Java program to do server-side information, not the tail plus "\ n".
Java program to do the server side of the information to Flash, the end of the "".
2 Pass the data, the next is resolved, at both ends can directly apply their own string APIs to facilitate the processing. There is no more detail here.
Here is a brief introduction I do a small demo, through Java to send data to flash let flash a ball moving from left to right:

Import java.io.*;
Import java.net.*;
public class Mainserver
{
public static Boolean Sflag;
public static void Main (String args[])
{
Try
{
ServerSocket Server=null;
Try
{
Server=new ServerSocket (4700);
}
catch (Exception e)
{
System.out.println ("Can not be listen to:" +e);
}
Socket Socket=null;
Try
{
System.out.println ("Java Server Start:");
Socket=server.accept ();
SYSTEM.OUT.PRINTLN ("Client has connected\n");
}
catch (Exception e)
{
System.out.println ("Error:" +e);
}
BufferedReader is=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));//*correct get the input from the Client
PrintWriter os=new PrintWriter (Socket.getoutputstream ()); /*correct make OS the output to client*/
Sflag=true;
ControlThread testcontroller=new controlthread (OS);
Testcontroller.start ();//start control thread.
while (Sflag)
{ ;
}
SYSTEM.OUT.PRINTLN ("server ends successfully!\n");
Is.close ();
Socket.close ();
Server.close ();
}
catch (Exception e)
{
System.out.println ("Error:" +e);
}
}
}

The main program is quite simple, set up the socket server, and start a management thread, well, here's a look at this management thread:

Import java.io.*;
public class ControlThread extends Thread
{
Private point Controlnode;
Private PrintWriter OutputStream;
Public ControlThread (PrintWriter inoutputstream)
{
outputstream=inoutputstream;//the object that gets the data to write back, this is the Flash client.
}
public void Run ()
{
Controlnode=new Point (180,150);
for (int i=0;i<100;i++)
{
Try
{
Sleep (400);
}
catch (Java.lang.InterruptedException e)
{
System.out.println (e);
}
controlnode.mx=180+i*2;
Outputstream.println (controlnode.tostring () + "the");
Outputstream.flush ();
System.out.println (Controlnode.tostring ());
}
Mainserver.sflag=false;
}
}

Pass to change a point node information, to the object in Flash, using the sleep function to achieve a certain delay effect, to control the Flash Object function.
The node class is simple enough:

Class Point
{
public int mx;
public int i;
Public point (int inx,int iny)
{
Mx=inx;
My=iny;
}
Public String toString ()
{
return new String (mx+ "," +my);
}
}

Next go to Flash:
Mainly through the following work:
1) Establish the connection
2 to receive the message back, and to the scene response to the animation, change the object's coordinate values.
Quite an easy job, isn't it?
1. Establish the connection:

function Mainf (): Void
{
if (_root. DEBUG)
CREATEDEBUGMC ();
var serveraddress:string= "127.0.0.1";
var port:number=4700;
_root.gsocket=new Xmlsocket ();
_root.gsocket.onconnect=myonconnect;
_root.gsocket.ondata=myondata;
if (!_root.gsocket.connect (Serveraddress,port))
{
Trace3 ("Connection failure!");
}
}

2 receive the message back and the scene responds to the animation, changing the object's coordinate value. Implemented in a OnData function:

function Myondata (str:string): Void
{
Trace3 (str);
var arr:array=str.split (",");
var tmpx:number=number (arr[0]);
var tmpy:number=number (arr[1]);
_ROOT.BALL._X=TMPX;
_root.ball._y=tmpy;
}

All right, here's the source code:
Java_flash1.rar
Java_flash2.rar
If the above link can not respond to the download (may be blocked by the site), you can use the download tool (such as thunder, etc.) to download.
Welcome to criticize and correct comments!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.