Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create task-and-gen-tcp.markdown #990

Merged
merged 1 commit into from
Jun 14, 2017
Merged

Conversation

kimshrier
Copy link
Contributor

The KVServer.accept/1 function needs to have an integer port number and System.get_env/1 returns the environment variable value as a string. The reason you need an integer port number is because :gen_tcp.listen/2 calls :inet_tcp.getserv/1 and :inet_tcp.getserv/1 is expecting the port to be either an integer or an atom. In the case that the port is an atom, it is used to look up the port number. For this example code, since we are already assuming an integer port number, just expecting a string version of the integer port number is sufficient.

Also, by passing the entire expression into String.to_integer/1, if the environment variable is missing, you get the missing $PORT message. If the environment variable is present but not a string representation of an integer, you get an ArgumentError that shows the problem being associated with :erlang.binary_to_integer/1, which is a clear indication of the problem.

The KVServer.accept/1 function needs to have an integer port number and System.get_env/1 returns the environment variable value as a string.  The reason you need an integer port number is because :gen_tcp.listen/2 calls :inet_tcp.getserv/1 and :inet_tcp.getserv/1 is expecting the port to be either an integer or an atom.  In the case that the port is an atom, it is used to look up the port number.  For this example code, since we are already assuming an integer port number, just expecting a string version of the integer port number is sufficient.

Also, by passing the entire expression into String.to_integer/1, if the environment variable is missing, you get the missing $PORT message.  If the environment variable is present but not a string representation of an integer, you get an ArgumentError that shows the problem being associated with :erlang.binary_to_integer/1, which is a clear indication of the problem.
@josevalim josevalim merged commit d431504 into elixir-lang:master Jun 14, 2017
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

vladdu pushed a commit to vladdu/elixir-lang.github.com that referenced this pull request Jan 26, 2021
The KVServer.accept/1 function needs to have an integer port number and System.get_env/1 returns the environment variable value as a string.  The reason you need an integer port number is because :gen_tcp.listen/2 calls :inet_tcp.getserv/1 and :inet_tcp.getserv/1 is expecting the port to be either an integer or an atom.  In the case that the port is an atom, it is used to look up the port number.  For this example code, since we are already assuming an integer port number, just expecting a string version of the integer port number is sufficient.

Also, by passing the entire expression into String.to_integer/1, if the environment variable is missing, you get the missing $PORT message.  If the environment variable is present but not a string representation of an integer, you get an ArgumentError that shows the problem being associated with :erlang.binary_to_integer/1, which is a clear indication of the problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants