[RESOLVED] Need some help with simple socket server
I've created a simple Java server app and an AS3 client. Basically AS3 client only connects to the server on localhost:4444 using XMLSocket.
Java server runs great. It listens to the port 4444 and even reacts to my as3 client connection.
AS3 client is supposed to create a new TextField and "Got it" text to it on connection event. That's where the problem appears.
I can see it tries to get a policy file. But I have this file placed into the same folder with my Java server:
Here's its source:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy
SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
Here's the source of the testApp.jar
Actionscript Code:
package com.konstantin;
import java.io.*; import java.net.*;
publicclass Server {
publicstaticvoid main(String[] args) throws IOException { System.out.println("Welcome to Server side"); BufferedReader in = null; PrintWriter out= null;
// create server socket try{ servers = new ServerSocket(4444); }catch(IOException e){ System.out.println("Couldn't listen to port 4444"); System.exit(-1); }
try{ System.out.print("Waiting for a client..."); fromclient = servers.accept(); System.out.println("Client connected"); }catch(IOException e){ System.out.println("Can't accept"); System.exit(-1); }
in = new BufferedReader(new InputStreamReader(fromclient.getInputStream())); out = new PrintWriter(fromclient.getOutputStream(),true); String input;
The problem's resolved.
Java server need to generate the policy file and send it to AS3 client or else the connection's closed immediately after flash player starts looking for a policy file.
/* Open a socket to listen */ try { serverSocket = new ServerSocket(4444); } catch(IOException e) { System.out.println("Could not listen on port: 4444"); System.exit(-1); }
// Try catch a socket to listen on Socket clientSocket = null; try { System.out.println("Waiting for auth on 4444..."); clientSocket = serverSocket.accept(); } catch(IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); }
// Now a stream has been opened... InputStream in = null; OutputStream out = null; try { in = clientSocket.getInputStream(); out = clientSocket.getOutputStream(); } catch(IOException e) { System.out.println("Failed to get streams."); System.exit(-1); }
// Try catch a socket to listen on for data try { System.out.println("Waiting on 4444 fo data..."); clientSocket = serverSocket.accept(); } catch(IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); }
// Now a stream has been opened... in = null; out = null; try { in = clientSocket.getInputStream(); out = clientSocket.getOutputStream(); } catch(IOException e) { System.out.println("Failed to get streams."); System.exit(-1); }
System.out.println("Socket data connection waiting.");