Click here to Skip to main content
15,391,859 members
Articles / Programming Languages / C#
Posted 1 Oct 2001


155 bookmarked

Introduction to TCP client server in C#

Rate me:
Please Sign up or sign in to vote.
4.73/5 (76 votes)
1 Oct 2001
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

Comments and Discussions

Question20 years later, this is still awesome Pin
Dan Klemer27-Feb-21 11:12
MemberDan Klemer27-Feb-21 11:12 
GeneralMy vote of 5 Pin
Member 1364508925-Jan-18 23:27
MemberMember 1364508925-Jan-18 23:27 
QuestionGood for absolute beginners. Very simplistic model. Not useful in practice. Pin
Member 429190623-Nov-17 1:34
MemberMember 429190623-Nov-17 1:34 
AnswerRe: Good for absolute beginners. Very simplistic model. Not useful in practice. Pin
Member 1364508925-Jan-18 22:23
MemberMember 1364508925-Jan-18 22:23 
GeneralRe: Good for absolute beginners. Very simplistic model. Not useful in practice. Pin
Member 42919064-Mar-18 21:51
MemberMember 42919064-Mar-18 21:51 
QuestionWhy you use the same IP address ? Pin
mariomoyeda10-Aug-17 10:22
Membermariomoyeda10-Aug-17 10:22 
QuestionError Pin
Reymar Ecat Escalante10-Mar-17 23:22
MemberReymar Ecat Escalante10-Mar-17 23:22 
BugException: Invalid Argument Supplied Pin
RHoward7810-Jun-15 10:22
MemberRHoward7810-Jun-15 10:22 
GeneralRe: Exception: Invalid Argument Supplied Pin
Member 141138469-Jan-19 22:30
MemberMember 141138469-Jan-19 22:30 
QuestionMultiple client support Pin
mapartha7-Dec-14 15:20
Membermapartha7-Dec-14 15:20 
AnswerRe: Multiple client support Pin
Member 429190623-Nov-17 1:27
MemberMember 429190623-Nov-17 1:27 
GeneralMy vote of 1 Pin
matthias Weiser19-Nov-14 1:53
Membermatthias Weiser19-Nov-14 1:53 
GeneralYour tutorial is excellent Pin
Member 103515933-Sep-14 23:17
MemberMember 103515933-Sep-14 23:17 
GeneralMy vote of 5 - Good way to get started! Pin
William Ivanski3-Jul-14 17:32
professionalWilliam Ivanski3-Jul-14 17:32 
QuestionCan this Client Code Communicate to Server written in C language ?? Pin
Member 1084085925-May-14 7:48
MemberMember 1084085925-May-14 7:48 
AnswerRe: Can this Client Code Communicate to Server written in C language ?? Pin
William Ivanski3-Jul-14 17:34
professionalWilliam Ivanski3-Jul-14 17:34 
GeneralRe: Can this Client Code Communicate to Server written in C language ?? Pin
markjuggles18-Feb-16 4:42
Membermarkjuggles18-Feb-16 4:42 
QuestionThanks for this. Pin
Member 1084178624-May-14 13:20
MemberMember 1084178624-May-14 13:20 
SuggestionVery useful article Pin
Perić Željko23-Mar-14 12:04
professionalPerić Željko23-Mar-14 12:04 
Questionexcellent pedagogical presentation of the essentials Pin
gclinkscode27-Jul-13 7:15
Membergclinkscode27-Jul-13 7:15 
QuestionI want to send a message to client which is run on different network. Can u pls help me.... Pin
suresh1110198317-May-13 5:09
Membersuresh1110198317-May-13 5: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 6:49
professionalTdmonkeypoop5-Feb-14 6:49 
QuestionStill refused connection Pin
ahmedMoheb21-Apr-13 12:38
MemberahmedMoheb21-Apr-13 12:38 
AnswerRe: Still refused connection Pin
Tdmonkeypoop30-Jan-14 3:01
professionalTdmonkeypoop30-Jan-14 3:01 
GeneralMy vote of 5 Pin
Schrodinger's Box29-Nov-12 4:43
MemberSchrodinger's Box29-Nov-12 4:43 

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.