flex application resets connection when using XmlSocket
I am flex newbie and I am trying to get the example given here :http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_5.html
I am using the same java server given there. and I am creating the XmlSocket in a flex air application. When I run my air application I get a java.net.SocketException connection reset at the java server.
Both are stand alone applications on my desktop.
Flex Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function init():void
{
var xmlsock:XMLSocket = new XMLSocket();
//xmlsock.addEventListener(DataEvent.DATA, onData);
xmlsock.addEventListener(Event.CLOSE, onData);
xmlsock.addEventListener(Event.CONNECT, onData);
xmlsock.addEventListener(DataEvent.DATA, onData);
xmlsock.addEventListener(IOErrorEvent.IO_ERROR, onData);
xmlsock.addEventListener(ProgressEvent.PROGRESS, onData);
xmlsock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onData);
xmlsock.connect("localhost", 9020);
//xmlsock.send("<hello></hello>");
}
private function onData(event:Event):void
{
myText.text=""+event.toString();
}
]]>
</mx:Script>
<mx:Panel>
<mx:TextArea id="myText">
</mx:TextArea>
</mx:Panel>
</mx:WindowedApplication>
Java 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 = 9020;
try
{
port = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e)
{
// Catch exception and keep going.
}
server = new SimpleServer(port);
}
private SimpleServer(int port)
{
System.out.println(">> Starting SimpleServer");
try
{
socket = new ServerSocket(port);
incoming = socket.accept();
readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
printOut = new PrintStream(incoming.getOutputStream());
printOut.println("Enter EXIT to exit.\n\0");
out("Enter EXIT to exit.\r");
boolean done = false;
while (!done)
{
String str = readerIn.readLine();
if (str == null)
{
done = true;
}
else
{
out("Echo: " + str + "\r");
if(str.trim().equals("EXIT开发者_StackOverflow"))
{
done = true;
}
}
incoming.close();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
private void out(String str)
{
printOut.println(str);
System.out.println(str);
}
}
Thanks a lot in advance..
If I am interpreting your statement correctly that the Exception is occuring in your Java Server, not your AIR application I am guessing it is flash issuing a poilcy file request. I have applications that use XMLSockets (it runs as plain flash, not AIR, but as far as I know they are very similar.) I noticed that when a connection is established it will always send a policy file request (the string "<policy-file-request/>" followed by a null byte), and as soon as it receives what it considers a reply from the server it disconnects the socket. If this is the case, you should see the request echo on the console for your java server. To combat this you will need to either run a separate policy server and set the policy server before you connect, or change the server to handle the policy file request. I have a post on another forum that has example server / client code (the file XMLSocket.zip) it contains a sample policy and java server, the flash client is in open laszlo (which can be compiled to flash).
精彩评论