Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Inter-Process Communication in .NET Using Named Pipes, Part 2

Rate me:
Please Sign up or sign in to vote.
4.77/5 (41 votes)
14 Jun 20044 min read 273.3K   4.2K   132  
This article explores a way of implementing Named Pipes based Inter-Process Communication between .NET applications
using System;
using System.IO;

using AppModule.InterProcessComm;

namespace AppModule.NamedPipes {
	#region Comments
	/// <summary>
	/// Used by client applications to communicate with server ones by using named pipes.
	/// </summary>
	#endregion
	public sealed class ClientPipeConnection : APipeConnection {
		#region Comments
		/// <summary>
		/// The network name of the server where the server pipe is created.
		/// </summary>
		/// <remarks>
		/// If "." is used as a server name then the pipe is connected to the local machine.
		/// </remarks>
		#endregion
		private string Server = ".";
		#region Comments
		/// <summary>
		/// Closes a client named pipe connection.
		/// </summary>
		/// <remarks>
		/// A client pipe connection is closed by closing the underlying pipe handle.
		/// </remarks>
		#endregion
		public override void Close() {
			CheckIfDisposed();
			NamedPipeWrapper.Close(this.Handle);
		}
		#region Comments
		/// <summary>
		/// Connects a client pipe to an existing server one.
		/// </summary>
		#endregion
		public override void Connect() {
			CheckIfDisposed();
			this.Handle = NamedPipeWrapper.ConnectToPipe(this.Name, this.Server);
		}
		#region Comments
		/// <summary>
		/// Attempts to establish a connection to the a server named pipe. 
		/// </summary>
		/// <remarks>
		/// If the attempt is successful the method creates the 
		/// <see cref="AppModule.NamedPipes.PipeHandle">PipeHandle</see> object
		/// and assigns it to the <see cref="AppModule.NamedPipes.APipeConnection.Handle">Handle</see>
		/// field.<br/><br/>
		/// This method is used when it is not known whether a server pipe already exists.
		/// </remarks>
		/// <returns>True if a connection is established.</returns>
		#endregion
		public bool TryConnect() {
			CheckIfDisposed();
			bool ReturnVal = NamedPipeWrapper.TryConnectToPipe(this.Name, this.Server, out this.Handle);

			return ReturnVal;
		}
		#region Comments
		/// <summary>
		/// Creates an instance of the ClientPipeConnection assuming that the server pipe
		/// is created on the same machine.
		/// </summary>
		/// <remarks>
		/// The maximum bytes to read from the client is set to be Int32.MaxValue.
		/// </remarks>
		/// <param name="name">The name of the server pipe.</param>
		#endregion
		public ClientPipeConnection(string name) {
			this.Name = name;
			this.Server = ".";
			this.maxReadBytes = Int32.MaxValue;
		}
		#region Comments
		/// <summary>
		/// Creates an instance of the ClientPipeConnection specifying the network name
		/// of the server.
		/// </summary>
		/// <remarks>
		/// The maximum bytes to read from the client is set to be Int32.MaxValue.
		/// </remarks>
		/// <param name="name">The name of the server pipe.</param>
		/// <param name="server">The network name of the machine, where the server pipe is created.</param>
		#endregion
		public ClientPipeConnection(string name, string server) {
			this.Name = name;
			this.Server = server;
			this.maxReadBytes = Int32.MaxValue;
		}
		#region Comments
		/// <summary>
		/// Object destructor.
		/// </summary>
		#endregion
		~ClientPipeConnection() {
			Dispose(false);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Ivan Latunov is a Software Architect with long term commercial experience in the areas of software architecture and engineering, design and development of web and distributed applications and business and technical analysis. Technical blog. Website: ivanweb.com.

Comments and Discussions