Flex3. Socket connections Part 2

    Continued Socket Connections Part 1

    • XMLSocket.connect () can only connect to computers located in the same domain as the SWF file. This restriction does not apply to SWF files running from a local drive. (This restriction is identical to the security rules for URLLoader.load ().) To connect to a server daemon running in a different domain than the one where the SWF is located on the server, you can create a security policy file in which you can register permission to gain access from another domain .

    Note: Configuring a server to work with an XMLSocket object can be very difficult. Therefore, if your application does not require real-time interaction, use the URLLoader class instead of the XMLSocket class.

    You can use the XMLSocket.connect () and XMLSocket.send () methods of the XMLSocket class to transfer XML data to the server through a socket connection. The XMLSocket.connect () method establishes a connection to the web server port. The XMLSocket.send () method sends an XML object to the server using the established socket connection.

    When the XMLSocket.connect () method is called, Flash Player opens a TCP / IP connection to the server and keeps it open until one of the following events occurs:
    • The XMLSocket.close () method of the XMLSocket class is called.
    • There is no longer a reference to an existing XMLSocket object.
    • Flash Player Shutdown
    • A disconnected connection (for example, disconnecting a modem).

    Creating a Java XML Socket Server and Connecting to It


    The following code demonstrates a simple XML Socket Server written in Java that accepts incoming connections and displays received messages in a command window. By default, a new server is created on port 8080 of your computer, although you can specify a different port number when starting the server from the command line.

    Create a new text document and add the following code into it: Save the document to disk as SimpleServer.java and compile using a Java compiler that creates a Java file called SimpleServer.class You can start the XML socket server by opening a command prompt and typing java SimpleServer

    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 = 8080;
            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);
        }
    }
    


    . The SimpleServer.class file can be located anywhere on your computer or network; it is not necessary to place it in the root directory of your web server.

    If you are unable to start the server because the path to the Java class was not found, try java -classpath like this. SimpleServer (My knowledge in Java is not so deep, so if I didn’t correctly formulate the translation of this paragraph, please correct me)

    To create an XMLSocket connection from your ActionScript application, you need to create a new instance of the XMLSocket class and call the XMLSocket.connect () method with the host name and port number, namely:

    var xmlsock:XMLSocket = new XMLSocket();
    xmlsock.connect("127.0.0.1", 8080);


    The securityError event (flash.events.SecurityErrorEvent) can occur if an XMLSocket.connect () call is made and an attempt is made to connect to a server located outside the security sandbox, or if the specified port number is less than 1024.

    Each time a data is received from the server, the DATA event fires ( flash.events.DataEvent.DATA): You can

    xmlsock.addEventListener(DataEvent.DATA, onData);
    private function onData(event:DataEvent):void{
        trace("[" + event.type + "] " + event.data);
    }
    


    send XMLSocket data to the server using the XMLSocket.send () method with passing an object or string as an XML parameter. Flash Player converts the specified parameter into an object of type String, and transfers the content, XMLSocket to the server, adding zero (0) bytes at the end.

    xmlsock.send(xmlFormattedData);

    The XMLSocket.send () method does not return any value confirming that the data was transferred successfully. If an error occurs during the transfer, an exception of type IOError is thrown.

    Each XML message sent by the socket server should end with a newline character (\ n)

    Thank you in advance for your constructive comments, and sincerely hope that this translation will be useful to someone and will help to solve the tasks.

    Also popular now: