15.2 Determining Connection Client Information

 <  Day Day Up  >  

You want to determine information about a connecting client within a server application.


Technique

The Socket class contains several useful properties that allow you to determine information about a connecting client. One of the key pieces of information is the RemoteEndPoint property that returns an EndPoint object. The EndPoint class itself is abstract, so to glean useful information, cast the EndPoint to an IPEndPoint object. The IPEndPoint class contains the IP address and port number of the remote client. You can also use the LocalEndPoint property defined in the Socket class to access information regarding the server:

 
 Console.WriteLine( "Client End Point: IP: {0} Port: {1}",     ((IPEndPoint)s.RemoteEndPoint).Address.ToString(),     ((IPEndPoint)s.RemoteEndPoint).Port.ToString() ); Console.WriteLine( "Server End Point: IP: {0} Port: {1}",     ((IPEndPoint)s.LocalEndPoint).Address.ToString(),     ((IPEndPoint)s.LocalEndPoint).Port.ToString() ); 

The Socket class is designed to work with all types of network addressing schemes known as address families . The AddressFamily property returns a value specifying the addressing mode being used by the socket. In most cases, and because the Internet Protocol (IP) is the most widely used addressing method, the AddressFamily is AddressFamily.InterNetwork .

Listing 15.1 created a server using TCP as the communication protocol. Data is passed back and forth between the client and server in a streaming fashion. You can verify this information by accessing the ProtocolType and SocketType properties, which return TCP and stream values, respectively.

The last significant property is Connected . This Boolean value represents the state of the connection during the last data transmission. You should not use this property, however, as a method to detect whether the client computer is still connected because it's unreliable. Each time data is sent and received, the Connected property can be updated, but during the time in between, the property cannot be updated because there is no reliable method to make this determination.

Comments

The Socket class is an extremely versatile class that can support a wide variety of underlying protocols. One thing to note to avoid confusion is that even though the Socket class is able to support these different protocols, the protocols themselves must be installed and available on the operating system. The Socket class simply provides an abstracted API to those protocols.

You'll see properties being used throughout the .NET Framework simply for informational purposes. It's up to the developer to determine whether to use that information and, if so, in what way. The EndPoint information contained within the LocalEndPoint and RemoteEndPoint properties is a good example. While used within the Socket class itself, these properties can also serve certain purposes within your application. The RemoteEndPoint is a good example. This property contains the IP address and port of the connecting client. With this information, your server can create a configurable filtering mechanism to prevent any inherent security risks. One of the most popular server exploits is the denial-of-service (DoS) hack. It occurs when a group of remote computers repeatedly connects and sends data to a server in the hopes of overloading it so incoming connections cannot be made. Of course, this type of attack is usually limited to Web servers, but it is something you should keep in mind.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net