logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C# Program to Establish Client Server Relationship

Posted on October 21, 2013 by Manish
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  :" + 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. }

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.
« Prev Page - C# Program to Perform File Comparison
» Next Page - C# Program to Check whether the given Integer has an Alternate Pattern
« C# Program to Illustrate how User Authentication is Done
C++ Program to Compute Sum of elements of vector using for_each() Algorithm »

Deep Dive @ Sanfoundry:

  1. Javascript Questions and Answers
  2. C# Programming Examples on Delegates
  3. C Programming Examples on Set & String Problems & Algorithms
  4. C# Programming Examples on Strings
  5. C# Programming Examples on Functions
  6. C Programming Examples on Bitwise Operations
  7. C# Questions and Answers
  8. Java Questions and Answers
  9. C# Programming Examples on Files
  10. C# Programming Examples on Networking
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry