예제 #1
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);


                bool wexit   = false;
                int  tmpcp   = 0;//数组位置指针
                int  tmpsize = 0;

                while (!wexit)
                {
                    tmpsize = BitConverter.ToInt32(byteData, tmpcp);
                    if ((tmpsize == 0) || (tmpsize > 100))
                    {
                        wexit = true;
                    }
                    else
                    {
                        byte[] tmpdata = new byte[tmpsize];
                        Array.Copy(byteData, tmpcp, tmpdata, 0, tmpsize);
                        tmpcp = tmpcp + tmpsize;

                        CNetData msgReceived = new CNetData(tmpdata);
                        if (_event_devnetdata != null)
                        {
                            _event_devnetdata(msgReceived);
                        }
                    }
                }



                //CNetData msgReceived = new CNetData(byteData);
                //if (_event_devnetdata != null)
                //{
                //    _event_devnetdata(msgReceived);
                //}

                byteData = new byte[1024];
                clientSocket.BeginReceive(byteData,
                                          0,
                                          byteData.Length,
                                          SocketFlags.None,
                                          new AsyncCallback(OnReceive),
                                          null);
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "CNetClient_OnReceive", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
예제 #2
0
 public void SendData(CNetData datasend)
 {
     byte[] b = datasend.ToByte();
     try
     {
         clientSocket.Send(b, 0, b.Length, SocketFlags.None);
     }
     catch
     {
     }
 }
예제 #3
0
        private void OnConnect(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndConnect(ar);

                //We are connected so we login into the server
                CNetData msgToSend = new CNetData();
                msgToSend.cmdCommand = Command.Login;
                msgToSend.intDevnum  = intDevnum;
                msgToSend.strZjbh    = null;

                byte[] b = msgToSend.ToByte();

                //Send the message to the server
                clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "CNetClient_OnConnect", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }