📜 ⬆️ ⬇️

Flex3. Socket connections Part 2

Continuation of the translation Socket Connections Part 1


Note: Setting up 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 a server through a socket connection. The XMLSocket.connect () method establishes a connection to a 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:

Creating 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 to it:

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

Save the document to SimpleServer.java disk and compile it using a Java compiler that will create a Java file with the name SimpleServer.class. You can run an XML socket server by opening a command line and typing java SimpleServer . The SimpleServer.class file can be located anywhere on your computer or online; it is not necessary to locate it in the root directory of your web server.

If you are unable to start the server due to the fact that the path to the Java class is not found, try this java -classpath. SimpleServer. (My knowledge of Java is not so deep, so if I incorrectly formulated 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, specifying the host name and port number, as follows:

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


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

Whenever data is received from the server, a DATA event (flash.events.DataEvent.DATA) is triggered:

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


You can send data to XMLSocket by using the XMLSocket.send () method with passing an XML object or string as a parameter. Flash Player converts the specified parameter to an object of type String, and sends the content to the XMLSocket server, adding zero (0) bytes to 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 transmission, an IOError exception is thrown.

Each XML message sent by the socket server must end with a new line beginning character (\ n)

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

Source: https://habr.com/ru/post/52928/


All Articles