Srikanth Technologies

Socket programming using C#.NET

In this blog, I show how to create a simple server socket and client socket using C# in .NET. We build two applications - one is server socket which receives request from client and sends system date as a string, another is client socket which makes a request to server socket, receives data and displays it to user.

Building Server Socket

Follow the steps given below to create a server socket.
  1. Start Visual studio.Net. Select File -> New Project.Select Visual C# in project type and select Console Application under templates. Enter project name Server
  2. You are provided with Program.cs. Rename class Program to ServerSocket and filename Program.cs to ServerSocket.cs. However, note these steps are not mandatory.
  3. Write the following code in Main() function.
    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    
    namespace Server
    {
        class ServerSocket
        {
            static void Main(string[] args)
            {
                TcpListener l = new TcpListener(1000);  // listens on port number 1000
                l.Start();   // start listener
                
                Console.WriteLine("Server has started.Waiting for clients...");
                while (true)
                {
                    TcpClient c = l.AcceptTcpClient();   // wait for client to make request
                    NetworkStream  ns = c.GetStream ();  // access stream to send data to client
                    string st = DateTime.Now.ToLongDateString();   // get system date and convert it to string
                    byte [] buf = System.Text.Encoding.ASCII.GetBytes (st);  // convert string to an array of bytes
                    ns.Write(buf,0, st.Length );  // write to stream
                }
                
            } // Main()
        } // end of class
    }  // end of namespace
    
  4. Use Build->Build Solution. It creates Server.exe file in BIN\DEBUF folder of the project.
  5. Go to DEBUG folder in BIN folder of the project and double click on Server.exe to run server socket
  6. Server socket starts and waits for client requests.

Now, let us proceed to create client socket application.

Building Client Socket

Follow the steps given below to create a client socket, which makes a request to server and displays the data received from server.
  1. Start Visual studio.Net. Select File -> New Project.Select Visual C# in project type and select Console application under templates. Enter project name as Client.
  2. You are provided with Program.cs. Rename class Program to ClientSocket and filename Program.cs to ClientSocket.cs. However, note these steps are not mandatory.
  3. Write the following code in Main() function.
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Sockets;
    
    namespace Client
    {
        class ClientSocket
        {
            static void Main(string[] args)
            {
                // connect to server running on localhost at port no. 1000
                TcpClient c = new TcpClient("localhost", 1000);  
                NetworkStream ns = c.GetStream();  // get stream
                
                byte[] buf = new byte[100];   // create byte array to receive data
                ns.Read(buf, 0, 100);  // read data from stream into byte array
    
                string st = System.Text.Encoding.ASCII.GetString(buf);   // convert byte array to string
                Console.WriteLine(st);  
            }
        }
    }
    
  4. Use Build->Build Solution to build application. 
  5. Run the project using CTRL + F5. You must see today's date on the screen

Sockets are used to build distributed applications. But for large scale applications where you have to send a lot of data, sockets are too cumbersome.   A better option provided by  .NET is  .NET Remoting.  In future posts I will show how to create a simple distributed application using .NET Remoting.

Keep Learning,

Srikanth.

Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :

Comments