6 Replies - 985 Views - Last Post: 22 April 2009 - 10:37 AM

#1 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Trying to understand actionscript + networks

Posted 21 April 2009 - 02:06 PM

Hi everyone,

I've been trying to take what I have designed on paper and apply it to real code, but have been failing. I can't seem to grasp how an application can keep up with the status of other members in real time.

Lets say I wanted to create a game in flash that has 2 people currently logged in. To keep things simple, let's say each player is just a small black circle movie clip, and our background is the default white background.

It's decently easy to be able to allow a player to control his black circle with the arrow keys using keypresses when the .swf loads. What about displaying his friend who is running the .swf in another window on another machine?

I honestly can't figure out how something like this is done, but I've seen it done OVER AND OVER AND OVER, so someone out there understands the networking code behind it.


What I have thought about doing, although most likely wrong, but certainly an alternative:
I know that FLASH can handle PHP code, and was thinking of using PHP to talk to an SQL database to pull current coordinates of both players and display them, having this code execute every second to keep the game updated. The problem with this is that it would be a LOT of SQL queries, and it may not update fast enough to appear "smooth".


What exactly do I do? Please help me get on the right path.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to understand actionscript + networks

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: Trying to understand actionscript + networks

Posted 21 April 2009 - 03:55 PM

Well, the problem with that is that Flash will open up new connections with the server each time it needs to update, which could be multiple times a second.

What I would suggest is creating a simple Python server application which takes socket connections and relays messages between people who connect to it. This could work fine, but you would have to have some way of connecting between two and only two people at a time, this could be connecting people who log in as they log in to one other person, or only allowing two people to connect at a time.

Hope that sense.
Was This Post Helpful? 0
  • +
  • -

#3 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Trying to understand actionscript + networks

Posted 21 April 2009 - 06:25 PM

View PostBetaWar, on 21 Apr, 2009 - 02:55 PM, said:

Well, the problem with that is that Flash will open up new connections with the server each time it needs to update, which could be multiple
times a second.

What I would suggest is creating a simple Python server application which takes socket connections and relays messages between people who connect to it. This could work fine, but you would have to have some way of connecting between two and only two people at a time, this could be connecting people who log in as they log in to one other person, or only allowing two people to connect at a time.

Hope that sense.


Hello again, BetaWar! Been a while (6 months+)!

-I mentioned 2 people for simplicity's sake. Why try to teach someone about online gaming with 100 people when we can focus on only 2 people? I don't want to limit only 2 connections :-P

-Python and Flash? We seem to be leaving the realm of knowledge I am currently residing in... lol
I don't know anything about Python. I just assumed most online games, regardless of the language platform, use some kind of database (such as SQL) to store user data and then use another language or program to manipulate the data in real time. Is Python worth learning?

I don't want to discredit you, but I've googled "Flash MMO" or "Flash Online Games" once or twice and have never seen a mention of Python, so maybe you are incorrect here?


Please just point me to where I can go to learn and I will commit myself 110% to it! I'm not looking for an easy way out! You can count on it.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: Trying to understand actionscript + networks

Posted 21 April 2009 - 07:39 PM

Quote

I don't want to discredit you, but I've googled "Flash MMO" or "Flash Online Games" once or twice and have never seen a mention of Python, so maybe you are incorrect here?

I may not be exactly on the ball, but MMos will require a socket connection with a "server" program which will manipulate data and send it to the correct connections. That is what battle.net, arena.net and things like runescape use. The only thing that changes between them is the user interface source. Runescape is a Java Applet, but Blizzard and Arena net use C/C++ executables to communicate with the server.

I said use Python because I know you can easily create a simple program to act as a socket server in Python and have it communicate with flash. You will need to ahve that same python application talk to a database if you want to save anything, but for a simple game of tic tac toe (or somethign of the like) I don't think people will be too interested in their saved games.

If you are going for something more indepth (like an MMO) you will hae to have somewhere to save data, at which point a database would be the best way to go.

You could use Java, Python, C#, C++, or whatever you wanted really to communicate between flash and the server, it is all just a trade off between speed and simplicity. C/C++ would be faster, but Java and Python will be easier to get set up and going.

Here is a quick tutorial on how to accomplish a python and flash simple socket client/ server relationship:
http://www.dreaminco...wtopic66135.htm

and I later solved the issues I was having with is:

Quote

So, basically when you (or the Flash app) attempts to connect with a host (program listening on the correct port at the given IP address). If you are doing this in Pyhton you can do something like so:
message = client[0].recv(1024)
				if message:
					 if message == "<policy-file-request/>\0":
							  client[0].send(policyTxt)


Where the policyTxt variable is filled with this:
policyTxt = "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"2727\" /></cross-domain-policy>\0"


That basically tells Flash that anything is allowed to connect to this host and that it is okay to open up a socket and communicate between the 2 of them.

The "\0" at the end of the policyTxt variable and the if statement for message is a null byte character, which Flash sends out at the end of all of their messages, and requires to be sent to know that the sent message was completed.


Forgive any issues with the text above, I copied and pasted from something I wrote a while ago.

Any socket server can be made to allow more than 2 connections, you just have to have separate computers and get the server to echo input data to all those connected. Flash is good with socket connections as it sends a socket disconnect when you unload the swf, so you can see quickly who is connected.

For information on python's database API, here is a link:
http://www.python.or.../peps/pep-0249/

I was only saying that PHP is probably not the right thing to use as it will need to either hold a socket connection open (not really a good idea with PHP, as it could be hazardous to security) or reload a php script each time you want to connect to the database, which will be quite often.

Now, I may not be the best one to talk about how to maek a game with any success, as I haven't done much with game programming, but I would assume that making an mmo would be a lot like making an IRC client (which I have done to an extent) so that is just my 2 cents.

Hope that was of use :)
Was This Post Helpful? 1
  • +
  • -

#5 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Trying to understand actionscript + networks

Posted 21 April 2009 - 09:16 PM

You're a really nice guy, BetaWar, and I thank you for the time you spend replying.

I will happily spend some time reading this!

I once had a professor who talked about Python, but I, as a Network Security major (about to graduate with a bachelor's of science), have only done some programming in C++. We don't do much programming :-P (as an aside...my knowledge in Flash, Actionscript, PHP, and MySQL is all self-taught in my own time).


After I read your nifty tutorial on Flash/Python Client/Server connections, I'll check out general programming in Python.


Give me your opinion - Is Python considered a powerful language compared to C++? Is it new?
Was This Post Helpful? 0
  • +
  • -

#6 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Trying to understand actionscript + networks

Posted 21 April 2009 - 11:12 PM

So I spent like 2 hours learning the basics of python. From super basics of getting it to run in IDLE up to using def to create my own functions and stuff. Seems neat so far.

Ran through your socket tutorial in python and flash using XML, and surprisingly was very easy to not only "copy" but to learn and understand as well! Thanks!


Problems:

I am currently running the server in Python, and running the Flash file.

Here is my server output, so I know it is working:

Quote

We have opened a connection with ('127.0.0.1', 1630)

We have opened a connection with ('127.0.0.1', 1631)

We have opened a connection with ('127.0.0.1', 1632)

We have opened a connection with ('127.0.0.1', 1633)

We have opened a connection with ('127.0.0.1', 1634)

We have opened a connection with ('127.0.0.1', 1635)

We have opened a connection with ('127.0.0.1', 1636)


As you can see, there is a gap between each connection.. this is because, for some reason, I am not receiving in the message sent from the client.

Moving to the Flash Client, my only output is

Quote

Connected.


Something is definitely wrong with the sending and receiving of the actual data. Can you help me troubleshoot before moving on to the next, more fun things I plan to do with this? :-P

Also received this error when trying to switch localhost for my real IP:

Quote

Traceback (most recent call last):
File "C:\Documents and Settings\Jeremy\Desktop\prog.py", line 7, in -toplevel-
mySocket.bind (('98.109.218.123',2727))
File "<string>", line 1, in bind
error: (10049, "Can't assign requested address")

EDIT: Found this on a tutorial website online:
A couple things to notice: we used socket.gethostname() so that the socket would be visible to the outside world. If we had used s.bind(('', 80)) or s.bind(('localhost', 80)) or s.bind(('127.0.0.1', 80)) we would still have a "server" socket, but one that was only visible within the same machine.
Can you help decipher what he's saying? If I do ((socket.gethostname(), 2727)), what IP do I tell my CLIENT (flash) to connect to?


Sorry, this may be a lot to handle, but if you break it down into chunks, I'm really only asking like 3 things :-P. BetaWar, I <3 You

Looking forward to my reply in the morning! Good Night!

This post has been edited by Xioshin: 21 April 2009 - 11:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: Trying to understand actionscript + networks

Posted 22 April 2009 - 10:37 AM

Quote

Give me your opinion - Is Python considered a powerful language compared to C++? Is it new?

Python is not as powerful as a lower level langauge (like C++), but Python is build with C/C++ so you can easily extend it with C libraries to increase speed and functionality. As for if it is new, I believe it has been around for a few years, so it isn't the newest thing out there, but it is new in comparison to C :)

Quote

A couple things to notice: we used socket.gethostname() so that the socket would be visible to the outside world. If we had used s.bind(('', 80)) or s.bind(('localhost', 80)) or s.bind(('127.0.0.1', 80)) we would still have a "server" socket, but one that was only visible within the same machine.

That is pertaining to connecting to a server with a client application, where the server is on your computer (as is the client), so it is just saying how to connect to your own computer.

You will likely need to use the gethostname() function with the website url that you store the server application at.

If you want to see a simple example of the tutorial, follow this link:
http://reigninggames.../basics_01.html

Here is the source for the server application (python):
import Queue
import socket
import threading

policyTxt = "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"2727\" /></cross-domain-policy>\0"
			
class ClientThread(threading.Thread):
   def run(self):
	  while True:
		 client = clientPool.get()
		 if client != None:
			print 'Received connection:', client[1][0]
			while True:
				message = client[0].recv(1024)
				if message:
					print message
					if message != "<policy-file-request/>\0":
						reply = "ECHO: "+ message
						client[0].send(reply)
				if message == "<policy-file-request/>\0":
					client[0].send(policyTxt)
				if message == "<close-connection/>\0":
					print "Connection closed: ", client[1]
					client[0].close();
					break;

clientPool = Queue.Queue(0)

for x in xrange(10):
   ClientThread().start()

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 2727))
server.listen(5)

while True:
   clientPool.put(server.accept())


It should connect if you open the server first then the url above, and the client (swf) should output "Connected"

The server should print out:

Quote

Recieved connection: 127.0.0.1
<policy-file-request/>
Recieved connection: 127.0.0.1
testing it out - Flash style :)


Then when you type text into the input on the swf and click the send button it should send the message to the server, which will output it and reply with:

Quote

Recieved: ECHO: <message>


When you close the server the client will print out "Connection closed"

As for the third question, I didn't see one... :D

Hope that makes some sense.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1