Skip to main content
Inspiring
January 15, 2015
Question

How to Connect to TCP Server in AIR?

  • January 15, 2015
  • 0 replies
  • 451 views

I have a TCP server that apparently is working and is listening for incoming connections:

package

{

    import flash.display.Sprite;

    import flash.events.EventDispatcher;

    import flash.events.Event;

    import flash.events.*;

    import flash.events.IOErrorEvent;

    import flash.events.ProgressEvent;

    import flash.events.ServerSocketConnectEvent;

    import flash.net.ServerSocket;

    import flash.net.Socket;

    

    public class TCP extends Sprite

    {

        private var serverSocket:ServerSocket;

        private var clientSockets:Array = new Array();

        public function TCP()

        {

            try

            {

                // Create the server socket

                serverSocket = new ServerSocket();

                

                // Add the event listener

                serverSocket.addEventListener( Event.CONNECT, connectHandler );

                serverSocket.addEventListener( Event.CLOSE, onClose );

                

                // Bind to local port 8087

                serverSocket.bind( 8087, "127.0.0.1" );

                

                // Listen for connections

                serverSocket.listen();

                trace( "Listening on " + serverSocket.localPort );

            }

            catch(e:SecurityError)

            {

                trace(e);

            }

        }

        public function connectHandler(event:ServerSocketConnectEvent):void

        {

            //Thesocket is provided by the event object

            var socket:Socket = event.socket as Socket;

            clientSockets.push( socket );

            

            socket.addEventListener( ProgressEvent.SOCKET_DATA, socketDataHandler);

            socket.addEventListener( Event.CLOSE, onClientClose );

            socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );

            

            //Send a connect message

            socket.writeUTFBytes("Connected");

            socket.flush();

            

            trace( "Sending connect message" );

        }

        

        public function socketDataHandler(event:ProgressEvent):void

        {

            var socket:Socket = event.target as Socket

            //Read the message from the socket

            var message:String = socket.readUTFBytes( socket.bytesAvailable );

            trace( "Received: " + message);

            // Echo the received message back to the sender

            message = "Echo -- " + message;

            socket.writeUTFBytes( message );

            socket.flush();

            trace( "Sending: " + message );

        }

        

        private function onClientClose( event:Event ):void

        {

            trace( "Connection to client closed." );

            //Should also remove from clientSockets array...

        }

        private function onIOError( errorEvent:IOErrorEvent ):void

        {

            trace( "IOError: " + errorEvent.text );

        }

        private function onClose( event:Event ):void

        {

            trace( "Server socket closed by OS." );

        }

}}


But how do I connect to it? I'm trying to make a multiplayer game, and I want variables and strings to be communicated to each client, but what code do I have to put in to get one of the  clients to connect to the other? Again, it successfully LISTENS for a connection, but I don't know how to get anything to connect to it. Another thing is that I get an error thrown at me when I try to test the standalone SWF file, it only listens for connections when I hit "test movie" in the FLA... Thanks in advance...

This topic has been closed for replies.