• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Question] AS3 Socket Programming

Custom Title Activated
Loyal Member
Joined
Oct 26, 2012
Messages
2,357
Reaction score
1,086
Hello guys,

I have a question. I wanted to create an own SWF with sockets and packets. This is my MessageHandler.as code:

PHP:
package com {
	import flash.utils.Dictionary;
	import flash.utils.ByteArray;
	import flash.net.Socket;
	
	public class MessageHandler {
		
		private const Incoming:Dictionary = new Dictionary();
		private var mSocket:Socket;

		public function MessageHandler(mSocket:Socket) {
			this.mSocket = mSocket;
			
			Object o = new Object();
			o.test = new StartupCrypto(mSocket);
			
			Incoming[1] = o;
		}

		public function Handle(arg1:ByteArray) : void
		{
			var Reader:MessageEventReader = new MessageEventReader(arg1);
			
			var Header:int = Reader.readShort();
			
			if (Incoming[Header] == null)
			{
				this.mSocket.writeUTFBytes("hi");
			}
			
			if (Incoming[Header] != null)
			{
				try
				{
					this.Incoming[Header].
				}
				catch (e:Error)
				{
					throw new Error(e.message, e.errorID);
				}
			}
			else
			{
				this.mSocket.writeUTF("Cannot handle incoming packet " + Header + "!");
			}
		}
	}
	
}

And my TcpConnection.vb code:

PHP:
Imports System.Net.Sockets
Public Class TcpConnection
    Dim mSocket As Socket
    Dim mBuffer As Byte() = New Byte(1024) {}
    Public Sub New(ByRef mSocket As Socket)
        Me.mSocket = mSocket

        Console.WriteLine("Accepted connection from {0}", mSocket.RemoteEndPoint)
    End Sub
    Public Sub Start()
        Me.mSocket.BeginReceive(Me.mBuffer, 0, Me.mBuffer.Length, SocketFlags.None, AddressOf OnReceive, Nothing)
        Me.mSocket.Send(New Byte() {0, 1})
    End Sub
    Private Sub OnReceive(ByVal iAr As IAsyncResult)
        Dim Bits As Integer = Me.mSocket.EndReceive(iAr)

        If (Bits > 0) Then
            Dim Data As String = System.Text.Encoding.Default.GetString(Me.mBuffer, 0, Bits)

            Console.WriteLine("Received " & Data)
        Else
            ' Disconnect user.
        End If
    End Sub
End Class

However, if I send [0][1] (short 1) and read it in flash, it says Incoming[1] is null. How can I make it, Incoming[ID] = a class, and to progress it, it will invoke the constructor of the class?
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Are you trying to send a vB class to flash and receive it as a AS3 class? If so, aint gonna happen, really.. You can send properties of a vB class to flash, and use those properties to instantiate an object in flash, however.
 
Custom Title Activated
Loyal Member
Joined
Oct 26, 2012
Messages
2,357
Reaction score
1,086
Are you trying to send a vB class to flash and receive it as a AS3 class? If so, aint gonna happen, really.. You can send properties of a vB class to flash, and use those properties to instantiate an object in flash, however.

The point is, I send bytes to the flash socket. The socket receives short 1 but it says Incoming dictionary with short 1 is null, but it aint.
 
Chasing 99 Red Balloons
Joined
Jan 9, 2008
Messages
857
Reaction score
229
For one your casting up to an integer var k:int = blah.readShort()

Secondly you don't appear to be calling any functions from the class just accessing it via the array.
 
Back
Top