When you invoke the
connection to the server and keeps that connection open until one of the following occurs:
The
■
XMLSocket.close()
No more references to the XMLSocket object exist.
■
Flash Player exits.
■
The connection is broken (for example, the modem disconnects).
■
Creating and connecting to a Java XML socket server
The following code demonstrates a simple XMLSocket server written in Java that accepts
incoming connections and displays the received messages in the command prompt window.
By default, a new server is created on port 8080 of your local machine, although you can
specify a different port number when starting your server from the command line.
Create a new text document and add the following code:
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
388
Networking and Communication
XMLSocket.connect()
method of the XMLSocket class is called.
method, Flash Player opens a TCP/IP