using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using CommonClassLib;
using System.Threading;

namespace SocketsTCPIP
{
    public static class CClientTCPIP
    {
        static Socket clientSocket;
        static string _tcpError;
        static bool _IFInit = false;
        static byte[] _getData=null;
        public static string TcpError
        {
            get { return _tcpError; }
            set { _tcpError = value; }
        }
        static bool InitTcpIP(EndPoint epServer, EndPoint epLocal)
        {
            try
            {
                //Using TCPIP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                //Bind Local Port
                clientSocket.Bind(epLocal);
                //Login to the server
                clientSocket.Connect(epServer);
                _IFInit = true;
                return true;
            }
            catch (Exception ex)
            {
                _tcpError = ex.Message;
                _IFInit = false;
                return false;

            }

        }
        public static bool SendTCPIP(string _remoteIP, int _remotePort,
            string _localIP, int _localPort, byte[] _sendData)
        {
            
            try
            {
                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(_remoteIP);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _remotePort);
                EndPoint epServer = (EndPoint)ipEndPoint;
                //IP address of the local machine
                IPAddress localAddress = IPAddress.Parse(_localIP);
                //Server is listening on port 1000
                IPEndPoint localEndPoint = new IPEndPoint(localAddress, _localPort);
                EndPoint epLocal = (EndPoint)localEndPoint;
                lock (clientSocket)
                {
                    if (_IFInit == false)
                    {
                        if (InitTcpIP(epServer,epLocal ) == false)
                        {
                            return false;
                        }
                    }

                clientSocket.Send(_sendData);
                //CCarryConvert.WriteDarkCasket("CClientTCPIP", "Send", _remoteIP + ":" + _remotePort, _sendData);

                }
                return true;

            }
            catch (Exception ex)
            {
                TcpError = ex.Message;
                return false;
            }
        }
        public static byte[] GetTCPIP(string _remoteIP, int _remotePort,
            string _localIP, int _localPort)
        {
            try
            {
                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(_remoteIP);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _remotePort);
                EndPoint epServer = (EndPoint)ipEndPoint;
                //IP address of the local machine
                IPAddress localAddress = IPAddress.Parse(_localIP);
                //Server is listening on port 1000
                IPEndPoint localEndPoint = new IPEndPoint(localAddress, _localPort);
                EndPoint epLocal = (EndPoint)localEndPoint;
                lock (clientSocket)
                {
                    if (_IFInit == false)
                    {
                        if (InitTcpIP(epServer, epLocal) == false)
                        {
                            return null;
                        }
                    }
                    clientSocket.Receive(_getData);
                }
                return _getData;

            }
            catch (Exception ex)
            {
                TcpError = ex.Message;
                return null;
            }

        }
        
    }
}
