0 Replies Latest reply on Aug 11, 2010 12:54 AM by msc_santu

    Unable to connect to a socket in its own domain

    msc_santu Level 1

      Hi,

       

      I wrote a very simple application that uses socket.  I created Java Server to accept client connections on port 8843 as specified below.  I created flex application as Client application and i want to establish connection with the server on the port 8843.  I can able to establish connection if i run the flex application directly(running .swf file).  If i place the same flex application in Tomcat Server 6.0, i can not able to establish connection.  I enabled log for the debug version of flash player using mm.cfg.  I pasted the log file content also.

       

       

      Can you please help me how to establish connection between client and server.

       

      Thanks in advance.

       

      Regards,

      Santhosh

       

       

      c:\documents and settings\flexuser\Application Data\Macromedia\Flash Player\Logs\Policyfiles.txt
      __________________________________________________________________________
      OK: Root-level SWF loaded: http://localhost:8080/SampleFlexSocket/richclient/SampleFlexSocket.swf
      OK: Searching for <allow-access-from> in policy files to authorize data loading from resource at xmlsocket://localhost:8843 by requestor from http://localhost:8080/SampleFlexSocket/richclient/SampleFlexSocket.swf
      Warning: Domain localhost does not specify a meta-policy.  Applying default meta-policy 'master-only'.  This configuration is deprecated.  See http://www.adobe.com/go/strict_policy_files to fix this problem.
      OK: Policy file accepted: http://localhost:8080/crossdomain.xml
      Warning: Timeout on xmlsocket://localhost:8843 (at 3 seconds) while waiting for socket policy file.  This should not cause any problems, but see http://www.adobe.com/go/strict_policy_files for an explanation.
      Error: SWF from http://localhost:8080/SampleFlexSocket/richclient/SampleFlexSocket.swf may not connect to a socket in its own domain without a policy file.  See http://www.adobe.com/go/strict_policy_files to fix this problem.
      __________________________________________________________________________________________ ____________________________________________________________________________________
      c:\documents and settings\flexuser\Application Data\Macromedia\Flash Player\Logs\flashlog.txt
      __________________________________________________________________________
      Warning: Domain localhost does not specify a meta-policy.  Applying default meta-policy 'master-only'.  This configuration is deprecated.  See http://www.adobe.com/go/strict_policy_files to fix this problem.
      Warning: Timeout on xmlsocket://localhost:8843 (at 3 seconds) while waiting for socket policy file.  This should not cause any problems, but see http://www.adobe.com/go/strict_policy_files for an explanation.
      Error: SWF from http://localhost:8080/SampleFlexSocket/richclient/SampleFlexSocket.swf may not connect to a socket in its own domain without a policy file.  See http://www.adobe.com/go/strict_policy_files to fix this problem.
      *** Security Sandbox Violation ***
      Connection to localhost:8843 halted - not permitted from http://localhost:8080/SampleFlexSocket/richclient/SampleFlexSocket.swf
      __________________________________________________________________________________________ ____________________________________________________________________________________
      crossdomain.xml [Placed this file in Tomcat 6.0 Root Folder]
      _____________
      <?xml version="1.0"?>
      <!DOCTYPE cross-domain-policy
        SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
      <cross-domain-policy>
        <allow-access-from domain="localhost" />
        <allow-access-from domain="*" />
      </cross-domain-policy>

      __________________________________________________________________________________________ _____________________________________________________________________________________
      SimpleServer.java [Server Program - Java]
      ______________
      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 = 8843;

              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)
              {
         e.printStackTrace();
                  System.out.println(e);
              }
          }

          private void out(String str)
          {
              printOut.println(str);
              System.out.println(str);
          }
      }

      __________________________________________________________________________________________ _____________________________________________________

      SampleFlexSocket.mxml [Client Program] -- Deployed in Tomcat 6.0 Server
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle">
      <mx:Script>
        <![CDATA[
         import mx.controls.Alert;
         public var a:Socket;
        
         private function handleClose():void
         {
          if (a != null)
          {
           a.close();
          }
         }
         private function handleClick():void
         {
          Alert.show("Clicked");
          var a:Socket = new Socket();
          Security.allowDomain("*");
          Security.loadPolicyFile("http://localhost:8080/crossdomain.xml");
          a.addEventListener(DataEvent.DATA,onData);
          a.addEventListener(Event.ACTIVATE,handleActivate);
          a.addEventListener(IOErrorEvent.IO_ERROR,handleIOError);
          a.addEventListener(SecurityErrorEvent.SECURITY_ERROR,handleSecError);
          a.connect("localhost",8843);
          a.writeObject("Santhosh\n");
         }
         public function handleIOError(event:IOErrorEvent):void
         {
          Alert.show("IO Error occured: \n" + event.text);
         }
         public function onData(event:DataEvent):void
         {
          Alert.show("Event type is:" + event.type + "\nData is:" + event.data);
         }
         public function handleActivate(event:Event):void
         {
          Alert.show("Connection activated");
         }
         public function handleSecError(event:Event):void
         {
          Alert.show("Security Error occured");
         }
        ]]>
      </mx:Script>
      <mx:Button label="Open Connection" click="handleClick()"/>
      <mx:Button label="Close Connection" click="handleClose()"/>
      </mx:Application>
      __________________________________________________________________________________________ ______________________