C# Program to Establish Client Server Relationship

This C# Program Establishes Client Server Relationship. Here this explains as how a relationship is established between server and a client.

Here is source code of the C# Program to Establish Client Server Relationship. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Establish Client Server Relationship
  3.  */
  4. //SERVER PROGRAM
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. namespace Server336
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             try
  18.             {
  19.                 IPAddress ipAd = IPAddress.Parse("10.18.227.162");
  20.                 TcpListener myList = new TcpListener(ipAd, 8001);
  21.                 myList.Start();
  22.                 Console.WriteLine("The server is running at port 8001...");
  23.                 Console.WriteLine("The local End point is  :" + 
  24.                                    myList.LocalEndpoint);
  25.                 Console.WriteLine("Waiting for a connection.....");
  26.                 Socket s = myList.AcceptSocket();
  27.                 Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  28.                 byte[] b = new byte[100];
  29.                 int k = s.Receive(b);
  30.                 Console.WriteLine("Recieved...");
  31.                 for (int i = 0; i < k; i++)
  32.                 {
  33.                     Console.Write(Convert.ToChar(b[i]));
  34.                 }
  35.                 ASCIIEncoding asen = new ASCIIEncoding();
  36.                 s.Send(asen.GetBytes("The string was recieved by the server."));
  37.                 Console.WriteLine("\nSent Acknowledgement");
  38.                 s.Close();
  39.                 myList.Stop();
  40.             }
  41.             catch (Exception e)
  42.             {
  43.                 Console.WriteLine("Error..... " + e.StackTrace);
  44.             }
  45.             Console.ReadLine();
  46.         }
  47.     }
  48. }
  49.  
  50. //CLIENT PROGRAM
  51. using System;
  52. using System.Collections.Generic;
  53. using System.Linq;
  54. using System.Text;
  55. using System.IO;
  56. using System.Net;
  57. using System.Text;
  58. using System.Net.Sockets;
  59.  
  60. namespace Client336
  61. {
  62.     class Program
  63.     {
  64.         static void Main(string[] args)
  65.         {
  66.             try
  67.             {
  68.                 TcpClient tcpclnt = new TcpClient();
  69.                 Console.WriteLine("Connecting.....");
  70.                 tcpclnt.Connect("10.18.227.162", 8001);                
  71.                 Console.WriteLine("Connected");
  72.                 Console.Write("Enter the string to be transmitted : ");
  73.                 String str = Console.ReadLine();
  74.                 Stream stm = tcpclnt.GetStream();
  75.                 ASCIIEncoding asen = new ASCIIEncoding();
  76.                 byte[] ba = asen.GetBytes(str);
  77.                 Console.WriteLine("Transmitting.....");
  78.                 stm.Write(ba, 0, ba.Length);
  79.                 byte[] bb = new byte[100];
  80.                 int k = stm.Read(bb, 0, 100);
  81.                 for (int i = 0; i < k; i++)
  82.                     Console.Write(Convert.ToChar(bb[i]));
  83.                 tcpclnt.Close();
  84.                 Console.Read();
  85.             }
  86.             catch (Exception e)
  87.             {
  88.                 Console.WriteLine("Error..... " + e.StackTrace);
  89.             }
  90.         }
  91.     }
  92. }

Here is the output of the C# Program:
output
Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.