First of all we have to understand how Server and Client will connect each other. For creating these connections we use socket objects , from System.Net Library which comes from .Net Framework directly. Multiple clients can connect to the server at the same time but that is more complicated architecture for the beginning. So, we begin with single Server- single Client Architecture.

Now I’ll explain the entire code line by line. First we begin with server socket.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace Server_Client
{
    class Server
    {
        public static int port = 1300;
        //You can change port but make sure that the port is not being used.

        public static string ip = "127.0.0.1";
        //Local IP always equals 127.0.0.1 
        // or 'local'

        public static IPEndPoint IEP = new IPEndPoint(IPAddress.Parse(ip),port);
        //Now we create a new IPEndPoint object 
        //In fact There is no solid information what EndPoint is.
        //Because it's a bit abstract 
        //Whatever, it is enough to know that we need them for connections.

        public static Socket serversocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        //here we have created out server socket object so we can now start the socket and wait for
        //Connections (Client(s))
        //Most important things here are our SocketType must be Stream and ProtocolType TCP
        //We will analyze them in the following articles.

        static void Main(string[] args)
        {
            try
            {
                //Now it's time to start the server socket object.
                serversocket.Bind(IEP);

                serversocket.Listen(4);
                //Our server has started looking for connection requests from clients
                //The parameter here is the number of how many clients can be in the connection queue

                Socket Client = serversocket.Accept();
                //This operation will assign client which is connected, to our Client object.
                //So we can use it to send and receive data.

                //Now we have our client which is connected to the server succesfully
                byte[] message = new byte[1024];
                //Now we have to Encode our message to Bytes so we can send it
                //Because, you know, we send and receive messages as a byte array in network transmissions.
                int NumberOfBytesRecieved = Client.Receive(message);
                //This Receive() function will return us how many bytes we got.
                string messagestr = Encoding.UTF8.GetString(message, 0, NumberOfBytesRecieved);
                //For making the message more clear we encode it again 
                //But this time, from bytes to string

                Console.WriteLine(messagestr);
                //Here we print it.

                //Now we gonna send the message to the client so it can know if it is connected !
                //We use bytes again for communication
                message = Encoding.UTF8.GetBytes("Hello Client!");
                Client.Send(message);

                Console.ReadLine();
            }catch(SocketException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}

Client Socket

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace Client
{
    class Program
    {
        public static Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        public static string ip = "127.0.0.1";
        //You can find your Lan Ip with ipconfig command in command line (for windows) (IPV4)
        //But don't forget, it is your LAN ip (Local) so only the devices which are connected to your router can connect 
        //to your server. It is not global ip!

        public static int port = 1300;
        public static IPEndPoint IEP = new IPEndPoint(IPAddress.Parse(ip), port);

        

        static void Main(string[] args)
        {
            try
            {
                client.Connect(IEP);
                //It is not an asynchronous function so program will wait untill the client connects
                //Make sure that the server is running
                //Client Connected

                byte[] message = new byte[1024];
                message = Encoding.UTF8.GetBytes("Hello Server!");
                client.Send(message, SocketFlags.None);
                //It is not an asynchronous function so program will wait untill the client connects
                //So the server have to be waiting for messages (receive state)

                int numberofbytesreceived = client.Receive(message);
                string msgfromserver = Encoding.UTF8.GetString(message, 0, numberofbytesreceived);
                Console.WriteLine("Message From Server: " + msgfromserver);
                client.Close();
                Console.ReadLine();
            }
            catch(SocketException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}