コード例 #1
0
        /*
          TimeoutObject.Reset();
                Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ipendpount = new IPEndPoint(IPAddress.Parse(info.Ip),int.Parse(info.Port));
                socketClient.BeginConnect(ipendpount, CallBackMethod,socketClient);
                //socketClient.Connect(IPAddress.Parse(IpAndport[i].Ip), int.Parse(IpAndport[i].Port));
                if (TimeoutObject.WaitOne(timeoutMSec, false)&&socketClient!=null)
                {
                    //MessageBox.Show("网络正常");
                    dict.Add(info.Ip, socketClient);
                    //------UI显示在线

                    // MessageBox.Show(IpAndport[i].Ip + "服务器" + "在线");
                    //开通讯线程
                    ApplyThread(OnLine, socketClient);
                    ShowIpStauts(info.Ip, OnLine);
                }
                else
                {
                    //MessageBox.Show("连接超时");
                    throw new SocketException();
                }

         */
        /// <summary>
        /// 连接一个服务器的方法
        /// </summary>
        /// <param name="objinfo">服务器信息对象</param>
        private void AloneConnect(object objinfo)
        {
            ClientInfo info = (ClientInfo)objinfo;
            try
            {
                Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                socketClient.Connect(IPAddress.Parse(info.Ip), int.Parse(info.Port));

                dict.Add(info.Ip, socketClient);

                ShowIpStauts(socketClient.RemoteEndPoint.ToString().Substring(0, socketClient.RemoteEndPoint.ToString().IndexOf(":")), OnLine);
                //开通讯线程
                ApplyThread(OnLine, socketClient);

            }
            catch (SocketException)
            {

                ShowIpStauts(info.Ip, DisConnection);
                //开重连
                ClientInfo Info = new ClientInfo(info.Ip, info.Port);
                ApplyThread(DisConnection, Info);

            }
        }
コード例 #2
0
        /// <summary>
        /// 开始向所有服务发送连接请求,即判断是否在线
        /// </summary>
        private void startconnect()
        {
            DataTable dt = helper.getDs("select * from MedicineInfo", "MedicineInfo").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ClientInfo info = new ClientInfo(dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString());
                List.Add(info);
            }

            pts = new ParameterizedThreadStart(client.connect);
            thradRecMsg = new Thread(pts);
            thradRecMsg.IsBackground = true;
            thradRecMsg.Start(List);
        }
コード例 #3
0
        //------------------------------------------------------  
        /// <summary>
        /// 接受来自服务器的信息
        /// </summary>
        /// <param name="Socketclient">服务器的socket对象</param>
        private void RecMsg(object Socketclient)
        {
            SocketInfo s = (SocketInfo)Socketclient;
            try
            {
               
                DateTime dt = DateTime.Now;
               
                while (s.Socket.Receive(s.Bufffer, 0, s.Bufffer.Length, SocketFlags.None) > 0)
                {
                    string command = Encoding.GetEncoding("GBK").GetString(s.Bufffer);
                    s.Command = command;
                    s.Bufffer = new byte[1024];
                    Qmessage.Enqueue(s);
                   
                  
                   
                   
                    
                }
            }
            catch (Exception ex)
            {
                if (FindIsExist(s.Ip) && !IsApplyRetry[s.Ip])
                {
                  
                    IsApplyRetry[s.Ip] = true;
                    ThreadPool[s.Ip].Abort();
                    ThreadPool.Remove(s.Ip);
                    dict[s.Ip].Socket.Close();
                    dict[s.Ip].Socket.Dispose();
                    dict.Remove(s.Ip);
                    ShowIpStauts(s.Ip, DisConnection);
                    MessageBox.Show(ex.ToString());
                    ParameterizedThreadStart pts = new ParameterizedThreadStart(Retry);
                    Thread thradRecMsg = new Thread(pts);
                    thradRecMsg.IsBackground = true;
                    ClientInfo info = new ClientInfo(s.Ip,s.Port);
                    thradRecMsg.Start(info);
                }
            }
           
           
            

        }