Ejemplo n.º 1
0
        /// <summary>
        /// callback method
        /// </summary>
        /// <param name="ar"></param>
        private void TCPCallBack(IAsyncResult ar)
        {
            TCPIPClient client = (TCPIPClient)ar.AsyncState;

            if (client.NetWork.Connected)
            {
                try
                {
                    NetworkStream ns             = client.NetWork.GetStream();
                    byte[]        recdata        = new byte[ns.EndRead(ar)];
                    string        receivedstring = (new ASCIIEncoding().GetString(client.buffer));
                    receivedstring = receivedstring.Substring(0, recdata.Length);
                    if (recdata.Length > 0)
                    {
                        if (receivedstring.Contains("\r\n"))
                        {
                            string[] recdataArray = receivedstring.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                            for (int i = 0; i < recdataArray.Length; i++)
                            {
                                if (MessageReceived != null)
                                {
                                    MessageReceived.BeginInvoke(client.Name, Message.ConvertStringToMessage(recdataArray[i]), null, null);//async output data
                                }
                            }
                            ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
                        }
                        else
                        {
                            Array.Copy(client.buffer, recdata, recdata.Length);
                            if (MessageReceived != null)
                            {
                                MessageReceived.BeginInvoke(client.Name, Message.ConvertByteToMessage(recdata), null, null);//async output data
                            }
                            ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
                        }
                    }
                    else
                    {
                        client.Disconnect();
                        LogerHelper2.ToLog("client :" + client.Name + " is disconnect from server", 2);
                        client.isClientOnline = false;
                        CommonMethod.ShowClientOffLine(client.Name);
                        lstClient.Remove(client);
                    }
                }
                catch (Exception ex)
                {
                    client.Disconnect();
                    LogerHelper2.ToLog(ex.Message + "client :" + client.Name + " is disconnect from server", 3);
                    client.isClientOnline = false;
                    CommonMethod.ShowClientOffLine(client.Name);
                    lstClient.Remove(client);
                }
            }
        }
Ejemplo n.º 2
0
        public void ConnectToServer(string ip, int port)
        {
            //client.NetWork.

            try
            {
                LogerHelper2.ToLog(string.Format("Connecting to Server: {0}:{1}", ip, port), 3);
                client         = new TCPIPClient();
                client.NetWork = new TcpClient();
                client.NetWork.Connect(ip.Trim(), port);//connect to server

                client.SetName();
                client.NetWork.GetStream().BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
                IsConnected = true;

                //if (client.NetWork.Connected)
                //{
                //    ServerConnected(client, true);
                //}
                //   lstClient.Add(client);
                //    BindLstClient();
                TxCount = 0;
            }
            catch (Exception ex)
            {
                IsConnected = false;
                client.Disconnect();
                //ServerConnected(client, false);
                MessageBox.Show("不能连接到库房App,请检查库房App是否打开.", "程序启动");
                LogerHelper2.ToLog("connecting Exception:" + ex.Message, 3);
            }
        }
Ejemplo n.º 3
0
 public void DisconnectFromServer()
 {
     if (client.NetWork.Connected)
     {
         LogerHelper2.ToLog("Disconnecting from Server", 3);
         client.Disconnect();
         //ServerConnected(client, false);
         IsConnected = false;
     }
 }
Ejemplo n.º 4
0
 private bool Send(byte[] data, TCPIPClient client)
 {
     try
     {
         client.NetWork.GetStream().Write(data, 0, data.Length);
     }
     catch (Exception ex)
     {
         LogerHelper2.ToLog("methond TcpServer.Send catch exception :" + client.Name + ex.Message, 3);
         client.Disconnect();
         return(false);
     }
     return(true);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Callback
 /// </summary>
 /// <param name="ar"></param>
 private void TCPCallBack(IAsyncResult ar)
 {
     try
     {
         TCPIPClient client = (TCPIPClient)ar.AsyncState;
         if (client.NetWork.Connected)
         {
             NetworkStream ns      = client.NetWork.GetStream();
             byte[]        recdata = new byte[ns.EndRead(ar)];
             Array.Copy(client.buffer, recdata, recdata.Length);
             if (recdata.Length > 0)
             {
                 if (MessageReceived != null)
                 {
                     string[] temp = Encoding.ASCII.GetString(recdata).Split('\n');
                     foreach (string item in temp)
                     {
                         if (item != "")
                         {
                             MessageReceived.BeginInvoke(client.Name, Message.ConvertStringToMessage(item), null, null);//Async call back
                         }
                     }
                 }
                 ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
             }
             else
             {
                 IsConnected = false;
                 client.Disconnect();
                 //ServerConnected(client, false);
                 //    lstClient.Remove(client);
                 //    BindLstClient();
             }
         }
     }
     catch (Exception e)
     {
         LogerHelper2.ToLog("Exception in Connection TCPCallBack:\r\n" + e.Message, 3);
     }
 }