A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Need some help with simple socket server

  1. #1
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268

    [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.*;

    public class Server {

      public static void main(String[] args) throws IOException {
        System.out.println("Welcome to Server side");
        BufferedReader in = null;
        PrintWriter    out= null;

        ServerSocket servers = null;
        Socket       fromclient = 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;

        System.out.println("Wait for messages");
        while ((input = in.readLine()) != null) {
         if (input.equalsIgnoreCase("exit")) break;
         out.println("S ::: "+input);
         System.out.println(input);
        }
        out.close();
        in.close();
        fromclient.close();
        servers.close();
      }
    }

    The source for Client can be found in attachments.

    What am I doing wrong?
    Attached Files Attached Files
    Last edited by caseyryan; 08-04-2010 at 12:55 AM.

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    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.

    I found this server-side code with google:
    Actionscript Code:
    package com.konstantin;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class Server
    {

    public static void main(String[] args)
    {
        ServerSocket serverSocket = null;

        /* 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);
        }

        System.out.println ("Socket connection incoming!");

        // Keep going while we can...
        byte b[] = new byte[100];
        int offset = 0;
        String s;
        try
        {
            boolean done = false;
            boolean auth = false;
            String protocol_target = "<policy-file-request/>";
            byte[] p_bytes = protocol_target.getBytes ();
            int result;
            while (!done)
            {
                if (in.read (b, offset, 1) == -1)
                    done = true;
                else
                {
                    if (!auth)
                    {
                        ++offset;
                        b[offset] = 0;
                        if (offset != p_bytes.length)
                        {
                            System.out.println ("Waiting for protocol data... ("+offset+"/"+p_bytes.length+")");
                        }
                        else
                        {
                            // Compare byte data
                            for (int i = 0; i < p_bytes.length; ++i)
                            {
                                System.out.print (b[i]+" ");
                            }
                            System.out.print ("\n");
                            System.out.flush ();
                            for (int i = 0; i < p_bytes.length; ++i)
                            {
                                System.out.print (p_bytes[i]+" ");
                            }
                            System.out.print ("\n");
                            System.out.flush ();
                            boolean match = true;
                            for (int i = 0; i < p_bytes.length; ++i)
                            {
                                if (b[i] != p_bytes[i])
                                {
                                    match = false;
                                    System.out.println ("Mismatch on " + i + ".");
                                }
                            }
                            if (match)
                                auth = true;
                            else
                            {
                                System.out.println ("Bad protocol input.");
                                System.exit (-1);
                            }
                        }

                        // Auth
                        if (auth)
                        {
                            System.out.println ("Authing...");
                            s = "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain='*' to-ports='4444' /></cross-domain-policy>";
                            b = s.getBytes();
                            out.write (b, 0, b.length);
                            b[0] = 0;
                            out.write (b, 0, 1); // End
                            out.flush ();
                            offset = 0;
                            b = new byte[100];
                            b[0] = 0;
                            auth = true;
                            System.out.println ("Auth completed.");
                        }
                    }
                }
            }
        }
        catch (IOException e)
        {
            System.out.println("Stream failure: " + e.getMessage());
            System.exit(-1);
        }

        // Finished.
        try
        {
            in.close ();
            out.close ();
            clientSocket.close ();
        }
        catch (Exception e)
        {
            System.out.println("Failed closing auth stream: " + e.getMessage());
            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.");

        // Echo
        try
        {
            boolean done = false;
            while (!done)
            {
                if (in.read (b, offset, 1) == -1)
                    done = true;
                else
                {
                    b[1] = 0;
                    s = new String (b);
                    System.out.print (s);
                    System.out.flush ();
                }
            }
        }
        catch (IOException e)
        {
            System.out.println("Failed echo stream: " + e.getMessage());
            System.exit(-1);
        }

        // Finished.
        try
        {
            in.close ();
            out.close ();
            clientSocket.close ();
            serverSocket.close ();
        }
        catch (Exception e)
        {
            System.out.println("Failed closing stream: " + e.getMessage());
            System.exit(-1);
        }
    }

    }

    Now everything works correctly.
    Last edited by caseyryan; 08-04-2010 at 12:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center