Archive for the ‘.NET’ Category

What is Active and Passive FTP?

September 7, 2008

What is FTP?
File Transfer Protocol (FTP) is a network protocol used to transfer data from one computer to another through a network, such as the Internet. FTP is a file transfer protocol for exchanging and manipulating files over any TCP-based computer network. A FTP client may connect to a FTP server to manipulate files on that server. As there are many FTP client and server programs available for different operating systems, FTP is a popular choice for exchanging files independent of the operating systems involved.

What is Active and Passive FTP?
The Basics
FTP is a TCP based service exclusively. There is no UDP component to FTP. FTP is an unusual service in that it utilizes two ports, a ‘data’ port and a ‘command’ port (also known as the control port). Traditionally these are port 21 for the command port and port 20 for the data port. The confusion begins however, when we find that depending on the mode, the data port is not always on port 20.

Active FTP
In active mode FTP the client connects from a random unprivileged port (N > 1023) to the FTP server’s command port, port 21. Then, the client starts listening to port N+1 and sends the FTP command PORT N+1 to the FTP server. The server will then connect back to the client’s specified data port from its local data port, which is port 20.
From the server-side firewall’s standpoint, to support active mode FTP the following communication channels need to be opened:

  • FTP server’s port 21 from anywhere (Client initiates connection)
  • FTP server’s port 21 to ports > 1023 (Server responds to client’s control port)
  • FTP server’s port 20 to ports > 1023 (Server initiates data connection to client’s data port)
  • FTP server’s port 20 from ports > 1023 (Client sends ACKs to server’s data port)

When drawn out, the connection appears as follows:

In step 1, the client’s command port contacts the server’s command port and sends the command PORT 1027. The server then sends an ACK back to the client’s command port in step 2. In step 3 the server initiates a connection on its local data port to the data port the client specified earlier. Finally, the client sends an ACK back as shown in step 4.
The main problem with active mode FTP actually falls on the client side. The FTP client doesn’t make the actual connection to the data port of the server–it simply tells the server what port it is listening on and the server connects back to the specified port on the client. From the client side firewall this appears to be an outside system initiating a connection to an internal client–something that is usually blocked.

Passive FTP
In order to resolve the issue of the server initiating the connection to the client a different method for FTP connections was developed. This was known as passive mode, or PASV, after the command used by the client to tell the server it is in passive mode.
In passive mode FTP the client initiates both connections to the server, solving the problem of firewalls filtering the incoming data port connection to the client from the server. When opening an FTP connection, the client opens two random unprivileged ports locally (N > 1023 and N+1). The first port contacts the server on port 21, but instead of then issuing a PORT command and allowing the server to connect back to its data port, the client will issue the PASV command. The result of this is that the server then opens a random unprivileged port (P > 1023) and sends the PORT P command back to the client. The client then initiates the connection from port N+1 to port P on the server to transfer data.
From the server-side firewall’s standpoint, to support passive mode FTP the following communication channels need to be opened:

  • FTP server’s port 21 from anywhere (Client initiates connection)
  • FTP server’s port 21 to ports > 1023 (Server responds to client’s control port)
  • FTP server’s ports > 1023 from anywhere (Client initiates data connection to random port specified by server)
  • FTP server’s ports > 1023 to remote ports > 1023 (Server sends ACKs (and data) to client’s data port)

When drawn, a passive mode FTP connection looks like this:

In step 1, the client contacts the server on the command port and issues the PASV command. The server then replies in step 2 with PORT 2024, telling the client which port it is listening to for the data connection. In step 3 the client then initiates the data connection from its data port to the specified server data port. Finally, the server sends back an ACK in step 4 to the client’s data port.
While passive mode FTP solves many of the problems from the client side, it opens up a whole range of problems on the server side. The biggest issue is the need to allow any remote connection to high numbered ports on the server. Fortunately, many FTP daemons, including the popular WU-FTPD allow the administrator to specify a range of ports which the FTP server will use. The second issue involves supporting and troubleshooting clients which do (or do not) support passive mode. As an example, the command line FTP utility provided with Solaris does not support passive mode, necessitating a third-party FTP client, such as ncftp.


ASP.NET Page Life Cycle

August 7, 2008
When an ASP.NET page is requested by the client-browser, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. Moreover, for custom controls, understanding the page life cycle is very important in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code.

Page Life-cycle Stages
When a web page is requested it goes through a number of stages to get completely built. In addition to the page life-cycle stages, there are application stages that occur before and after a request to the page. Following are the stages under which a page goes before getting rendered to the client.

Page Request: The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled or whether a cached version of the page can be sent in response without running the page.

Start: In this step, the HTTP properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the
IsPostBack property. The page’s UICulture property is also set.

Page initialization: During page initialization, controls on the page are available and each control’s UniqueID property is set. Any themes are also applied to the page. For the page post backs the controls values are not set in this stage from the view state.

Load: During load stage, if the current request is a postback, control properties are loaded with information recovered from view state and control state.

Validation: The Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
Postback event handling: If the request is a postback, any relevant event handlers are called.

Rendering: Before rendering, view state is saved for the page and all controls. During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page’s Response property.

Unload: Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.

Life-cycle Events
At each stage of the page life cycle, the page raises certain events that can be used for custom coding. For control events, the event handlers must be bound to the enets. The following lists the page life-cycle events:
1. PreInit
2. Init
3. InitComplete
4. PreLoad
5. Load
6. Control events
7. LoadComplete
8. PreRender
9. SaveStateComplete
10. Render
11. Unload

How to send mail in C# ?

May 26, 2008

Often a c# programmer has to write code to send e-mails. This feature is required in many scenarios such as sending a mail once a scheduled job is over or sending a mail after the daily builds are done etc. Here is a sample code that can be used to send emails using C#.

using System.Net.Mail;

namespace EmailHelper
public static class EMail
public static void SendMail()
// Create mail message object
MailMessage mail = new MailMessage();
// Add the FROM address
mail.From = new MailAddress("");
// Add TO address
mail.To.Add(new MailAddress(""));
// Enter the subject
mail.Subject = "This is a test Mail.";
// Enter body of email
mail.Body = "This is the body of the test mail.";
// Put smtp server you will use
SmtpClient smtpServer = new SmtpClient("SMTP Host");
// Send the mail

In the above code we make use of System.Net.Mail namespace. In the SendMail method, we first create a mail message using the MailMessage class and then send it using the SmtpClient. A SMTP server is specified which is used to send mails.

Comments are Welcome !

How to secure .NET Remoting communication ?

May 26, 2008

Often we require securing the .NET remoting communication. Here is a fantastic article that will enable you to secure .net remoting communication using asymmetric encryption and decryption.


Java Language Conversion Assistant (JLCA)

April 7, 2008

The Java Language Conversion Assistant (JLCA) is a tool that automatically converts existing Java-language code into C#. This tool accepts Java files and converts them to equivalent C# files. It creates its own project for doing so. Also TODO’s are inserted into your C# code wherein the JLCA converter fails to find the equivalent C# code. Version 1.0 of this tool ships with Visual Studio .NET 2003 and can be installed during Setup. This tool is also available with VS 2005. Now it becomes very easy for the developers to migrate their existing Java application to Microsoft .NET framework.
More information on this tool can be obtained from:

Whats new in Visual Studio 2008 ????

March 24, 2008

Microsoft Visual Studio .NET 2008 has been released. Click here to find out Whats new in Visual Studio .NET 2008.

VB.NET To C#.NET Code Converter Tool

January 24, 2008

Following links provide a tool for code conversion from C# to VB.NET and viceversa:

Visual Studio 2008 Shortcuts !

January 15, 2008
Here are the shortcuts for Visual Studio 2008 !!!!

What is LINQ ?

December 19, 2007

LINQ is Language Integrated Query. LINQ is an integrated part of Orcas. LINQ has a great power of querying on any source of data, data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable interface. Microsoft basically divides LINQ into three areas and that are give below.

1. LINQ to Object {Queries performed against the in-memory data}
2. LINQ to ADO.Net
3. LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
4. LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
5. LINQ to Entities {Microsoft ORM solution}
6. LINQ to XML (formerly XLinq) { Queries performed against the XML source}

LINQ to XML (XLINQ): Sample

Sample.xml will contain the following:

<title>LINQ Sample</title>
<article>This is a sample XLinq source.</article>

C# code to query the xml:
//Load the xml into XDocument.
XDocument sample = XDocument.Load(“sample.xml”);
//Query the XML document.
XElement samplexElement = new XElement(“feed”,
from b in sample.Element(“feed”).Elements(“entry”)
select new XElement
//Display the XML.