Пример #1
0
        public void DoSendRequest(object oData)
        {
            try
            {
                byte[] data = (byte[])oData;

                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer
                // connected to the same address as specified by the server, port
                // combination.
                TcpClient client = new TcpClient(LocalHost, QuPort);

                // Get a client stream for reading and writing.
                NetworkStream stream = client.GetStream();

                // Send the message to the connected TcpServer.
                stream.Write(data, 0, data.Length);

                string message = Encoding.ASCII.GetString(data, 0, data.Length);
                Message = string.Format("Sent: {0}", message);

                WindowUtils.DoEvents();
                Thread.Sleep(5000);

                // Receive the TcpServer.response.
                // Buffer to store the response bytes.
                data = new byte[256];

                // Read the first batch of the TcpServer response bytes.
                int bytes = stream.Read(data, 0, data.Length);

                if (MsgGetSystemState.Check4Reply(data, bytes))
                {
                    byte[] dataGetSystemState = new byte[13];
                    Buffer.BlockCopy(data, 0, dataGetSystemState, 0, 13);
                    MidiChannel = BitConverter.ToString(dataGetSystemState, 8, 1);
                    BoxId       = dataGetSystemState[9];
                    OSVersion   = string.Format("{0}.{1}", BitConverter.ToString(dataGetSystemState, 10, 1), BitConverter.ToString(dataGetSystemState, 11, 1));
                    Message     = string.Format("Received: {0}", ByteArrayToString(dataGetSystemState));

                    for (int i = 0; i < 4; i++)
                    {
                        byte[] dataMsgNrpn = new byte[4];
                        Buffer.BlockCopy(data, 13 + i * 4, dataMsgNrpn, 0, 4);
                        Message = string.Format("Received: {0}", ByteArrayToString(dataMsgNrpn));
                    }
                }
                else
                {
                    string responseData = Encoding.ASCII.GetString(data, 0, bytes);
                    Message = string.Format("Received: {0}", responseData);
                }
                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Message = string.Format("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Message = string.Format("SocketException: {0}", e);
            }
        }