0

I'm new with actionscript and want to create a socket server and listen to it, here is my code:

package
{ 
  import flash.display.Sprite;
  import flash.net.ServerSocket; 
  import flash.events;
  import flash.events.ServerSocketConnectEvent;
  import flash.net.ServerSocket;
  import flash.net.Socket;

    public class one extends Sprite
    { 
        private static  var server:ServerSocket;
        private static  var client:Socket;

      public static function create() 
      {
        one.server = new ServerSocket();
        one.server.bind(8888);
        one.server.addEventListener(ServerSocketConnectEvent.CONNECT, one.serverConnectHandler);
        one.server.listen();
      }

      public static function serverConnectHandler(e:ServerSocketConnectEvent):void 
      {
        var socket:Socket = e.socket;
        one.client = socket;
      }

      public static function send(param1:String)
      {
        one.client.writeUTFBytes(param1);
      }
    }
}

When I compile it using mxmlc.exe -static-link-runtime-shared-libraries=true one.as but get the error:

C:\Users\Mark\Desktop\test program\one.as(23): col: 53 Error: Type was not found or was not a compile-time constant: ServerSocketConnectEvent.

Same exact problem was mentioned here but Can't I somehow point to those libraries during compilation? The only solution is to build application form adobe proffesional? I'm little lost.

user3503143
  • 381
  • 3
  • 10

1 Answers1

0

This is a simple error: Look at your imports.

import flash.net.ServerSocket; 
import flash.events; //this is the problem
import flash.events.ServerSocketConnectEvent;

You have use bad syntax when declaring the source paths of your imports. You should have received two errors, the second should say Error 1172: Definition flash:events could not be found.

import flash.events; //this is wrong syntax & does not load EVENTS Class (API)
import flash.events.*; //this is correct syntax to allow EVENTS Class

Start with import flash.+ ClassName. + ABC; ... where ABC; is a specific Name; or just use a generic *;

Solution:
So just replace import flash.events; with import flash.events.*; and it should work.

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Even if I only have `import flash.display.Sprite; import flash.net.ServerSocket; import flash.events.ServerSocketConnectEvent; import flash.net.ServerSocket; import flash.net.Socket;` I still get same error, As I understand problem is in importing `ServerSocketConnectEvent` – user3503143 Nov 22 '19 at 06:47
  • Make sure you are compiling as AIR application (not just outputting an SWF to use in the browser), right? Using `flash.net.Socket` is fine for SWF (Flash Player plugin) but for using `ServerSocket` it must be compiled as AIR (makes AS3 into native app for desktop or mobile). Incase you have only Flex SDK (I see the `mxmlc.exe`) then instead you should compile AIR apps with this: https://www.adobe.com/devnet/air/air-sdk-download.html – VC.One Nov 22 '19 at 07:19
  • PS: Thinking about it, there is no `ServerSocket` in Flex so maybe that's why the Flex compiler complains, It should have also error'ed on the first `Server...` code (2nd import) but somehow it was okay with your imports until the 4th line. Also why import twice that `import flash.net.ServerSocket;`? Does removing one of those duplicates help? – VC.One Nov 22 '19 at 07:25
  • No, I'll try what you suggested about AIR and share results – user3503143 Nov 22 '19 at 07:42
  • Stop press!! According to the docs... the Flex SDK includes the AIR SDK. Don't know if your Flex has the latest AIR though So still worth a download. **(1)** Try this [basic tutorial](https://help.adobe.com/en_US/air/build/WS144092a96ffef7cc4c0afd1212601c9a36f-8000.html) instructions with your current Flex SDK. To compile use `amxmlc.exe` the "a" is for AIR compiling via Flex, see also article: [AIR compiling via Flex](https://help.adobe.com/en_US/air/build/WS5b3ccc516d4fbf351e63e3d118666ade46-7fa1.html). – VC.One Nov 22 '19 at 09:29
  • **(2)** If you already got the AIR SDK (and not need any Flex components, you're shown code isn't using any of them) then try this [tutorial](https://help.adobe.com/en_US/air/build/WS5b3ccc516d4fbf351e63e3d118666ade46-7ecc.html) (alt version of [same tutorial](https://www.digimantra.com/air/creating-your-first-hello-world-application-in-adobe-air/)) to make sure everything compiles okay. Afterwards you can drop the web page display part and simply use your AS3 code instead. Use `ADL.exe` to preview/debug or use `ADT.exe` to compile an installable EXE of your code app. – VC.One Nov 22 '19 at 09:40