Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (2025)

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Showonly | Search instead for

Did you mean:

  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ

    Dedicated community for Japanese speakers

  • 한국 커뮤니티

    Dedicated community for Korean speakers

Exit

  • Home
  • Animate
  • Discussions
  • Re: Re: Establishing a socket connection between a...
    • Home
    • Animate
    • Discussions
  • Re: Re: Establishing a socket connection between a...

Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (4)

New Here ,

/t5/animate-discussions/establishing-a-socket-connection-between-a-swf-file-and-a-socket-test-program-tcp-ip-builder-windows/td-p/6438479 Aug 27, 2014 Aug 27, 2014

Copy link to clipboard

Copied

I have an issue with a college project I'm working on.

Using Actionscript 3, I made a simple .swf program, an animated, interactive smiley, that 'reacts' to number inputs in a input-box.

For the sake of the project, I now need to make the framework for establishing a socket connection with the smiley .swf, and another program.

This is where I encounter issues. I have very little knowledge of AS3 programming, so I'm not certain how to establish the connection - what's required code-wise for it, that is.

To test the connection, I'm attempting to use the "TCP/IP builder" program from windows, which lets me set up a server socket. I need to program the .swf file into a client - to recognize it, connect to it, then be able to receive data (so that the data can then be used to have the smiley 'react' to it - like how it does now with the input-box, only 'automatically' as it gets the data rather than by manual input).

My attempts at coding it are as follows, using a tutorial (linked HERE😞

//SOCKET STUFF GOES HERE

//****************************************************************

var socket:XMLSocket;

stage.addEventListener(MouseEvent.CLICK, doConnect);

// This one connects to local, port 9001, and applies event listeners

function doConnect(evt:MouseEvent):void

{

stage.removeEventListener(MouseEvent.CLICK, doConnect);

socket = new XMLSocket("127.0.0.1", 9001);

socket.addEventListener(Event.CONNECT, onConnect);

socket.addEventListener(IOErrorEvent.IO_ERROR, onError);

}

// This traces the connection (lets us see it happened, or failed)

function onConnect(evt:Event):void

{

trace("Connected");

socket.removeEventListener(Event.CONNECT, onConnect);

socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);

socket.addEventListener(DataEvent.DATA, onDataReceived);

socket.addEventListener(Event.CLOSE, onSocketClose);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

}

function onError(evt:IOErrorEvent):void

{

trace("Connect failed");

socket.removeEventListener(Event.CONNECT, onConnect);

socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);

stage.addEventListener(MouseEvent.CLICK, doConnect);

}

// Here, the flash tracks what keyboard button is pressed.

// If 'q' is pressed, the connection ends.

function keyUp(evt:KeyboardEvent):void

{

if (evt.keyCode == 81) // the key code for q is 81

{

socket.send("exit");

}

else

{

socket.send(evt.keyCode);

}

}

// This one should handle the data we get from the server.

function onDataReceived(evt:DataEvent):void

{

try {

trace("From Server:", evt.data );

}

catch (e:Error) {

trace('error');

}

}

function onSocketClose(evt:Event):void

{

trace("Connection Closed");

stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);

socket.removeEventListener(Event.CLOSE, onSocketClose);

socket.removeEventListener(DataEvent.DATA, onDataReceived);

Trying to connect to the socket gives me either no result (other than a 'connection failed' message when I click the .swf), or the following error:

Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: file:///C|/Users/Marko/Desktop/Završni/Flash%20documents/Smiley%5FTCP%5FIP%5Fv4.swf cannot load data from 127.0.0.1:9001.
at Smiley_TCP_IP_v4_fla::MainTimeline/doConnect()[Smiley_TCP_IP_v4_fla.MainTimeline::frame1:12]

TOPICS

ActionScript

Views

11.0K

Translate

Translate

Report

Report

  • Follow
  • Report

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (6) 1 Correct answer

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (7)

    LEGEND , Aug 29, 2014 Aug 29, 2014

    sinious LEGEND , Aug 29, 2014 Aug 29, 2014

    127.0.0.1 might be taken over via your HTTP software, I'm not familiar with the tool you're using. It also might have been repointed in your hosts file (in Windows you'll find that here: C:\Windows\System32\drivers\etc). Check to see if anything is overriding 127.0.0.1 (local loopback).

    Outside that, I gave you source to a quick AIR server that just opens a listener at 127.0.0.1:8910 so you'd need no other tools to test the client, even though you have one. I just include the source and a produce

    ...

    Votes

    Upvote

    Translate

    Translate

    Jump to answer

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (9) 53 Replies 53

    Jump to latest reply

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (12)

    Zarhon AUTHOR

    New Here ,

    /t5/animate-discussions/establishing-a-socket-connection-between-a-swf-file-and-a-socket-test-program-tcp-ip-builder-windows/m-p/6438529#M149160 Oct 09, 2014 Oct 09, 2014

    Copy link to clipboard

    Copied

    In Response To sinious

    Well, error-wise, I put the whole code under 'try', and via CTRL+SHIFT+ENTER debugging I got it to freeze only when I inputted a wrong IP, giving the previous 'invalid socket address' error. In the browser, it just freezes. I think the issue is that it freezes because of invalid socket addresses, and the browser version doesn't give a dismiss/continue popup error screen to keep going. So now I need a method to get it to stop crashing from an invalid address, I guess?

    Tried adding the textbox to get the error message to show, but it didn't, freezing as usual, despite try/catch. Is this because of a conflict with another try{} command that's already present within the code (can you 'nest' the try/catch commands within other try blocks)?

    I also spoke to my teacher, and now I have an task related to animation rather than the connection - apparently, the animation now needs to be more 'fluid', that is:

    - The eyes need to not 'snap' into position instantly, so it looks natural.

    - The head needs to rotate in a manner similar to the eyes, slower rather than instant.

    - The mouth's expressions need to animate/transition, rather than instantly switch between themselves.

    The eyes use simple x / y position changes, like this...

    else if (jedinica == 7) {

    wholebody.botheyes.eyesocket.eyeball.pupil.x = 0

    wholebody.botheyes.eyesocket.eyeball.pupil.y = 5

    wholebody.botheyes.eyesocket2.eyeball2.pupil.x = 0

    wholebody.botheyes.eyesocket2.eyeball2.pupil.y = 5

    }

    else if (jedinica == 8) {

    wholebody.botheyes.eyesocket.eyeball.pupil.x = 4

    wholebody.botheyes.eyesocket.eyeball.pupil.y = 4

    wholebody.botheyes.eyesocket2.eyeball2.pupil.x = 4

    wholebody.botheyes.eyesocket2.eyeball2.pupil.y = 4

    }

    etc...

    The head rotates with the rotate commands...

    else if (desetka == 1) {

    wholebody.rotation = -20

    }

    else if (desetka == 2) {

    wholebody.rotation = -15

    }

    etc...

    And the mouth uses gotoAndStop to switch between named frames with the different mouth emotions.

    wholebody.mouth.gotoAndStop("Gasp");

    Given what I have so far...
    1) What can I do to make the 'snapping' between eye positions/head rotations more fluid (in other words, slowing them down so they look more natural)? Is there a specific command that slows it down for those two command types, or do I have to apply math functions creatively?
    2) What's the best/simplest method to give the mouth 'transitioning' animations, or to give the named frames appear/disappear animations without them conflicting with each other, or running at the wrong moments? Extending the named mouth frames and letting it morph from an empty/default state?

    Votes

    Upvote

    Translate

    Translate

    Report

    Report

    • Follow
    • Report

    Community guidelines

    Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (13)

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (14)

    LEGEND ,

    /t5/animate-discussions/establishing-a-socket-connection-between-a-swf-file-and-a-socket-test-program-tcp-ip-builder-windows/m-p/6438530#M149161 Oct 09, 2014 Oct 09, 2014

    Copy link to clipboard

    Copied

    In Response To Zarhon

    You are free to nest try/catch as much as you like, as long as you nest properly. For example this is valid:

    try

    {

    // some code

    try

    {

    // more code

    }

    catch (e:Error)

    {

    // handle "more codes" errors

    }

    }

    catch (e:Error)

    {

    // handle "some code" errors

    }

    But don't forget that the catch block needs to immediately follow the end of a try {} block.

    Networks always choke everything, no matter the app. Windows explorer itself still hangs for a long time trying to connect to a network resource that won't communicate or doesn't exist. There's no "stop" button on it, it just freezes and sometimes if you end the task your entire OS (running on an explorer) restarts the desktop haha. So network issues have always caused apps to halt.

    You should validate the IP entered. Two ways to go about that are you can separate the text entry boxes into 4 boxes, one for each part of the IP address, (e.g. [ ] . [ ] . [ ] . [ ]). Then you just get the .text of each of the 4 boxes and validate like that. This is a really easy approach.

    Otherwise to do a single TextField it's getting into more advanced areas of ActionScript but I'll show you an example.

    Rather than using regular expressions, a simple string check and then a technique can be used to validate the IP. You expect 4 numbers separated by periods and nothing else. One simple way is to first even verify periods exist, then split the numbers up by the period into an Array. You can then verify 4 numbers exist and that each one is 255 or lower.

    e.g. an example frame script you can try (paste it in a new document and run it:

    var a:String = '123.123.123.123';

    trace(validateIP(a));

    function validateIP(str:String):Boolean

    {

    trace('Validating IP: ' + str);

    if (!str) return false;

    trace('We have a string. Checking for periods');

    if (str.indexOf('.') == -1) return false;

    trace('We have a period, splitting the string by the periods.');

    var IPNumbers:Array = str.split('.');

    trace('We have ' + IPNumbers.length + ' number(s), making sure there are exactly 4');

    if (IPNumbers.length != 4) return false;

    trace('We have 4 potential numbers, check all of them.');

    for (var i:int = 0; i < IPNumbers.length; i++)

    {

    trace('Is ' + int(IPNumbers) + ' NaN?');

    if (int(IPNumbers) != NaN)

    {

    // (note the first number shouldn't be 0 but you

    // can add that logic if you like)

    // so is it less than 0 or over 255? return false if so

    trace('It is a number, is it in range 0-255?');

    if ( int(IPNumbers) < 0 || int(IPNumbers) > 255 ) return false;

    trace('Yes, in range, moving on.');

    }

    else

    {

    // NaN found, invalid IP, return false

    trace('Number is NaN, invalid.');

    return false;

    }

    }

    trace('All 4 numbers are in range so this might be a valid IP, returning true\n');

    return true;

    }

    When run on the IP I gave it returns true. Try some other values. Do mind the note that it will consider 0.0.0.0 a valid IP as much as 1.0.0.0, but you know they're not.

    I can only do so much for you, it's your college class so if you need to strengthen it up, what I gave you is a starting point. Easier to grasp than Regular Expressions at least.

    On the movement issues, read this:

    Tween - Adobe ActionScript® 3 (AS3 Flash) API Reference

    Just as you can tween on the timeline (animate properties like x/y/scale/alpha/rotate/etc) you can do it with code too. That class is built-in and pretty self explanatory.

    Votes

    Upvote

    Translate

    Translate

    Report

    Report

    • Follow
    • Report

    Community guidelines

    Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (15)

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (16)

    Zarhon AUTHOR

    New Here ,

    /t5/animate-discussions/establishing-a-socket-connection-between-a-swf-file-and-a-socket-test-program-tcp-ip-builder-windows/m-p/6438531#M149162 Oct 14, 2014 Oct 14, 2014

    Copy link to clipboard

    Copied

    In Response To sinious

    I'm made some 'reverse' and 'forward' animations, and now I'm having issues 'calling' them properly to animate.

    The intent it to have the following sequence:
    Read textbox to identify current 'mouth expression' -> play 'reverse' animation of current mouth expression -> play animation of new expression.

    The error occurs with the 'play reverse' part.

    Here's how the nested frames look.

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (17)

    The animations have an empty frame, play to 'appear', then stop(); at the last frame. Then there's an empty frame, and the 'reverse' animation of those frames, starting with a named frame (Rev_[something]) so I can call it to play from that point, and then it stop(); again.

    I've made sure to add names to the "instances" in the 'faces' layer.

    Here's how my code for calling them works.

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (18)

    I get this error when I try to run those parts of the code:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    at Smiley_TCP_IP_server_v15_fla::MainTimeline/handler()[Smiley_TCP_IP_server_v15_fla.MainTimeline::frame2:73]

    Debug confirms that the errors are on the "reverse-animation-calling" lines of the code.

    I'm guessing I'm incorrectly 'calling' the frames to play (or it cant find/identify them), but it *should* be correct from what the 'instance' and 'frame' names are. Where's the error that's causing the crash? How am I supposed to 'correctly' call them?

    Dropbox link to .fla file:

    Dropbox - Smiley_TCP_IP_server_v15.fla

    Additionally, I'm guessing with how the code is set up currently, the reverse and forward animations will both run at the same time, rather than one after the other. How do I get them to go in sequence (one finishes, and then the next "goto" command starts)?

    Votes

    Upvote

    Translate

    Translate

    Report

    Report

    • Follow
    • Report

    Community guidelines

    Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (19)

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (20)

    LEGEND ,

    /t5/animate-discussions/establishing-a-socket-connection-between-a-swf-file-and-a-socket-test-program-tcp-ip-builder-windows/m-p/6438532#M149163 Oct 15, 2014 Oct 15, 2014

    Copy link to clipboard

    Copied

    LATEST

    In Response To Zarhon

    Everything you need is in the debuggers error. I often second guess the debugger having been in the trenches so long but I've learned that 99% of the time, I'm wrong, 1% of the time I might have stumbled on an ultra rare bug. I'm guessing you're in the 99% so take a look at the error. It says the error is exactly in the FLA you posted, MainTimeline/handler(), frame 2, line 73. That line of code is in your picture:

    wholebody.mouth.smileframe.gotoAndPlay("Rev_neutral");

    Without even downloading I'm going to assume this does not exist at the time in which it is called. Re-read that sentence because it's loaded. Remember that if you ever replace parts of the body (say change the smile symbol, etc) it must contain a frame label named "Rev_neutral".

    *downloads file to verify*

    And there's the problem. You have 4 frames in your "wholebody.mouth" symbol. Frame 1 has "Rev_smile", frame 2 "REV_NOPE", frame 3... ahh, there's the "Rev_neutral".

    You're running that branching code when you shouldn't. You should only seek a frame label if you know it exists. That requires you to pay attention to the current "state" of the face and all the various parts.

    Quite frankly this is where the teacher should be coming in to show you how to do things like this. If they're just handing this giant project over to you without even explaining the basics of Flash then you should give them a heads up that it'll be impossible to complete this project with zero help. I'd like to keep helping as well but the deeper into the project you get the more I feel you're getting off the path you should have went down to create it originally.

    I'll briefly explain something you can do to work with what you have but your teacher needs to kick it in from here. But please take what I'm saying very loosely. I'm explaining a simple concept more than giving you code to complete your task. This is just for illustration of what you could do. There's a million ways to handle the same situation.

    You can create something simple like an array or object (preferred) to track the state of each piece, in the same dot-notation syntax you're already doing. Or, you can run a lot of branched code to check on your existing character before ever doing anything. Both are really the same thing, one just requires less functions.

    Say you were to track the current mouth position. You know there's 4 potential mouth positions, the instance names tell them: "smileframe", "nopeframe", "neutralframe" and "gaspframe". You can assign a variable to instantly tell you which position it's in and only run the correct animation from that.

    e.g.

    var faceState:Object = new Object();

    faceState.mouth = new Object();

    // set default state, name of symbol and frame number it's on

    faceState.mouth.name = "smileframe";

    faceState.mouth.frame = 1;

    function updateMouth():void

    {

    // see if the mouth is already on the right frame

    if (wholebody.mouth.currentFrame != faceState.mouth.frame)

    {

    wholebody.mouth.gotoAndStop(faceState.mouth.frame);

    }

    }

    Now when you want to change to a new type of mouth you already know what state the mouth is in, set by default to smileyframe. The reason I need both values (frame and name) is I use them separately in a single function for different purposes.

    Say you want the mouth in the "nopeframe" position. First start the rewind animation of the current mouth and set the new state values at the same time. At the end of the rewind animation it should fire off the "updateMouth()" function which just sets the mouth to your desired frame:

    // rewind the current mouth using the universally named "rewind" frame label

    wholebody.mouth[faceState.mouth.name].gotoAndPlay("reverse");

    // at the end of that reverse sequence the updateMouth() function should fire off, so reset your target mouth

    // here's the state we want to be in

    faceState.mouth.name = "nopeframe";

    faceState.mouth.frame = 2;

    Now you have your smileyframe mouth running the reverse animation. At the end it fires off updateFrame() function from a frame script which checks to see if the currentFrame is the frame we want (the "nopeframe" or frame 2). It's not so it changes the mouth to frame 2.

    There you've daisy chained some animation.

    I realize this is a bit complex for a new Flash user and honestly it's never the way I'd actually build something like this but you're very deep in your project and your teacher really needs to help you with this. You can't be expected to randomly learn Flash with no training.

    Good luck on your project!

    Votes

    Upvote

    Translate

    Translate

    Report

    Report

    • Follow
    • Report

    Community guidelines

    Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (21)

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (22)

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (23)

      • 1
      • 2
      • 3

    Resources

    Learn

    Get started with Adobe Animate guide

    Quick guide for keyboard shortcuts

    How to make an ad for Adwords in Animate CC?

    Drawing in Animate

    Latest Features

    What's New in Adobe Animate?

    Copyright © 2024 Adobe. All rights reserved.

    Using the Community Experience League Terms of Use Privacy Cookie preferences Do not sell or share my personal information AdChoices

    Re: Re: Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3. (2025)

    References

    Top Articles
    Latest Posts
    Recommended Articles
    Article information

    Author: Nathanael Baumbach

    Last Updated:

    Views: 5826

    Rating: 4.4 / 5 (75 voted)

    Reviews: 82% of readers found this page helpful

    Author information

    Name: Nathanael Baumbach

    Birthday: 1998-12-02

    Address: Apt. 829 751 Glover View, West Orlando, IN 22436

    Phone: +901025288581

    Job: Internal IT Coordinator

    Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

    Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.