C# Networking

      

The .NET framework provides two namespaces, System.Net and System.Net.Sockets for network programming. The System.Net.Sockets.Socket is an important class from the System.Net.Sockets namespace. There are some other helper classes like IPEndPoint, IPADdress, SocketException etc, which can be used for Network programming. The .NET framework supports both synchronous and asynchronous communication between the client and server. There are different methods supporting for these two types of communication. A synchronous method is operating in blocking mode, in which the method waits until the operation is complete before it returns. But an asynchronous method is operating in non-blocking mode, where it returns immediately, possibly before the operation has completed.

Dns Class 

 

 

The System.net namespace provides this class, which can be used to creates and send queries to obtain information about the host server from the Internet Domain Name Service (DNS). Remember that in order to access DNS, the machine executing the query must be connected to a network. If the query is executed on a machine, that does not have access to a domain name server, a System.Net.SocketException is thrown. All the members of this class are static in nature. The important methods of this class are-









public static IPHostEntry GetHostByAddress(string address) Where address should be in a dotted-quad format like "202.87.40.193". This method returns an IPHostEntry instance containing the host information. If DNS server is not available, the method returns a SocketException.



public static string GetHostName() –



This method returns the DNS host name of the local machine.

public static IPHostEntry Resolve(string hostname) –



This method resolves a DNS host name or IP address to a IPHostEntry instance. The host name should be in a dotted-quad format like 127.0.01 or www.microsoft.com.

IPHostEntry Class

   

This is a container class for Internet host address information. This class makes no thread safety guarantees. The followings are the important members of this class. AddressList Property –



Gives an IPAddress array containing IP addresses that resolve to the host name.

Aliases Property –

Gives a string array containing DNS name that resolves to the IP addresses in AddressList property.



Example: net1.cs

IPEndPoint Class    

This class is a concrete derived class of the abstract class EndPoint. The IPEndPoint class represents a network end point as an IP address and a port number. There is couple of useful constructors in this class: IPEndPoint(long addresses, int port) IPEndPoint (IPAddress addr, int port) IPHostEntry IPHost = Dns.Resolve("www.c-sharp.com"); Console.WriteLine(IPHost.HostName); string []aliases = IPHost.Aliases; IPAddress[] addr = IPHost.AddressList; Console.WriteLine(addr[0]); EndPoint ep = new IPEndPoint(addr[0],80);

Socket Programming 

The steps for creating a simple synchronous client are as follows. – – – – –



Create a Socket instance. Connect the above socket instance to an end-point. Send or Receive information. Shutdown the socket. Close the socket

The Socket class provides a constructor for creating a Socket instance.



public Socket (AddressFamily af, ProtocolType pt, SocketType st) – – – – – – –

Where AddressFamily, ProtocolType and SocketType are the enumeration types declared inside the Socket class. The AddressFamily member specifies the addressing scheme that a socket instance must use to resolve an address. For example AddressFamily.InterNetwork indicates that an IP version 4 addresses is expected when a socket connects to an end point. The SocketType parameter specifies the socket type of the current instance. For example SocketType.Stream indicates a connection-oriented stream and SocketType.Dgram indicates a connectionless stream. The ProtocolType parameter specifies the ptotocol to be used for the communication. For example ProtocolType.Tcp indicates that the protocol used is TCP and ProtocolType.Udp indicates that the protocol using is UDP.



public Connect (EndPoint ep) – – –





The Connect() method is used by the local end-point to connect to the remote end-point. This method is used only in the client side. Once the connection has been established the Send() and Receive() methods can be used for sending and receiving the data across the network. We can use the Connected property of the Socket class to know whether the current Socket instance is connected or not. A property value of true indicates that the current Socket instance is connected.



IPHostEntry IPHost = Dns.Resolve("www.c-sharp.com"); Console.WriteLine(IPHost.HostName); string []aliases = IPHost.Aliases; IPAddress[] addr = IPHost.AddressList; Console.WriteLine(addr[0]); EndPoint ep = new IPEndPoint(addr[0],80); Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,Protoc olType.Tcp); sock.Connect(ep); if(sock.Connected) Console.WriteLine("OK");

 

The Send() method of the socket class can be used to send data to a connected remote socket. public int Send (byte[] buffer, int size, SocketFlags flags) –



Where byte[] parameter store the data to send to the socket, size parameter containing the number of bytes to send across the network. The SocketFlags parameter can be a bitwise combination of any one of the following values defined in the System.Net.Sockets.SocketFlags enumerator.



SocketFlags.None –



SocketFlags.DontRoute –



Indicates that the control data did not fit into an internal 64-KB buffer and was truncated.

SocketFlags.Broadcast –



The message was too large to fit into the specified buffer and was truncated.

SocketFlags.ControlDataTruncated –



Send without using routing tables.

SocketFlags.Truncated –



Use no flags for this call.

Indicates a broadcast packet.

SocketFlags.Multicast –

Indicates a multicast packet.

  

The method Send() returns a System.Int32 containing the number of bytes send. Other overloaded versions of Send() method as follows. public int Send (byte[] buffer, SocketFlags flags) public int Send (byte[] buffer) public int Send (byte[] buffer,int offset, int size, SocketFlags flags)

 

The Receive() method can be used to receive data from a socket. public int Receive(byte[] buffer, int size, SocketFlags flags) –



Where byte[] parameter storing the data to receive to the socket, size parameter containing the number of bytes to receive across the network. The SocketFlags parameter can be a bitwise combination of any one of the following values defined in the System.Net.Sockets.SocketFlags enumerator explained above.





The overloaded versions of Receive() methods are shown below. public int Receive (byte[] buffer, SocketFlags flags) public int Receive (byte[] buffer) public int Receive (byte[] buffer,int offset, int size, SocketFlags flags)





When the communication across the sockets is over, the connection between the sockets can be terminated by invoking the method ShutDown(). public void ShutDown(SocketShutdown how) – –





Where ‘how’ is one of the values defined in the SocketShutdown enumeration. The value SoketShutdown.Send means that the socket on the other end of the connection is notified that the current instance would not send any more data. The value SoketShutdown.Receive means that the socket on the other end of the connection is notified that the current instance will not receive any more data and The value SoketShutdown.Both means that both the action are not possible.



 

Remember that the ShutDown() method must be called before the Close() method to ensure that all pending data is sent or received. A socket can be closed by invoking the method Close(). public void Close() –



This method closes the current instance and releases all managed and un-managed resources allocated by the current instance. This method internally calls the Dispose() method with an argument of ‘true’ value, which frees both managed and unmanaged resources used by the current instance.



protected virtual void Dispose(bool) –



The above method closes the current instance and releases the un-managed resources allocated by the current instance and exceptionally release the managed resources also. An argument value of ‘true’ releases both managed and unmanaged resources and a value of ‘false’ releases only unmanaged resources.

 

Example: client.cs Example: server.cs

C# Networking

public static IPHostEntry. GetHostByAddress(string address). ○ Where address should be in a dotted-quad format like "202.87.40.193". ○ This method returns an IPHostEntry instance containing the host information. ○ If DNS server is not available, the method returns a SocketException.

48KB Sizes 1 Downloads 126 Views

Recommend Documents

Networking - DLINK.pdf
DCS-933L Wireless N Day & Night Cloud IP Camera with Build in WiFi Extender (My D-LINK) 235.00 1 ... Network Storage & NVR (Network Video Recorder).

Networking Technologies.pdf
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying.

Networking & More.pdf
There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps.

Networking Support.pdf
Loading… Page 1. Whoops! There was a problem loading more pages. Retrying... Networking Support.pdf. Networking Support.pdf. Open. Extract. Open with.

Linux Networking
Online editions .... 8.15 Using VNC for Remote Linux-to-Linux Administration. 252 ... 9.8 Revoking Certificates. 282 .... account management, cross-platform file and printer sharing, cross-platform user .... Learn to monitor basic system health,.

Networking Support.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Networking ...

Networking Router.pdf
For example, telephone system. Telephone call establishes the circuit from the originating. phone through the local switching office, to a remote switching office, ...

Networking University Placement
Summer 2014. To apply: http://careers.exponential-e.com. Interview Dates: ... experienced professional's playing a critical part in our business success. It will be ...

Networking
equipment. They allow people to work together, too. If you've ever used the Internet, you have used a computer network. Physical Media Various ways of connecting computers can be accomplished through physical media. The medium can be any type of tele

Networking University Placement
March 2014 (applications due by Sunday 2nd March) ... The Exponential-e Pre Sales team design network solutions based upon customer requirements.

Networking LINKSYS.pdf
Loading… Whoops! There was a problem loading more pages. Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Main menu. There was a problem previewing

Networking LINKSYS.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Networking LINKSYS.pdf. Networking LINKSYS.pdf. Open. Extract. Open with. Sign In. Main menu. Whoops! There

Chapter 25. Networking -
The server creates a server socket and, once a connection to a client is ... the server's Internet host name or IP address. ...... In such cases, it is best to use.