Click here to Skip to main content
12,690,140 members (30,225 online)
Click here to Skip to main content
Add your own
alternative version


141 bookmarked

Introduction to TCP client server in C#

, 1 Oct 2001
Rate this:
Please Sign up or sign in to vote.
An article on TCP client server programming


This is a simple implementation of a TCP client server relationship.

To use

Compile the server and client programs separately. Before compiling change the IP address in both programs to match that of your machine (NOTE : to get your IP address  run 'ipconfig' from the command prompt in Windows NT/2000 m/c's)

When the server program is run, it will indicate at which IP it is running and the port it is listening to. Now run the client program is run , so as to establish a connection with the server.

When a connection is established the server will display the IP address and Port from where it has accepted the connection and client will ask for the string which is to be transmitted to the server.

The server on reciept of the string will display it, send an acknowledgement which will be recieved by the client.

The client can be either run from the same machine as the server or from a different machine. If run from a different machine then a network connection should exist between the machines running the server and client programs 

/*   Server Program    */
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class serv {
    public static void Main() {
    try {
        IPAddress ipAd = IPAddress.Parse("");
         // use local m/c IP address, and 
         // use the same in the client

/* Initializes the Listener */
        TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */        
        Console.WriteLine("The server is running at port 8001...");    
        Console.WriteLine("The local End point is  :" + 
                          myList.LocalEndpoint );
        Console.WriteLine("Waiting for a connection.....");
        Socket s=myList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
        byte[] b=new byte[100];
        int k=s.Receive(b);
        for (int i=0;i<k;i++)

        ASCIIEncoding asen=new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("\nSent Acknowledgement");
/* clean up */            
    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);


/*       Client Program      */

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

public class clnt {

    public static void Main() {
        try {
            TcpClient tcpclnt = new TcpClient();
            // use the ipaddress as in the server program
            Console.Write("Enter the string to be transmitted : ");
            String str=Console.ReadLine();
            Stream stm = tcpclnt.GetStream();
            ASCIIEncoding asen= new ASCIIEncoding();
            byte[] ba=asen.GetBytes(str);
            byte[] bb=new byte[100];
            int k=stm.Read(bb,0,100);
            for (int i=0;i<k;i++)
        catch (Exception e) {
            Console.WriteLine("Error..... " + e.StackTrace);




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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

BugException: Invalid Argument Supplied Pin
RHoward7810-Jun-15 11:22
memberRHoward7810-Jun-15 11:22 
QuestionMultiple client support Pin
mapartha7-Dec-14 16:20
membermapartha7-Dec-14 16:20 
GeneralMy vote of 1 Pin
matthias Weiser19-Nov-14 2:53
membermatthias Weiser19-Nov-14 2:53 
GeneralYour tutorial is excellent Pin
Member 103515934-Sep-14 0:17
memberMember 103515934-Sep-14 0:17 
GeneralMy vote of 5 - Good way to get started! Pin
William Ivanski3-Jul-14 18:32
memberWilliam Ivanski3-Jul-14 18:32 
QuestionCan this Client Code Communicate to Server written in C language ?? Pin
Member 1084085925-May-14 8:48
memberMember 1084085925-May-14 8:48 
AnswerRe: Can this Client Code Communicate to Server written in C language ?? Pin
William Ivanski3-Jul-14 18:34
memberWilliam Ivanski3-Jul-14 18:34 
GeneralRe: Can this Client Code Communicate to Server written in C language ?? Pin
markjuggles18-Feb-16 5:42
membermarkjuggles18-Feb-16 5:42 
Questionclient-server communication Pin
panqnik25-May-14 7:54
memberpanqnik25-May-14 7:54 
QuestionThanks for this. Pin
Member 1084178624-May-14 14:20
memberMember 1084178624-May-14 14:20 
SuggestionVery useful article Pin
Perić Željko23-Mar-14 13:04
memberPerić Željko23-Mar-14 13:04 
Questionexcellent pedagogical presentation of the essentials Pin
gclinkscode27-Jul-13 8:15
membergclinkscode27-Jul-13 8:15 
QuestionI want to send a message to client which is run on different network. Can u pls help me.... Pin
suresh1110198317-May-13 6:09
membersuresh1110198317-May-13 6:09 
AnswerRe: I want to send a message to client which is run on different network. Can u pls help me.... Pin
Tdmonkeypoop5-Feb-14 7:49
professionalTdmonkeypoop5-Feb-14 7:49 
QuestionStill refused connection Pin
ahmedMoheb21-Apr-13 13:38
memberahmedMoheb21-Apr-13 13:38 
AnswerRe: Still refused connection Pin
Tdmonkeypoop30-Jan-14 4:01
memberTdmonkeypoop30-Jan-14 4:01 
GeneralNice one Pin
bilal786994-Apr-13 8:36
memberbilal786994-Apr-13 8:36 
GeneralMy vote of 5 Pin
josemartins29-Nov-12 5:43
memberjosemartins29-Nov-12 5:43 
Questionclient server query Pin
akki2309915-Nov-12 2:31
memberakki2309915-Nov-12 2:31 
Questionclient server query Pin
akki2309914-Nov-12 10:20
memberakki2309914-Nov-12 10:20 
QuestionUnable to read data from the transport connection: An established connection was aborted by the software in your host machine Pin
Dipakm15-Oct-12 1:35
memberDipakm15-Oct-12 1:35 
GeneralMy vote of 3 Pin
Mihaescu Florin14-Jul-12 0:55
memberMihaescu Florin14-Jul-12 0:55 
GeneralMy vote of 4 Pin
Laxmikant_Yadav16-Feb-12 2:48
memberLaxmikant_Yadav16-Feb-12 2:48 
Questionproblem Pin
mostafa.elsadany12-Sep-11 17:56
membermostafa.elsadany12-Sep-11 17:56 
Generalgreetings Pin
samathu25-Feb-11 1:30
membersamathu25-Feb-11 1:30 
GeneralNewer or updated version Pin
jotorres26-Aug-10 5:26
memberjotorres26-Aug-10 5:26 
GeneralC# Protocol Builder Pin
Hassan Assalih18-Aug-10 23:23
memberHassan Assalih18-Aug-10 23:23 
GeneralRe: C# Protocol Builder Pin
svbrown19-Jan-11 8:53
membersvbrown19-Jan-11 8:53 
GeneralMy vote of 5 Pin
naim94a18-Jul-10 6:06
membernaim94a18-Jul-10 6:06 
GeneralTools to build Protocol Pin
Hassan Assalih14-May-10 22:14
memberHassan Assalih14-May-10 22:14 
GeneralLovely!!! Pin
Teenpacker8-Apr-09 9:57
memberTeenpacker8-Apr-09 9:57 
GeneralMessage Removed Pin
M-Dawg25-Jul-09 12:17
memberM-Dawg25-Jul-09 12:17 
GeneralRe: Lovely!!! Pin
tomtom198029-Jul-09 22:39
membertomtom198029-Jul-09 22:39 
GeneralRe: Lovely!!! Pin
shani_n715-May-10 8:18
membershani_n715-May-10 8:18 
GeneralRe: Lovely!!! Pin
olekskin26-Jan-11 7:57
memberolekskin26-Jan-11 7:57 
Generalconnection refused if I use local machine's network IP instead of '' Pin
Aatif Chaudhry4-Mar-09 1:38
memberAatif Chaudhry4-Mar-09 1:38 
GeneralRe: connection refused if I use local machine's network IP instead of '' Pin
jbo.s19-Nov-09 23:23
memberjbo.s19-Nov-09 23:23 
Generalbest simple C# server/client Pin
ericgoodchild16-Dec-08 6:48
memberericgoodchild16-Dec-08 6:48 
GeneralWow that was easy Pin
jchalfant20-Nov-08 10:15
memberjchalfant20-Nov-08 10:15 
GeneralRe: Wow that was easy Pin
Sp3cial2-Dec-08 16:08
memberSp3cial2-Dec-08 16:08 
QuestionReceiveTimeOut Pin
borbelyr29-Apr-08 1:52
memberborbelyr29-Apr-08 1:52 
Generalnamespace 'Net' does not exists Pin
rami_84a29-Feb-08 10:13
memberrami_84a29-Feb-08 10:13 
GeneralRe: namespace 'Net' does not exists Pin
Diamonddrake8-Oct-09 13:28
memberDiamonddrake8-Oct-09 13:28 
Generaltransfering file between client and server in C# Pin
metal_sheer15-Mar-06 5:48
membermetal_sheer15-Mar-06 5:48 
QuestionMultiple Connections? Pin
Rikaino16-Jul-05 22:35
memberRikaino16-Jul-05 22:35 
AnswerRe: Multiple Connections? Pin
Mavarok4-Jun-06 9:57
memberMavarok4-Jun-06 9:57 
GeneralHelp needed Pin
Md Saleem Navalur29-Mar-05 1:54
memberMd Saleem Navalur29-Mar-05 1:54 
GeneralRe : Help needed Pin
Anonymous4-Apr-05 10:08
sussAnonymous4-Apr-05 10:08 
GeneralRe: Help needed Pin
Anon E Moss10-Jun-05 12:43
sussAnon E Moss10-Jun-05 12:43 
GeneralPort 443 Pin
Dominic George30-Nov-03 16:49
memberDominic George30-Nov-03 16:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170117.1 | Last Updated 2 Oct 2001
Article Copyright 2001 by S.Thangaraju
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid