dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: problem with 2-way communication on sockets server

  1. #1
    Hi all,


    I have a simple flash script that, upon connecting successfully to a perl socket server, loops around sending an incrementing counter to the server and expecting to receive that counter back in the SWF in the form of a dynamic text box variable (status). Sending the counter to the server works fine - it increments and prints out to STDOUT. Receiving it back works the first time into the loop. The next iteration, I get nothing and the dynamictext box is blanked out. I'm currently using XML to send data back to the SWF to keep things simple and to conform to what XMLSocket, on the face of it, seems to require - this is my first experience with XMLSockets in Flash - but eventually be able to receive back raw data (as I'm able to do sending to the server). I'm using Flash 5.

    Here's the initialisation frame *before* entering the loop at "success":

    myXML = new XMLSocket();
    myXML.onConnect = myOnConnect;
    myXML.onXML = myOnXML;
    myXML.connect(null, 12345);

    stop();

    function myOnConnect(success)
    {
    if(success)
    {
    gotoAndPlay("success");
    }
    else
    {
    gotoAndStop("failure");
    }
    }

    function myOnXML(input)
    {
    e = input.firstChild;
    if (e != null && e.nodeName == "MESSAGE") {
    status = e.attributes.inc;
    }
    // status = input;
    }

    According to my understanding of the onXML method, I don't need to do anything else. All XML string sent back to flash will automatically be dealt with by the myOnXML method.


    My perl socketserver is:

    #!E:\perl\bin\perl -wl
    use Socket;
    use FileHandle;
    use strict;
    use Carp;
    $| = 1;
    $/ = "\0";

    my $port = 12345;

    socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
    setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
    SERVER->autoflush();
    CLIENT->autoflush();

    my $addr = sockaddr_in($port, INADDR_ANY);
    bind(SERVER, $addr) or die "Couldn't bind to port $port: $!\n";

    listen(SERVER, SOMAXCONN) or die "Couldn't listen on port $port: $!\n";


    while(1)
    {
    accept(CLIENT, SERVER);
    print "Client connected!";
    while(<CLIENT>)
    {
    chomp;
    print $_;
    my $message = qq(<MESSAGE INC="$_" />);
    print CLIENT "$message\000";
    }
    print "Client disconnected!";
    }
    close(SERVER);



    Anyone got any ideas?

  2. #2
    no-one?

  3. #3
    still no luck - been trying with onData:

    myXML = new XMLSocket();
    myXML.onData = myOnData;

    function myOnData(input)
    {
    status = input;
    }

    Once again, I just get status the first time through the loop then nothing.

    Can anyone help?

  4. #4
    still no-one?!?!?!?!?

  5. #5
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    I got that to work with slight changes to your perl script:
    Code:
    #!/usr/bin/perl
    
    use Socket;
    $| = 1;
    $/ = "\0";
    
    my $port = 12345;
    
    socket(SERVER, AF_INET, SOCK_STREAM, getprotobyname('tcp'));
    setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
    select SERVER; $| = 1; select STDOUT;
    
    my $addr = sockaddr_in($port, INADDR_ANY);
    bind(SERVER, $addr) or die "cannot bind to $port\n";
    listen(SERVER, 5) or die "cannot listen\n";	## without that, does not do anything sensible :)
    
    while(1)
    {       $paddr = accept(CLIENT, SERVER);
            my ($dummy, $iport, $iaddr) = unpack('n n N x8', $paddr);	# just for reporting
            print "connected from $iport\n";
            select CLIENT; $| = 1; select STDOUT;	# unblock CLIENT socket here, it is not defined before anything connects
            while(<CLIENT>)
            {       chomp;
                    print "$_\n";
                    print CLIENT "<MESSAGE INC=\"$_\" />\000";
            }
            print "disconnected\n";
            close CLIENT;	# also close CLIENT rather than SERVER (listen) socket here
    }
    Musicman

  6. #6
    Thanks for the attempt but I get the same result as before - I get my server printing the SWF input to the command line (STDOUT) but only sending it back to the client once and once only. The major change that you made to my code is to manually flush the pipes - the autoflush() method that I used does this anyway. Additionally, you flushed CLIENT after it had been 'defined'. However, I got exactly the same result wherever I flushed CLIENT.

    I think the problem is not in the perl script but in the flash implementation of .onData().

  7. #7
    Hi Musicman,

    I'd love to see the movie and script.

    The listen command was always there.

    Closing the CLIENT socket and moving it to within the outer while(1) loop has no effect.

  8. #8
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    I sent you a mail through the board but probably it did not reach its target. Maybe send me a mail through the board, if you dont want to show your email in public

    Musicman

  9. #9
    didn't get your email.

    my email is sir_fragalot_nz@hotmail.com

    you can ftp my fla file and the server script at:

    http://www.music.salford.ac.uk/~mark/Socket.zip

  10. #10
    well, after all that.......

    got your server code, edited it, stripped it down until it was exactly the same as mine and still working. The answer turned out to be the -l flag on my shebang line. Take that away and my server script all works. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center