DNX: C#: Dynamic Keyword

As you may know, Microsoft is getting into the open source community with the ASP.NET and DNX with the mono project.
The .NET Execution Environment (DNX) is a software development kit which allows you to run .NET applications on Windows, Mac, and Linux using different frameworks such as .NET framework, .NET Core, and Mono. However, everything are not roses. There are many incomplete libraries, incompatibilities, lack of documentation, and most of the examples such as doing SQL or SOAP do not work depending on which the library targets you are planning to code for. Therefore, I decided to test the basics on all the library targets. I am starting with DNX451.

The dynamic keyword seems to be working fine in DNX451 with C#. Below is a code example for those who wish to play with it.


using System;

namespace ConsoleApp1 {
	public class Program {
		public void Main(string[] args){			
			new DynamicKeywordExample.DynamicKeywordExample();
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");

using System;

namespace DynamicKeywordExample {
	public class DynamicKeywordExample {
		public DynamicKeywordExample(){
			var test = "This is a string";
			//test = 5; // This will fail because cannot implicitly convert type 'int' to 'string'
			Console.WriteLine("Value: {0}", test);
			// To be able to change to any time you want use Dynamic DynamicKeywordExample
			dynamic test2 = "This is another string";
			Console.WriteLine("Value: {0}", test2);
			test2 = 10;
			Console.WriteLine("Value: {0}", test2);

			// Using Dynamic is bad for performance

AGCRM-MacBook-Pro:Examples user$ dnx . me
Compiled on DNX451: .NET Framework

Value: This is a string
Value: This is another string
Value: 10
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Dynamic Keyword," in Alejandro G. Carlstein Ramos Mejia Blog, September 2, 2015, http://www.acarlstein.com/?p=3182.

Programming Languages Q&A – Part 1

NOTIFICATION: These examples are provided for educational purposes. The use of this code and/or information is under your own responsibility and risk. The information and/or code is given ‘as is’. I do not take responsibilities of how they are used.

If any of these answers is incorrect or you wish a different explanation, please let me know by commenting.

  1. What is “Binding Time”?
    Binding time is the time in which two things associate (also known as binding) or the time in which any implementation decision is made.
  2. What is the distinction between decisions that are bound statically to those bound dynamically?
    Anything that is bound before run-time are considered bound statically while anything that is bound after run-time are bound dynamically.
  3. What is the distinction between the lifetime of a name-to-object and the visibility of a name-to-object?
    The lifetime of a name-to-object is the time between the creating and the destruction of the name-to-object. This is also known as binding’s lifetime.
    The visibility of a name-to-object is decided by the scope used on the programming language. This means that depending of how the scope works (dynamically or statically), we can access to the name-to-object.
  4. How is determined when an object is allocated on the stack, on the heap, or allocated statically?
    1. An object is allocated and deallocated in the stack (last-in, first out order memory) when the objects are in conjunction with a subroutine calls and returns.
    2. When an object is needed to be allocated and deallocated at arbitrary times, or when the size is decided at run-time, the heap will be used for the object allocation/deallocation.
    3. An object is allocated statically when the object is given an absolute address that is retained thought the whole program’s execution.
  5. What is the information and/or objects that are store in a stack frame?
    1. Global variables
    2. Program instructions
    3. Local variables
    4. Constants (including numeric and string valued constants)
    5. Run-time tables
    6. Frames (also known as activation records) with are composed of:
      Arguments, return values, local variables, temporaries, and bookkeeping.
  6. What is a frame pointer and what it is used for?
    A frame pointer is a register that is used for pointing to a location in which the frame of the current subroutine is located.
    When a code require to access to a local variable within the current frame or an argument, the frame pointer is used.
  7. Is a calling sequence a subroutine?
    Yes. The calling sequence  whole purpose is to maintain the stack. This subroutine will be executed before and after the caller call for the prologue and epilogue of the subroutine itself.
  8. What is the prologue and epilogue?
    The prologue is the code executed at the beginning.
    The epilogue is the code executed at the end.
  9. When does an internal fragmentation occurs?
    An internal fragmentation occurs when a block larger than the object to be hold is allocated by the storage-management algorithm. This mean an extra space is unused.
  10. When does an external fragmentation occurs?
    An external fragmentation occurs when an active object have blocks assigned to it scattered though the heap in such a way that there exist unused space composed of multiple blocks. This could create a problem since there could not be a block that would be large enough to satisfy a request for another object in the future.
  11. What is garbage collection?
    The basic concept of garbage collection is a run-time library with the purpose of identifying and reclaim space which is unused or have unreachable objects.
  12. What is static scoping?
    We call static scoping when the binding between names and objects is determined at compile-time when we analyse the program’s text without having in consideration the flow of the program.
  13. What is dynamic scoping?
    We call dynamic scoping when the bindings depend on the flow of the program at run-time.
  14. In programming languages such as Ada, what called elaboration?
    In programming languages such as Ada, when a process by which declarations have become active when the control first enters a scope is called an elaboration. In short, we call elaboration when there is a creation of bindings.
  15. What do we call a referencing environment?
    We refer to a set of active bindings (determined by static or dynamic scope rules) as current referencing environment.  This means that referencing environment are dependent of binding rules.
  16. What is a declaration?
    A declaration is when a name (variable) is introduced and it indicates its scope but may omit certain implementation details. An example in C:

    extern int integer_variable;
  17. What is a definition?
    A definition is a name (variable) which describe an object with enough details for which the compiler can determine its implementation. An example in C:

    int integer_variable = 10;
  18. What is the distinction between the declaration and a definition that is so important?
    A name (variable) is required to be declared before it can be used. This means that by just defining a name, it does not mean that we can use it. We must declared first. A definition do not provide enough information to the compiler to know how much memory must be allocated and/or the scope that would be given to the name.
  19. What is information hiding and why is it important?
    The main idea of information hiding is to make some portion of an algorithm invisible. By making an interface, we reduce the “cognitive load” on a programmer. Reducing the “cognitive load” means that the programmer have less to worry about and can focus on the rest.
    Information hiding also reduce the risk of name conflict and provide safeguards on the integrity of data abstractions.
  20. What are aliases and why they are considered a problem in language design and/or implementation?
    Names that are referencing to a same object are called Aliases. A problem that may occur is when inside a subroutine, two names are pointing to a same location of memory created locally in the subroutine. If this subroutine goes out of scope, then any of these names may be pointing to a location of memory that does not exist anymore for that subroutine. Example in C:

    void f(int *pointer_to_variable){
      int integer_variable = 10;
      pointer_to_variable = &integer_variable;
    int main(int argc, char* argv[]){
      int *integer_pointer;
      printf('value: %d', *integer_pointer);
      return 0;

    This means that we may print anything else, since integer_variable would goes out of scope when returning from the funtion f.
    So, integer_pointer would be pointing to an address in memory where any other value than 10 would be store.


Introduction to Network Security – Part 3

NOTIFICATION: These examples are provided for educational purposes. The use of this code and/or information is under your own responsibility and risk. The information and/or code is given ‘as is’. I do not take responsibilities of how they are used.

Socket Programming

In networks applications, communications are almost always done using a client-server model.

A client will perform request for a service to the server, and receive a service from the server (which should be usually on). The only thing that the client needs to know is the address of the server and which port to use. Once the connection between the client and the server is establish, then the client and the server can send and receive information back and forwards. Eg. FTP client to a FTP server

The client usually should communicate with only one server at a time; however, there are exceptions such as the web browser. In today web browsers, a web browsers could connect to different servers in order to download all the elements of a page faster. A page could indicate that the images are from one server, while the flash animation is coming from a different server.

In order to a client and a server to communicate they must use sockets in order to send and receive messages from each other. A socket is used by a sending process to send a message to the transport infrastructure which deliver the message to a receiving process which also is using a socket to receive the message.

The first step for a process that is receiving a message is to have an identifier. This identifier must include the IP address and a port number. The port number must be associated with the process. The port number is a 16-bit number used to identify the application process. Notice that an IP address do not identify a process but the port does, so you can have more than one process running at the host which means different ports can be used at the same time with the same IP address.

IP Address

Right now, October 27 of 2010, there are two Internet Protocols (IP) available: IPv4 and IPv6.

  1. IPv4 (IP version 4) is a 32-bit binary number represented by using 4 decimal values, separated by periods, in dotted decimal notation. Each decimal value is an octed (8-bits) which range between 0 to 255. Example:  would be 11000000.10101000.00000001.01100101.
    1. 192 → 11000000
    2. 168 → 10101000
    3. 1      → 00000001
    4. 101 → 01100101
  2. IPv6 (IP version 6) is designed to succeed IPv4 (IP version 4). Instead of a 32-bit binary number, IPv6 use 128-bits. This means that the use of Notation Address Translation (NAT) is not needed as in IPv4. Network security such as IPSec (http://en.wikipedia.org/wiki/IPsec) which allow to authenticate and encrypt IP packages have being incorporated.


As explained previously a port is a 16-bit identifier number which identify the type of application process, this means that there are a total number of 65535 ports number available. For more information you can go to the Internet Assigned Numbers Authority (IANA) <http://www.iana.org> . This institution is in charge of the global coordination of the DNS Root, IP addressing, and other Internet protocol resources.

  1. Ports from 0 to 1023 are called well-known ports.
    1. Examples of reserved ports: HTTP (Port 80), FTP (Port 21), Telnet (Port 23), STMP (Port 25), etc.
  2. Ports from 1024 through 49151 are called reserved ports.
  3. Ports from 49152 to 65535 are called Dynamic ports, Private ports, or Ephemeral ports.
  4. For more information you can go to <http://www.iana.org/assignments/port-numbers>

TCP Socket on Client Side.

  1. Create a socket to create an endpoint for communication.
    1. (In Linux) ANSI C:
      int socket(int domain, int type, int protocol);
    2. For more information: <http://linux.die.net/man/7/sockets>
  2. Specify the server’s IP address and port to connect
  3. Establish a connection initiate a connection on a socket with the server.
    1. (In Linux) ANSI C:
      int connect(int sockfd, const struct sockaddr *addr,  socklen_t addrlen);
    2. For more information: <http://linux.die.net/man/2/connect>
  4. Send data to the server and receive data from the server
  5. Close the connection
Here is an example of a client/server program using ANSI C in Linux: <http://www.acarlstein.com/?p=891>
TCP Socket on Server Side
  1. Create a socket to create an endpoint for communication.
    1. (In Linux) ANSI C:
      int socket(int domain, int type, int protocol);
    2. For more information: <http://linux.die.net/man/7/sockets>
  2. Bind the socket with an address to specify the server’s IP address and port.
    1. (In Linux) ANSI C:
      int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
    2. For more Information: <http://linux.die.net/man/2/bind>
  3. Listen form incoming connections.
    1. (In Linux) ANSI C:
      int listen(int sockfd, int backlog);
    2. For more information: <http://linux.die.net/man/2/listen>
  4. Accept the connection with the client.
    1. (In Linux) ANSI C:
      int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
    2. For more information: <http://linux.die.net/man/2/accept>
  5. Send data to the server and receive data from the server
  6. Close the connection

Here is an example of a client/server program using ANSI C in Linux: <http://www.acarlstein.com/?p=891>