The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to get connection information about a socket for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Network-aware applications need to be able to detect the kind of network that is available at all times so that they can provide the optimum application responsiveness.

The Microsoft.Phone.Net.NetworkInformation namespace provides extension methods to the Socket class. You can use the GetCurrentNetworkInterface(Socket) method to get information such as whether the network interface uses a GSM or a CDMA cellular network, or whether the network interface uses Cellular or Wi-Fi technology. This topic demonstrates how to retrieve the current connection information for a socket.


For more information about extension methods, see How to: Call an Extension Method (Visual Basic) or Extension Methods (C# Programming Guide).

In the following procedure, you put the code in a button click event for testing purposes only. In your own applications, you can access the properties wherever you need them. The following procedure assumes that you have a Windows Phone application that has a page with a button named button1.

To get connection information about a socket

  1. At the top of the code-behind file for your page, add the following statement.

    using System.Net.Sockets;
    using Microsoft.Phone.Net.NetworkInformation;
    using System.Threading;
    using System.Text;
  2. To get all available information about the network interface, the socket must be connected. Add the following code to your button click event. This creates a socket and attempts to establish a connection.

    Important noteImportant Note:

    For this example to work, you must change the serverName and the portNumber in the following code to refer to a service and its port that you can communicate with over this TCP socket. If this is not done, a time-out will be received for the connection request.

            private void button1_Click(object sender, RoutedEventArgs e)
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                // To run this application, you should specify the name of a server on your network that is running
                // the required service. Please replace the "Placeholder" text below.
                string serverName = "Placeholder";
                // This identifies the port over which to communicate. In this sample, we need to just
                // supply the Echo port number, but you are free to select your own.
                int portNumber = 7;
                // Create DnsEndPoint. 
                DnsEndPoint hostEntry = new DnsEndPoint(serverName, portNumber);
                // Create a SocketAsyncEventArgs object to be used in the connection request.
                SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                socketEventArg.RemoteEndPoint = hostEntry;
                socketEventArg.UserToken = socket;
                socketEventArg.Completed += ShowNetworkInterfaceInformation;
                // // Make an asynchronous Connect request over the socket.

    In the preceding method, we assign the Completed event handler to ShowNetworkInterfaceInformation. This is the callback that is called when the asynchronous socket connection request completes or times out. In that method, we get all the network interface information if the connection was successful.

  3. In the same code-behind page, add the following method.

            /// <summary>
            /// Display the network information using the GetCurrentNetworkInterface extension method on the socket.
            /// </summary>
            /// <remarks>This is the callback from the ConnectAsync method.</remarks>
            void ShowNetworkInterfaceInformation(object s, SocketAsyncEventArgs e)
                // When ConnectAsync was called, it was passed the socket object in
                // the UserToken field of the socketEventArg. This context is retrieved once
                // the ConnectAsync has completed.
                Socket socket = e.UserToken as Socket;
                // Call GetCurrentNetworkInterface only if the connection was successful.
                if (e.SocketError == SocketError.Success)
                    NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();
                    // Use a StringBuilder to efficiently build up an information text about this
                    // NetworkInterfaceInfo.
                    StringBuilder sb = new StringBuilder();
                    // For clarity, append a timestamp so that we can see when this information was gathered.
                    sb.AppendLine("Last Updated: " + DateTime.Now.ToString());
                    sb.Append("Interface Name: ");
                    sb.Append("Interface State: ");
                    sb.Append("Interface Type: ");
                    sb.Append("Interface SubType: ");
                    DisplayMessage(sb.ToString(), "Network Interface Information", MessageBoxButton.OK);
                    DisplayMessage(e.SocketError.ToString(), "Error Getting Interface Information", MessageBoxButton.OK);
                // Close our socket since we no longer need it. 
                // The scope of this socket instance is this method. It is created every time the method is called. 
                // This was done purely to keep this method as self-contained as possible for this sample. 

    In the preceding method, the success of the connection request is determined by looking at the SocketError property on the SocketAsyncEventArgs. If successful, a call is made to the GetCurrentNetworkInterface extension method on the socket object. A simple text output is produced by combining the various property values from the NetworkInterfaceInfo object. Finally, a call is made to DisplayMessage, which is described in the following code.

  4. In the same code-behind page, add the following method.

             /// <summary>
            /// This helper method ensures that MessageBox.Show() is called on the UI thread.
            /// </summary>
            void DisplayMessage(string text, string caption, MessageBoxButton buttonConfiguration)
                Dispatcher.BeginInvoke(() =>
                    MessageBox.Show(text, caption, buttonConfiguration);
  5. After you build and run your solution, you should see a MessageBox like the following.

    Screenshot for How to: Get Connection Information

    In the preceding image, the value of Interface SubType is “unknown”. However, when connected to a cellular network, instead of being tethered or connected over Wi-Fi, this field displays the cellular network type, such as 2G and 3G.