An Echo Server and Client in C# Using TcpListener and TcpClient

1. If you have not already done so, download and install SharpDevelop. SharpDevelop is a free and open-source IDE for .NET. As of this writing, the latest version is available at the URL “http://www.icsharpcode.net/OpenSource/SD/Download/”.

2. Start SharpDevelop.

3. From SharpDevelop’s main menu bar, select the menu item “File – New – Solution”, or press “Control-Shift-N”.

4. In the tree control on the left side of the New Project dialog, click the “C# – Windows Applications” node, and click the “Console Application” icon that appears in the “Templates” box. Enter the text “TcpEchoClient” in the “Name” text box, then click the “Create” button. Control will return to the main SharpDevelop window, and the text of the “Program.cs” file will be visible in the text editor pane.

5. Replace the pre-generated text of Program.cs with the following and save:

// This code is adapted from a sample found at the URL 
// "http://blogs.msdn.com/b/jmanning/archive/2004/12/19/325699.aspx"

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace TcpEchoClient
{
	class TcpEchoClient
	{
		static void Main(string[] args)
		{			
			Console.WriteLine("Starting echo client...");

			int port = 1234;
			TcpClient client = new TcpClient("localhost", port);      				
			NetworkStream stream = client.GetStream();
			StreamReader reader = new StreamReader(stream);
			StreamWriter writer = new StreamWriter(stream) { AutoFlush = true };

			while (true)
			{
				Console.Write("Enter text to send: ");
				string lineToSend = Console.ReadLine();
				Console.WriteLine("Sending to server: " + lineToSend);
				writer.WriteLine(lineToSend);
				string lineReceived = reader.ReadLine();
				Console.WriteLine("Received from server: " + lineReceived);
			}
		}
	}
}

6. From the main menu bar, select the menu item “File – New – Project”, and follow the procedure given in a previous step to create another Console Application project, but name this project “TcpEchoServer”.

7. Replace the pre-generated text of the TcpEchoServer project’s Program.cs with the following and save:

// This code is adapted from a sample found at the URL 
// "http://blogs.msdn.com/b/jmanning/archive/2004/12/19/325699.aspx"

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace TcpEchoServer
{
    public class TcpEchoServer
    {
	public static void Main()
	{
		Console.WriteLine("Starting echo server...");

		int port = 1234;
		TcpListener listener = new TcpListener(IPAddress.Loopback, port);		
		listener.Start();

		TcpClient client = listener.AcceptTcpClient();
		NetworkStream stream = client.GetStream();
		StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true };
		StreamReader reader = new StreamReader(stream, Encoding.ASCII);

		while (true)
		{					
			string inputLine = "";
			while (inputLine != null)
			{
				inputLine = reader.ReadLine();
				writer.WriteLine("Echoing string: " + inputLine);
				Console.WriteLine("Echoing string: " + inputLine);
			}
			Console.WriteLine("Server saw disconnect from client.");
		}
	}
    }
}

8. In the Solution Explorer pane on one side of the SharpDevelop window, right-click the node for the TcpEchoServer project and select the item “Start without Debugging” from the menu that appears. A console window will be displayed, and the text “Starting echo server…” will appear within it.

9. Repeat the previous step to start the TcpEchoClient project. Another console window will appear, this one saying “Starting echo client…” and then prompting the user to “enter text to send:”.

10. Enter some text in the client console window and press enter. The server will echo that text back to the client.

This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

One Response to An Echo Server and Client in C# Using TcpListener and TcpClient

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s