using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
//using CommonClassLib;
namespace SocketsTCPIPServer
{

    public class CServerTCPIP
    {
        Thread mythread;
        Socket socket;
        Socket AcceptSocket;
        public Socket ClientSocket;
        string _returnData;
        string _tcpServerError;
        byte[] _revMessage;

        public byte[] RevMessage
        {
            get { return _revMessage; }
            set { _revMessage = value; }
        }
        public string TcpServerError
        {
            get { return _tcpServerError; }
            set { _tcpServerError = value; }
        }
        public string ReturnData
        {
            get { return _returnData; }
            set { _returnData = value; }
        }
        public CServerTCPIP()
        {
            try
            {
                IPAddress ServerIp = GetServerIP();
                IPEndPoint iep = new IPEndPoint(ServerIp, 1000);
                socket = new
                Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                
                if (socket.Connected == false)
                    socket.Bind(iep);
                
            }
            catch (System.Exception er)
            {
                _tcpServerError= er.Message;
            }
        }
        
        public static IPAddress GetServerIP()
        {

            return IPAddress.Parse("172.18.136.252");
        }
        public void StartListen()
        {
            mythread = new Thread(new ThreadStart(BeginListen));
            mythread.Start();
        }
        public void EndListen()
        {
           
            mythread.Abort();
        }
        private void BeginListen()
        {
            byte[] byteMessage = new byte[12];
            
            while (true)
            {
                try
                {
                    socket.Listen(50);
                    if (AcceptSocket==null )
                    {
                        AcceptSocket = socket.Accept();
                        ClientSocket = AcceptSocket;
                    }
                    

                    AcceptSocket.Receive(byteMessage);
                    //_revMessage = byteMessage;
                    ////CCarryConvert.WriteDarkCasket(this.ToString(), "Receive", newSocket.RemoteEndPoint.ToString(), byteMessage); 
                    ///////////////////////////////////
                    ////处理接收到数据
                    DealWithReceive(byteMessage, AcceptSocket);
                    
                }
                catch (SocketException ex)
                {
                    AcceptSocket.Close();
                    AcceptSocket = null;
                    _tcpServerError= ex.Message ;
                }
            }
        }
        void DealWithReceive(byte[] byteMessage, Socket newSocket )
        {
            try
            {
                //拆分客户端byteMessage信息，处理后反馈应答给客户端
                byte[] rdata = new byte[6] ;
                int task = byteMessage[3]* 256 + byteMessage[2];
                if (byteMessage[0] == 72)
                {
                    rdata[0] = 82;
                    rdata[1] = 0;
                    rdata[2] = byteMessage[2];
                    rdata[3] = byteMessage[3];
                    rdata[4] = 0;
                    rdata[5] =0;
                    newSocket.Send(rdata);
                    Thread.Sleep(10);
                    rdata[0] = 71;
                    rdata[1] = 0;
                    rdata[2] = byteMessage[2];
                    rdata[3] = byteMessage[3];
                    if (byteMessage[8]==255)
                    {
                    
                        if(task==7586)
                        {
                            rdata[4] = 9;
                        }
                        if (task == 7574)
                        {
                            rdata[4] = 8;
                        }
                        if (task == 7580)
                        {
                            rdata[4] = 7;
                        }
                        if (task == 7592)
                        {
                            rdata[4] = 6;
                        }
                    }
                    else
                    {
                        rdata[4] = byteMessage[8];
                    }
                    rdata[5] = 0;
                    newSocket.Send(rdata);
                    Thread.Sleep(10);
                }

               
                //CCarryConvert.WriteDarkCasket(this.ToString(), "Send", newSocket.RemoteEndPoint.ToString(), rdata); 
                //newSocket.Shutdown(SocketShutdown.Both);
                //newSocket.Close();
                _returnData = Encoding.ASCII.GetString(byteMessage);
            }
            catch (System.Exception ex)
            {
                _tcpServerError = ex.Message;
            }
        }
    }
}
