C# Program to Establish Client Server Relationship

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  :" + myList.LocalEndpoint);
  24.                 Console.WriteLine("Waiting for a connection.....");
  25.                 Socket s = myList.AcceptSocket();
  26.                 Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  27.                 byte[] b = new byte[100];
  28.                 int k = s.Receive(b);
  29.                 Console.WriteLine("Recieved...");
  30.                 for (int i = 0; i < k; i++)
  31.                 {
  32.                     Console.Write(Convert.ToChar(b[i]));
  33.                 }
  34.                 ASCIIEncoding asen = new ASCIIEncoding();
  35.                 s.Send(asen.GetBytes("The string was recieved by the server."));
  36.                 Console.WriteLine("\nSent Acknowledgement");
  37.                 s.Close();
  38.                 myList.Stop();
  39.             }
  40.             catch (Exception e)
  41.             {
  42.                 Console.WriteLine("Error..... " + e.StackTrace);
  43.             }
  44.             Console.ReadLine();
  45.         }
  46.     }
  47. }
  48.  
  49. //CLIENT PROGRAM
  50. using System;
  51. using System.Collections.Generic;
  52. using System.Linq;
  53. using System.Text;
  54. using System.IO;
  55. using System.Net;
  56. using System.Text;
  57. using System.Net.Sockets;
  58.  
  59. namespace Client336
  60. {
  61.     class Program
  62.     {
  63.         static void Main(string[] args)
  64.         {
  65.             try
  66.             {
  67.                 TcpClient tcpclnt = new TcpClient();
  68.                 Console.WriteLine("Connecting.....");
  69.                 tcpclnt.Connect("10.18.227.162", 8001);                
  70.                 Console.WriteLine("Connected");
  71.                 Console.Write("Enter the string to be transmitted : ");
  72.                 String str = Console.ReadLine();
  73.                 Stream stm = tcpclnt.GetStream();
  74.                 ASCIIEncoding asen = new ASCIIEncoding();
  75.                 byte[] ba = asen.GetBytes(str);
  76.                 Console.WriteLine("Transmitting.....");
  77.                 stm.Write(ba, 0, ba.Length);
  78.                 byte[] bb = new byte[100];
  79.                 int k = stm.Read(bb, 0, 100);
  80.                 for (int i = 0; i < k; i++)
  81.                     Console.Write(Convert.ToChar(bb[i]));
  82.                 tcpclnt.Close();
  83.                 Console.Read();
  84.             }
  85.             catch (Exception e)
  86.             {
  87.                 Console.WriteLine("Error..... " + e.StackTrace);
  88.             }
  89.         }
  90.     }
  91. }

No comments:

Post a Comment