using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using CommonClassLib;
namespace SocketsTCPIP
{
    public static class CClientTCPIP
    {
        static int _initCount=0;
        static bool _ifInit = false;
        static string _remoteIP;
        static int _remotePort;
        static Socket clientSocket;
        static EndPoint epServer;
        static byte[] _byteData = new byte[16];
        static string _tcpError;

        public static string TcpError
        {
            get { return _tcpError; }
            set { _tcpError = value; }
        }
        public static byte[] ByteData
        {
            get { return _byteData; }
            set { _byteData = value; }
        }
        public static bool  InitClientTCPIP()
        {
            if (_initCount > 3)
            {
                _tcpError = "连接远程主机时，发生三次连接失败错误！请检查远程主机是否正常运行!重新启动本系统。";
                return false;
            }
            try
            {
                //Using TCPIP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);

                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(_remoteIP);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _remotePort);

                epServer = (EndPoint)ipEndPoint;
                //Login to the server
                clientSocket.Connect(epServer);
                _ifInit = true;
                return true;
            }
            catch (Exception ex)
            {
                if(_initCount>3)
                {
                    _tcpError = "连接远程主机时，发生三次连接失败错误！请检查远程主机是否正常运行!重新启动本系统。";
                    return false;
                }
                _initCount++;
                _ifInit = false;
                _tcpError = ex.Message;
                return false;

            }
        }
        public static byte[] ConnectTCPIP(byte[] byteData, string remoteip, int remoteport)
        {
            
            try
            {
                _remoteIP = remoteip;
                _remotePort = remoteport;
                if (_ifInit == false)
                {
                    if (InitClientTCPIP() == false)
                    {
                        return null;
                    }
                }
                clientSocket.Send(byteData);
                CCarryConvert.WriteDarkCasket("CClientTCPIP", "Send", remoteip + ":" + remoteport, byteData);
                clientSocket.Receive(_byteData);
                CCarryConvert.WriteDarkCasket("CClientTCPIP", "Receive", remoteip + ":" + remoteport, _byteData);
                return _byteData;
            }
            catch (Exception ex)
            {
                _tcpError= ex.Message ;
                return null;
            }
        }
        public static byte[] ConnectTCPIP(string strData, string remoteip, int remoteport)
        {
            
            try
            {
                _remoteIP = remoteip;
                _remotePort = remoteport;
                if (_ifInit == false)
                {
                    if (InitClientTCPIP() == false)
                    {
                        return null;
                    }
                }

                byte[] byteData;
                byteData = Encoding.UTF8.GetBytes(strData);
                clientSocket.Send(byteData);
                CCarryConvert.WriteDarkCasket("CClientTCPIP", "Send", remoteip + ":" + remoteport, byteData); 
                clientSocket.Receive(_byteData);
                CCarryConvert.WriteDarkCasket("CClientTCPIP", "Receive", remoteip + ":" + remoteport, _byteData); 
                return _byteData;
            }
            catch (Exception ex)
            {
                _tcpError= ex.Message ;
                return null;
            }
        }
        public static void CloseSockets()
        {
            clientSocket.Shutdown(SocketShutdown.Both);
            clientSocket.Close();
        }

    
    }
}
