Resources » .NET programming » .NET Framework

Client server sample using WCF and net.tcp binding

Updated: Category: .NET Framework
Author: Member Level: GoldPoints: 20


This sample demonstrates how to use WCF (Windows Communication Foundation) to develop a typical client server application using TCP/IP binding.
You may refer to several tutorials to get an introduction to WCF. The purpose of this article is to demonstrate a sample implementation of client
server application using WCF and TCP/IP binding.

About this WCF Sample

This sample is a typical client server application, which includes a client that need to login first before it can access other components on the server.

The client application uses a login screen where user can input user id and password. When you login, the user id and password will be validated by calling corresponding
services on the server. Once authenticated, an authentication token will be returned from the server. Client stores this token in a custom context on the client.

After authenticating, the authentication token is sent from the client to the server as a custom Header along with each request made to the server. Server
retrieves this token from the headers and validates against a custom database.

This sample includes 2 solutions:

1. WCF Host
2. WCF Client

As the name indicates, the first solution represents a windows applications which hosts the WCF server components.

The second solution represents the client application which communicates with the server.

The Server project has 2 important manager classes:

1. UserManager - This class provides the services to authenticate user on the server side using user id and password. Also, returns a custom token to the client so that client need to pass only this authentication for any service calls.
2. SomeOtherManager - This class represents any other services that you want to implement on the server side. In this WCF client server example, this manager class implements only one method - GetServerTime(). But this method validates to see if user is authenticated before returning the server time.


This class implements the interface IClientMessageInspector and is used to inspect the messages from the client side.
In our example, we are adding some custom headers to the request using this class. We are sending the authentication
token as part of the header to the server. Server retrieves this token and validates the calls.


This class implements the interface IDispatchMessageInspector and is used on the server side to inspect the incoming requests.


CustomBehavior class implements the interface IEndpointBehavior and it is used to add the custom message inspectors to the WCF system.

How it works ?

The client application first shows the login screen. If user successfully login, an Authentication token is returned by the server. The client stores
this token using a custom class called ClientContext. From this point onwards, the ClientmessageInspector class retrieves this token and
sends to the server as a custom header along with each request.

On the server side, service methods explicitly checks if the authentication token is passed in the header and verifies it is valid or not.


The purpose of this article is to provide a simple client server sample application that uses WCF with net.tcp binding. I have not explained how to use custom behaviours to
inspect the messages on the client and server side, even though I have used them here to inject the headers to the requests and responses.

I learned it from Paolo Pialorsi's blog:

Did you like this resource? Share it with your friends and show your love!

Responses to "Client server sample using WCF and net.tcp binding"
Author: Sajid P K  18 Jul 2008   Member Level: Gold   Points : 1
hii... wht is its main differnce from remoting. is it same as remoting ?

Author: beena  28 Jul 2008   Member Level: Silver   Points : 0

SharePoint2003 Development - Building
Author: Happy Coder  30 Jul 2008   Member Level: Gold   Points : 0
Good One

Author: Arun Kumar Pandey  07 Aug 2008   Member Level: Silver   Points : 1
WCF is not same as remoting.Remoting works only on Microsoft plateform.
WCF is a unification technology which unites the following technologies:-

.NET remoting
Web services

Author: avtar  18 Aug 2008   Member Level: Gold   Points : 2
WCF is more loosely coupled inshort in remoting your hosting that is low level protocol TCPIP and your logic are tightly coupled where as in WCF you write your logic once and host it as webservice , remote service ..etc

so if u see in earlier version if same logic u want to host as webservice over http and also as remote serive over tcpip , you have to re-write everything but in wcf its just write once and host it on any binding..

Author: gopi  09 Sep 2008   Member Level: Silver   Points : 1

gud one

but how to set " your resource to 1st one allways " in the list

Author: Gaurav Kumar Arora  18 Oct 2008   Member Level: Gold   Points : 1
Hi Tony,

A very good introductory Article for WCF. What is merely difference here from Remoting where using low-level protocols.

Could you please suggest a Book for the same topic.

Thanks & regards,
Gaurav Arora

Author: Pradeep Iyer  17 Nov 2008   Member Level: Gold   Points : 1
Hi Tony,

Excellent article.

Anyone could gain good knowledge on WCF through this.

Thanks a lot.

Pradeep Y

Author: sanjay kumar  11 Dec 2008   Member Level: Bronze   Points : 0
Very well described about wcf

Author: Shuby Arora  02 Feb 2009   Member Level: Gold   Points : 1

Excellent article.
We'll gain good knowledge through this.

keep smilling
Shuby arora

Author: forcrack  27 Feb 2009   Member Level: Bronze   Points : 2

Processing form data with PHP

Create WebSite

Add meta tag to blog website

Python Tutorial

databases in Perl

Using Databases In PHP

Boot Sector

Creating A Simple Search Engine In PHP

Assembly language Tutorial

Batch file programming

free Mircoprocessors Tutorial Training

C Socket Programming In Unix

Author: Akanksha srivastava  12 Mar 2009   Member Level: Bronze   Points : 0
Gud article.

Author: Uttam Dhakal  26 Mar 2009   Member Level: Silver   Points : 1
.net remoting morever can be thinked as application to application communication whereas WCF is a bundle of communication technology which supports not only application to application but also machine to machine

Author: Surya Kant  08 Apr 2009   Member Level: Silver   Points : 1
Go with following link:

Author: Dosth Mohammed  12 Apr 2009   Member Level: Silver   Points : 1
here you can get more step by step tutorial from microsoft

Author: aradhya  12 May 2009   Member Level: Silver   Points : 1
Nice Article i got lot a Good idea regarding WCF.
Productive stuff.

Author: Gaurav  13 May 2009   Member Level: Silver   Points : 0
Please explain the scenarios where WCF can be used effectively.

Author: Praveen Pula  26 May 2009   Member Level: Gold   Points : 0
Nice one

Author: Kirankumar Potnuru  08 Jun 2009   Member Level: Gold   Points : 1
Its Good.
Can you explain what is the diffrence among WCF ,webservice,windows service and remoting

Author: Prasoon Kumar  16 Jun 2009   Member Level: Gold   Points : 2
@ Kirankumar Potnuru :-
WebServices :- It can be defined as a service which can be

utilized by an Application.

You create a Webservice when you need to share same information

between different Applications which may be/may be not developed

same technology(therefore it gives Flexibility of developing

Application with the help of different Technology) and may be

running on Different Platforms.

One classic example is stock Web services,since Any Application

developed in dotnet/java/others may like to have this

information about a stock ,therefore these informations has be

developed like something which is sharable by any

Application.In this scenario we will like to develop this

as Webservice.

Remoting :-Remoting also serves the same purpose as Webservice

but it doesn't have flexibility of supporting heterogeneous

environment but only serves purpose in homogeneous


Author: Nathan  17 Jun 2009   Member Level: Gold   Points : 0
Nice article...

Author: Kirankumar Potnuru  18 Jun 2009   Member Level: Gold   Points : 0
Hi Prasoon Kumar ,
Thanks for giving valuble information

Author: Ramesh Sahu  03 Sep 2009   Member Level: Gold   Points : 0
Good For Develpope and testing.

Author: muthuraman  07 Sep 2009   Member Level: Gold   Points : 1
hi tony,
this is a wonderful article.
we will gain very good knowledge on WCF through this one.

with warm regards,

Author: nishithraj  24 Nov 2009   Member Level: Gold   Points : 1

Nice article.

One question.

How the platform independency is achieved in WCF. Whether the client & Server calls are through SOAP??

Author: sajjad ahmad  26 Nov 2009   Member Level: Bronze   Points : 1
Its a simple but very knowledgeful article on WCF.
Thanx buddy.

How to Host WCF service in IIS?

Author: satyendra kr. Pandit  02 Mar 2010   Member Level: Silver   Points : 0

Author: YuDi  03 Mar 2010   Member Level: Gold   Points : 0
hi tony
i dont know why we are using wcf sevices
we can use simplw window services instead of wcf services


Author: Naveen Kumar  17 Mar 2010   Member Level: Gold   Points : 2
WCF is more loosely coupled inshort in remoting your hosting that is low level protocol TCPIP and your logic are tightly coupled where as in WCF you write your logic once and host it as webservice , remote service..

so if u see in earlier version if same logic u want to host as webservice over http and also as remote serive over tcpip , you have to re-write everything but in wcf its just write once and host it on any binding..

Author: Raju Melveetilpurayil  24 May 2010   Member Level: Gold   Points : 2
Respected MVP,
After reading this article i dont understand how it works? i download that attachment file help me a lot to understand what is WCF am poor in .net Remoting, WSE and MSMQ. i just read that WCF is .net Remoting, WSE and MSMQ. can i implement Chat program using WCF?. if you read my comment please suggest me and i will search relevant article abt WCF.

Author: Vamshikrishna Reddy  24 Aug 2010   Member Level: Gold   Points : 0
Nice Article

Author: arunms  25 Aug 2010   Member Level: Gold   Points : 1
Thanks Tony...Nice article..

WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.

Author: praveen  09 Sep 2010   Member Level: Gold   Points : 0

Author: Ilia  15 Sep 2010   Member Level: Bronze   Points : 0
Works great thanks.

Author: mike  22 Nov 2010   Member Level: Bronze   Points : 1
Hi, Great post.

Been trying to add a Callback functionality to this example but new to WCF so was wondering if anyone could give a example using Tony John´s example on how to use a callback that would fire of say 2 minutes after the user logged in replying some simple text string ?.

Author: saritagupta  14 Feb 2011   Member Level: Bronze   Points : 1
Nice post :)..

In classic object or component- oriented programming model offered only single way for client to call a method. Client will issue a call, block while the call was in progress, and continue executing once the method returned.

WCF will support classical Request-Replay model, along with that it also supports One-Way call(call and forget operation) and callback(service to call back the client)

Three modes of communication between client and service are

Request- Replay

Author: PR Sydney  02 Jun 2013   Member Level: Bronze   Points : 5
Hi Tony, thanks for a wonderful article. I am learner and working on VB.NET. I converted your code into VB.NET using SharpDevelop but it doesn't work as it should.

I have now both versions (C# and VB.NET) of the code and below combinations of the code working/not-working (I get "You are not authenticated") in not working combination:

working: c# host and C# client
working: C# host and vb client
working: vb host and c# client
not working: vb host and vb client

can someone please have a look and suggest me what is wrong in code. I have spend 2 days and checked entire code many time but still no luck.

I don't know C# so want to use VB.NET.

thanks in advance.

Don't know how/where to attach the code, so could someone help and I will send the code by email.

Author: Laxmikant  12 Jun 2013   Member Level: Gold   Points : 4
Nice Article...
Just to add here ... NetTcpBinding is mostly used when you have clients only in network, netNamedPipeBinding is used when the clients are on the same machine.
wsHttpBinindg or basicHttpBinding is used if clients can call it through http protocols like website.
In real application you have to implement same service with multiple bindings.
Here is my article on handling multiple wcf services.


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
      Last 7 Daysmore...

      Awards & Gifts
      Talk to Webmaster Tony John

      Online Members

      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India