コード例 #1
0
        public void ClientServiceLoop()
        {
            try
            {
                IPHostEntry ipHostInfo = Dns.GetHostEntry(remote_addr);
                IPAddress   ipAddress;
                IPEndPoint  ipepRemote = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 100);

                if (remote_addr == "127.0.0.1")
                {
                    ipepRemote = new IPEndPoint(IPAddress.Parse(remote_addr), int.Parse(remote_port));
                }
                else
                {
                    ipAddress  = ipHostInfo.AddressList[0];
                    ipepRemote = new IPEndPoint(ipAddress, int.Parse(remote_port));
                }

                byte[]        buffer = new byte[2048];
                int           count  = 0;
                string        text   = "";
                ASCIIEncoding buf    = new ASCIIEncoding();

                text = "DXClusterClient - Connecting to " + remote_addr.ToString();
                buf.GetBytes(text, 0, text.Length, buffer, 0);
                ClusterForm.Invoke(new CrossThreadCallback(ClusterForm.CrossThreadCommand), 1, buffer, text.Length);
                ClusterClient.Connect(ipepRemote);

                if (ClusterClient.Connected)
                {
                    sw   = ClusterClient.GetStream();
                    sr   = ClusterClient.GetStream();
                    text = "DXClusterClient - Connected to " + remote_addr.ToString();
                    buf.GetBytes(text, 0, text.Length, buffer, 0);
                    ClusterForm.Invoke(new CrossThreadCallback(ClusterForm.CrossThreadCommand), 1, buffer, text.Length);
                }

                while (ClusterClient.Connected)
                {
                    count = sr.Read(buffer, 0, 2048);

                    if (count == 0)
                    {
                        ClusterClient.Close();
                    }
                    else
                    {
                        ClusterForm.Invoke(new CrossThreadCallback(ClusterForm.CrossThreadCommand), 0, buffer, count);
                    }
                }

                text = "DXClusterClient - Disconnected";
                buf  = new ASCIIEncoding();
                buf.GetBytes(text, 0, text.Length, buffer, 0);
                ClusterForm.Invoke(new CrossThreadCallback(ClusterForm.CrossThreadCommand), 1, buffer, text.Length);

                ClusterClient.Close();
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                byte[]        buffer = new byte[100];
                string        text   = "DXClusterClient - Disconnected";
                ASCIIEncoding buf    = new ASCIIEncoding();
                buf.GetBytes(text, 0, text.Length, buffer, 0);

                try
                {
                    ClusterForm.Invoke(new CrossThreadCallback(ClusterForm.CrossThreadCommand), 1, buffer, text.Length);
                }
                catch
                {
                }
            }
        }