Skip to main content
Inspiring
April 12, 2015
Answered

How to Make Multiplayer TCP Flash Game?

  • April 12, 2015
  • 1 reply
  • 5823 views

I have a game in the works, and though I got the UDP to work perfectly, I sadly later found out that that is simply just a lossy protocol. There's apparently (as far as my knowledge goes) nothing you can do to make it resend data if the data was not received successfully by the recipient.

So, I was wondering how to use TCP? I've heard it's possible, but I can't seem to find a single tutorial or any information anywhere on how to do it. I found on Adobe's site socket connections, and though I got the server to connect (I think), I have no clue how to send data to it, how to get other clients to connect to it, etc. Any help?

This is the script I've been using, and though it does connect... that's basically all it does, I don't know where to go from here...

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." );

        }

}}

This topic has been closed for replies.
Correct answer kglad

unless you're using a server that all players connect to, you should be using adobe's peer-to-peer networking (rtmfp).

1 reply

kglad
kgladCorrect answer
Community Expert
April 12, 2015

unless you're using a server that all players connect to, you should be using adobe's peer-to-peer networking (rtmfp).

Inspiring
April 12, 2015

Thank you, I actually just found some documents online (one a server, one a client) and got the client to connect, as long as the server is on the same computer. I tried putting the server on one computer and a client on the other but the connection failed... is there something I'm missing here?

Here's an example of the client's script:

sock.connect('127.0.0.1',8910);

// receive data from socket

function onSocketData(e:ProgressEvent):void

{

    trace(sock.bytesAvailable + ' byte(s) available');

    // read the socket data

    if (sock.bytesAvailable > 0)

    {

        // (I know it's a string for this example)

        var msg:String = sock.readUTFBytes(sock.bytesAvailable);

       

        trace(msg);

       

        if (msg == 'Connected.')

        {

            // send a message back to the server (which it will return to me to confirm it worked)

            sock.writeUTFBytes('Thanks for connecting!');

            gotoAndStop(2);

        }

        // flush!

        sock.flush();

    }

}

Thank you.

Inspiring
April 13, 2015

you'll need to use the correct ip for the non-local client.


Alright, I looked up that the IP to connect to the LAN for the server is supposed to be like this:

serverSocket.bind( 8080, "192.168.1.4" );

But I get this error:

Error #2002: Operation attempted on invalid socket.

It seems to only work when I set it to:

(8910, '127.0.0.1');

What's going on here?