C# program to create a client program

Here, we are going to learn how to create a client program in C#?
Submitted by Nidhi, on November 02, 2020

Here, we will create a socket client program, that connects to the socket server, and then we can send or receive data in a network.


The source code to create a socket client program is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;

class SocketClient
    static void Main(string[] args)
            TcpClient client = new TcpClient();
            ASCIIEncoding encoding = new ASCIIEncoding();
            Stream stream;    
            string sendStr = "Hello";

            int recvLen = 0;
            byte[] sendByteArray;
            byte[] recvByteArray = new byte[256];

            client.Connect("", 15001);
            Console.WriteLine("Connected to server");
            stream = client.GetStream();

            sendByteArray = encoding.GetBytes(sendStr);
            stream.Write(sendByteArray, 0, sendByteArray.Length);
            Console.WriteLine("Data send...");

            recvLen = stream.Read(recvByteArray, 0, 100);
            for (int i = 0; i < recvLen; i++)



Connected to server
Data send...
data received
Press any key to continue . . .
C# program to create a client program


In the above program, we created a socket client program, that will connect to the socket server using the Hercules tool on localhost with port 15001, and then we can send and receive data over the network.

Here, we can replace the localhost IP address with a specific IP address.

C# Socket Programs »


What's New (MCQs)

Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.