• Home
  • Tutorials
  • Downloads
  • Games
  • Articles
  • Contact

Server Preparation

Setting up the server to listen for new connections is about as simple as it was to set up the client.


First, swap to your server program, which should have the 39dll.dll and MyINI.dll installed previously.
Next, create a script called scr_server_init and within this script place the following code:
Picture
This is fairly similar to the connection code for the client.

The port is defined as 50000 (matching the client's defined port), and then the 2 DLL's are initiated. The server needs to initiate MyINI.dll, whereas the client didn't. This is because the client does not have to manage lots of files, but the server does (the player accounts).

Finally, a listening socket is created and stored to a global variable. The value 10 in the call of tcplisten() represents the maximum number of active but not accepted connections to handle, a value of no particular importance right now.

The listening socket allows the server to monitor the specified port for incoming connections. This is important because we want to be able to accept connections from new clients.

Create an object, obj_controller. Run the script created above, scr_server_init(), in the Create event.

The server is now initiated when obj_controller is created. The next step is to make the server accept new connections every step. The server has been set up to do this, now we need to tell it to actually do it.
Add the following code into the Step event of obj_controller:
Picture
The function of this code is simply to accept new connections from clients.

In the initiation script, a listening socket was defined and stored to the variable global.server_listening_socket. In the above code, a temporary variable newsocket is declared, and then set to the return value of tcpaccept(), the function which accepts a connection on a specified listening socket. If it does accept a new connection, the function returns a new socket, otherwise is returns a negative number.

The conditional statement following simply checks whether tcpaccept() actually accepted a new connection.

If the value was greater than or equal to zero (therefor a new socket was created), then an instance of obj_player is created, an object which we haven't defined yet. The function instance_create() returns the ID of the instance it creates. In the code above that value is stored to the temporary variable i, and directly afterward i.socket is set to the value of newsocket.

The new instance of obj_player will now have a local variable named socket which links it directly to the client that originally connected. This is important because whenever the server wants to tell that specific client anything, it sends the message via this new socket!

The network is 50% completed! The client can send a connection attempt, and the server can accept it! However, neither the client or server has a reaction coded in the event that they lose connection, and packets aren't actually read or processed yet. That's next!
Picture
Picture

How To Make An MMORPG © 2010-2012
Copyright | Privacy Policy