Skip to content
Related Articles
Open in App
Not now

Related Articles

Establishing Client Server Relationship Through C#

Improve Article
Save Article
  • Last Updated : 19 Oct, 2022
Improve Article
Save Article

At the point when the server program is run, it will demonstrate at which IP it is running and the port it is paying attention to. Presently run the client program is run, in order to lay out an association with the server.

At the point when an association is laid out, the server will show the IP address and Port from where it has acknowledged the association, and the client will request the string which is to be sent to the server. The server on receipt of the string will show it, and send an affirmation which will be received by the client.

The client can be either run from a similar machine as the server or from an alternate machine. In the event that runs from an alternate machine, an organization association ought to exist between the machines running the server and client programs

Gather the server and client programs independently. Prior to gathering change the IP address in the two projects to match that of your machine.

Note: To get your IP address run ‘ipconfig’ from the order brief in Windows NT/2000 m/c’s.

 

Example 1: 

C#




// C# program for SERVER side establishing connection
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Servers
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("10.18.227.162");
                TcpListener myList = new TcpListener(ipAd, 8001);
                myList.Start();
                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);
                Console.WriteLine("Received...");
                for (int i = 0; i < k; i++)
                {
                    Console.Write(Convert.ToChar(b[i]));
                }
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("The string was received by the server."));
                Console.WriteLine("\nSent Acknowledgement");
                s.Close();
                myList.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
            Console.ReadLine();
        }
    }
}

C#




// C# program for CLIENT side connection
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;
  
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");
                tcpclnt.Connect("10.18.227.162", 8001);               
                Console.WriteLine("Connected");
                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);
                Console.WriteLine("Transmitting.....");
                stm.Write(ba, 0, ba.Length);
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(bb[i]));
                tcpclnt.Close();
                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
}

Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!