Visual Basic 1002005 [A Developers Notebook] [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual Basic 1002005 [A Developers Notebook] [Electronic resources] - نسخه متنی

شرکت رسانه او ریلی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







6.3. Get Information About a Network Connection


Some applications need to adjust how they
work based on whether a network connection is present. For example,
imagine a sales reporting tool that runs on the laptop of a traveling
sales manager. When the laptop is plugged into the network, the
application needs to run in a connected mode in
order to retrieve the information it needs, such as a list of
products, directly from a database or web service. When the laptop is
disconnected from the network, the application needs to gracefully
degrade to a disconnected mode that
disables certain features or falls back on slightly older data
that's stored in a local file. To make the decision
about which mode to use, an application needs a quick way to
determine the network status of the current computer. Thanks to the
new My.Computer.Network object, this task is easy.


Note: Need to find out if your computer's
currently online? With the My class, this test is just a simple
property away.




6.3.1. How do I do that?


The My.Computer.Network object provides a single
IsAvailable property
that allows you to determine if the current computer has a network
connection. The IsAvailable property returns
TRue as long as at least one of the configured
network interfaces is connected, and it serves as a quick-and-dirty
test to see if the computer is online. To try it out, enter the
following code in a console application:

If My.Computer.Network.IsAvailable Then
Console.WriteLine("You have a network interface.")
End If

If you want more information, you need to turn to the
System.Net and
System.Net.NetworkInformation namespaces, which
provide much more fine-grained detail. For example, to retrieve and
display the IP address for the current computer, you can use the

System.Net.Dns class by
entering this code:

' Retrieve the computer name.
Dim HostName As String = System.Net.Dns.GetHostName( )
Console.WriteLine("Host name: " & HostName)
' Get the IP address for this computer.
' Note that this code actually retrieves the first
' IP address in the list, because it assumes the
' computer only has one assigned IP address
' (which is the norm).
Console.WriteLine("IP: " & _
System.Net.Dns.GetHostByName(HostName).AddressList(0).ToString( ))

Here's the output you might see:

Host name: FARIAMAT
IP: 192.168.0.197

In addition, you can now retrieve even more detailed information
about your network connection that wasn't available
in previous versions of .NET. To do so, you need to use the new

System.Net.NetworkInformation.IPGlobalProperties
class, which represents network activity on a standard IP network.

The IPGlobalProperties class provides several
methods that allow you to retrieve different objects, each of which
provides statistics for a specific type of network activity. For
example, if you're interested in all the traffic
that flows over your network connection using TCP, you can call
IPGlobalProperties.GetTcpIPv4Statistics(). For most people, this is the most useful measurement of
the network. On the other hand, if you're using a
next-generation IPv6 network, you need to use
IPGlobalProperties.GetTcpIPv6Statistics(). Other methods exist
for monitoring traffic that uses the UPD or ICMP protocols.
Obviously, you'll need to know a little bit about
networking to get the best out of these methods.


Tip:
IP
(Internet Protocol) is the core building block of most networks and
the Internet. It uniquely identifies computers with a four-part IP
address, and allows you to send a basic packet from one machine to
another (without any frills like error correction, flow control, or
connection management). Many other networking protocols, such as
TCP (Transmission
Connection Protocol) are built on top of the IP infrastructure, and
still other protocols are built on top of TCP (e.g., HTTP, the
language of the Web). For more information about networking, refer to
a solid introduction such as
Internet
Core Protocols (O'Reilly).



The following code retrieves detailed statistics about the network
traffic. It assumes that you've imported the
System.Net.NetworkInformation namespace:

Dim Properties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties( )
Dim TcpStat As TcpStatistics
TcpStat = Properties.GetTcpIPv4Statistics( )
Console.WriteLine("TCP/IPv4 Statistics:")
Console.WriteLine("Minimum Transmission Timeout... : " & _
TcpStat.MinimumTransmissionTimeOut)
Console.WriteLine("Maximum Transmission Timeout... : " & _
TcpStat.MaximumTransmissionTimeOut)
Console.WriteLine("Connection Data:")
Console.WriteLine(" Current .................... : " & _
TcpStat.CurrentConnections)
Console.WriteLine(" Cumulative .................. : " & _
TcpStat.CumulativeConnections)
Console.WriteLine(" Initiated ................... : " & _
TcpStat.ConnectionsInitiated)
Console.WriteLine(" Accepted .................... : " & _
TcpStat.ConnectionsAccepted)
Console.WriteLine(" Failed Attempts ............. : " & _
TcpStat.FailedConnectionAttempts)
Console.WriteLine(" Reset ....................... : " & _
TcpStat.ResetConnections)
Console.WriteLine( )
Console.WriteLine("Segment Data:")
Console.WriteLine(" Received ................... : " & _
TcpStat.SegmentsReceived)
Console.WriteLine(" Sent ........................ : " & _
TcpStat.SegmentsSent)
Console.WriteLine(" Retransmitted ............... : " & _
TcpStat.SegmentsResent)

Here's the output you might see:

TCP/IPv4 Statistics:
Minimum Transmission Timeout... : 300
Maximum Transmission Timeout... : 120000
Connection Data:
Current .................... : 6
Cumulative .................. : 29
Initiated ................... : 10822


Note: Statistics are kept from the time the connection is
established. That means every time you disconnect or reboot your
computer, you reset the networking statistics.



  Accepted .................... : 41
Failed Attempts ............. : 187
Reset ....................... : 2271
Segment Data:
Received ................... : 334791
Sent ........................ : 263171
Retransmitted ............... : 617


6.3.2. What about...


...other connection problems, like a disconnected router, erratic
network, or a firewall that's blocking access to the
location you need? The network connection statistics
won't give you any information about the rest of the
network (although you can try to ping a machine elsewhere on the
network, as described in the previous lab, "Ping
Another Computer"). In other words, even when a
network connection is available there's no way to
make sure it's working. For that reason, whenever
you need to access a resource over the networkwhether
it's a web service, database, or application running
on another computeryou need to wrap your call in proper
exception-handling code.


6.3.3. Where can I learn more?


For more information on advanced network statistics, look up the
"IPGlobalProperties" index entry in
the MSDN help, or look for the "network information
sample" for a more sophisticated Windows Forms
application that monitors network activity.


/ 97