I am having trouble with talking to the server. I am not sure what going on with it. When I exit the flash it catch my text strings. I am using the binary socket.
Main.as
package
{
import flash.events.Event;
import flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.KeyboardEvent;
import flash.system.System;
public class Main extends Sprite
{
private var host:String;
private var port:int;
public var socket:Socket;
public var text_log:TextField = new TextField();
public var text_submit:TextField = new TextField();
//public var
public var chatPort:int = 5555;
public var policyPort:int = chatPort + 1;
public function Main()
{
host = "127.0.0.1";
port = 5555;
//flash.system.Security.loadPolicyFile("xmlsocket://" + host + ":" + policyPort)
flash.system.Security.allowDomain(host);
flash.system.Security.loadPolicyFile("*");
flash.system.Security.loadPolicyFile('flashpolicy.xml')
connect();
text_log.text = "";
text_log.border = true;
text_log.height = 256;
text_log.width = 256;
text_submit.height = 24;
text_submit.border = true;
text_submit.y = 256;
text_submit.width = 256;
text_submit.type = TextFieldType.INPUT;
addChild(text_log);
addChild(text_submit);
text_submit.addEventListener(KeyboardEvent.KEY_UP, onInputLineKey);
function onInputLineKey(e:KeyboardEvent):void {
if (e.keyCode == 13) { // ENTER was pressed
socket.writeUTFBytes(text_submit.text);
socket.flush();
trace("send..");
}
}
}
private function receiveMessage( message:String ):void
{
text_log.appendText("
"+message );
trace( message );
}
private function connect():void
{
socket = new Socket();
socket.addEventListener(Event.CONNECT, socketConnect);
socket.addEventListener(Event.CLOSE, socketClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, socketError );
try
{
socket.connect(host, port);
}
catch (e:Error)
{
trace("Error");
}
}
private function socketConnect( event:Event ):void
{
// handle connection connected
socket.addEventListener( ProgressEvent.SOCKET_DATA, socketData );
//sendMessage( "Hello Server from cleint" );
}
private function socketData( event:ProgressEvent ):void
{
receiveMessage( socket.readUTFBytes( socket.bytesAvailable ) );
trace("incoming data...");
}
private function socketClose( event:Event ):void
{
// handle connection closed
trace( "Socket Closed" );
text_log.appendText("
"+"server disconnected..." );
}
private function socketError( event:IOErrorEvent ):void
{
// handle connection error
trace( "Socket has run into an Error" );
}
}
}
SimpleServer.java
package server;
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 = 5555;
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.\r");
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"))
{
// done = true;
}
}
//incoming.close();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
private void out(String str)
{
printOut.println(str);
System.out.println(str);
}
}
Could any one help with this?