Sunday, July 17, 2011

Network programming in VB.NET and C#

The .NET framework provides an extensive set of functions to enable network programming using the objects in System.Net and System.Net.Socket namespaces. Let us look into some basic network concepts that we need to develop, say a chat program or a remote-signaling application in .NET.


[1] Sockets and Ports:

Socket is the communication agent on the physical network layer. When your computer talks to another one, socket is like a virtual device on your system that sends and receives the TCP packets. Port is like a channel on which packets relating to a specific program are being sent or received. When your program is listening for data on port number 2051, no other program can. In .NET framework, a socket is represented by the object System.Net.Sockets.Socket. However, the object TcpListener can also be used as an alternative to the Socket for listening to TCP packets.

[2] Addresses and Endpoints:

In order to talk to a remote computer, we need to know its IP-address. This is represented by System.Net.IPAddress. It contains special members to get some vital information. For eg, the IPAddress.Loopback gives the local loop-back address to communicate with the local host. This is usually 127.0.0.1.
An end-point is a combination of an ip-address and a port. As mentioned earlier, we need these two information about a remote computer to communicate with its listening socket. This is System.Net.IPEndpoint.

[3] The Dns namespace:

The namespace System.Dns is very important in .net. Without it you would be crippled to do even basic tasks like resolving a computer's host name to its ip-address. Below are some key static functions of this object:

Dns.GetHostEntry() - Resolves a given name or address to a specific host (returns IPHostEntry).

Dns.GetHostName() - Returns name of a host computer.

Dns.GetHostAddresses() - Returns all ip-addresses of a given host-name or ip-address.



Let us now look at some common network tasks to perform in VB.net or C#:

[1] Finding any computer's ip address: You can easily get the host-name given its ip-address by the GetHostEntry(), but how will you find the ip-address given its name? Here you have to check the array of addresses returned by the GetHostName(), as done in the below c# function that you can add in your class:

public IPAddress GetAddressFromHost(string hostName)
{
//IPAddress address = Dns.GetHostEntry(txtRemoteHost.Text).AddressList[0];
IPHostEntry host = Dns.GetHostEntry(hostName);
IPAddress[] addresses = Dns.GetHostAddresses(host.HostName);
IPAddress address;
if (addresses[0].ToString() == "127.0.0.1" && addresses.Length > 1)
{
address = addresses[1];
}
else
{
address = addresses[0];
}
return address;
}


[2] Sending a tcp message to another computer. All you need to know is the remote computer's host-name or ip-address and the port number on which it is listening to. You will have to make use of the Stream object to write your message:

public void SendMessage(string remoteHost, int remotePort, string command, string body)
{
try
{
//string hostName = Dns.GetHostEntry(remoteClient.EndPoint.Address).HostName;
string localHostName = Dns.GetHostEntry("localhost").HostName;
TcpClient client = new TcpClient(remoteHost,remotePort);
//TcpClient client = new TcpClient();
Stream s = client.GetStream();
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(localHostName + "#" + PortNo.ToString() + "#" + ServerName + "#" + command + "#" + body);
sw.Close();
s.Close();
client.Close();
}
catch
{
}
}

1 comment:

  1. Hi Prahlad, my name is henry, a graduate of computer science basically trying to specialise in windows and network programming using vb. Please i was surfing through the internet and i came incontact with your blog, please i need a mentor. Can you be my mentor. Thank you

    ReplyDelete