Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:08:23+00:00 2026-06-15T15:08:23+00:00

I am developing a simple framework in Erlang to handle 2-player turn-based games. The

  • 0

I am developing a simple framework in Erlang to handle 2-player turn-based games. The code is the following:

-module(game).
-export([start_server/0,generate_server/0,add_player/0,remove_player/0]).


generate_server() ->
Table_num = 0,
Player_num = 0,
io:format("Server generated...~n", []),
io:format("The current number of tables is ~w~n", [Table_num]),
io:format("The current number of players is ~w~n", [Player_num]),
receive
    login ->
        io:format("A new player has connected!~n", []),
        New = Player_num + 1,
        io:format("The current number of players is ~w~n", [New]);
    logout ->
        io:format("You have beeen succesfully disconnected~n", [])
end.



start_server() ->
    io:format("Welcome player!~nInitializing game...~n", []),
    io:format("Generating server...~n", []),
    register(server,spawn(game, generate_server, [])).


add_player() ->
    server ! login.


remove_player() ->
    server ! logout.

There are two main problems when I run this code:

  1. When I execute add_player() and then remove_player(), this second function crashes with an exception
  2. If I launch the program on one terminal window and then execute add_player() on a second terminal windows, I get an error. What should I do to be able to run it on more than one terminal window?

Any help would be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:08:24+00:00Added an answer on June 15, 2026 at 3:08 pm

    1/
    There is no loop in your server. When you start it, after some print, it waits on the receive statement.

    When it receives the login message, it executes the operations, and then the server code is finished; the process die and is unregistered. All variables disappear and the VM clean up the memory…

    So, later on, any process sending a message to the server will crash because you are using a name which is no more registered.

    To make it works you should keep a list of connected players (in list for example) and recall the server loop with this list as parameter.

    generate_server(Tlist,Plist) ->
    io:format("The current number of tables is ~w~n", [length(Tlist)]),
    io:format("The current number of players is ~w~n", [length(Plist)]),
    receive
        {login,Name} ->
            io:format("A new player ~p has connected!~n", [Name]),
            generate_server(Tlist,[Name|Plist]);
        {logout,Name} ->
            io:format("You have beeen succesfully disconnected~n", []),
            generate_server(Tlist,lists:delete(Name,Plist))
    end.
    

    and the call to generate_server is done by

    register(server,spawn(game, generate_server, [[],[]]))
    

    2/
    in order to use erlang messages between 2 different nodes, you need to:

    • share the same erlang coockie
    • discover the nodes (using netadm for example)
    • get the server pid or use golbal registered name

    see example at http://learnyousomeerlang.com/distribunomicon#alone-in-the-dark

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
I'm developing a simple webapp based on Spring framework. I have these two files
I'm developing a zend framework application that includes a simple email function. The development
I am developing a simple test framework in Java that needs to simulate weblogic
I have 6 months experience with Android, developing simple UI-based applications. Now I want
i am developing a simple RESTful API on slim framework. I had no problem
I am developing a simple website with the TinyWeb framework and Spark view engine.
I'm developing a compact framework 3.5 application on a windows CE based scanner device.
So, i am developing simple web application using Spring Framework. The application users can
I have a simple Zend Framework site I am developing with the addition that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.