Users Online

· Guests Online: 9

· Members Online: 1
Superadmin

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Establish Client Server Relationship

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.75 seconds
10,752,273 unique visits