using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
// Reference from https://www.c-sharpcorner.com/article/socket-programming-in-C-Sharp/
// Client app is the one sending messages to a Server/listener.   
// Both listener and client can send messages back and forth once a communication is established.  
public class socketclient
{
    public static int Main(String[] args)
    {
        StartClient();
        return 0;
    }
    public static void StartClient()
    {
        byte[] bytes = new byte[1024];
        try
        {
            // Connect to a Remote server  
            // Get Host IP Address that is used to establish a connection  
            // In this case, we get one IP address of localhost that is IP : 127.0.0.1  
            // If a host has multiple addresses, you will get a list of addresses              
            //
            Console.WriteLine("IP Address = ip1.ip2.ip3.ip4");
            Console.Write("ip1 = ");
            byte ip1 = (byte)Int32.Parse(Console.ReadLine());
            Console.Write("ip2 = ");
            byte ip2 = (byte)Int32.Parse(Console.ReadLine());
            Console.Write("ip3 = ");
            byte ip3 = (byte)Int32.Parse(Console.ReadLine());
            Console.Write("ip4 = ");
            byte ip4 = (byte)Int32.Parse(Console.ReadLine());
            Console.Write("port no = ");
            int myport = Int32.Parse(Console.ReadLine());
            // IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });
            IPAddress ipAddress = new IPAddress(new byte[] { ip1, ip2, ip3, ip4 });            
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, myport); // 11000
            // Create a TCP/IP  socket.    
            Socket sender = new Socket(ipAddress.AddressFamily,
            SocketType.Stream, ProtocolType.Tcp);
            // Connect the socket to the remote endpoint. Catch any errors.    
            try
            {
                // Connect to Remote EndPoint  
                sender.Connect(remoteEP);
                Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
                // Encode the data string into a byte array.    
                byte[] msg = Encoding.ASCII.GetBytes("Hello world1!<EOF>");
                // Send the data through the socket.    
                int bytesSent = sender.Send(msg);
                // Receive the response from the remote device.    
                int bytesRec = sender.Receive(bytes);
                Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
                // Release the socket.    
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        Console.WriteLine("\n Press any key to exit");
        Console.ReadKey();
    }
}