Exemplo n.º 1
0
        public byte[] seerMessageToBytes(SeerMessage msg)
        {
            MessageBox.Show(msg.length().ToString());
            byte[] bytes     = new byte[msg.length()];
            IntPtr structPtr = Marshal.AllocHGlobal(msg.length());

            Marshal.StructureToPtr(msg, structPtr, false);
            Marshal.Copy(structPtr, bytes, 0, msg.length());
            Marshal.FreeHGlobal(structPtr);
            return(bytes);
        }
Exemplo n.º 2
0
        private void button_send_Click(object sender, EventArgs e)
        {
            if (_tcpClient == null || !_tcpClient.Connected)
            {
                tcpConnet();
            }

            if (_tcpClient != null && _tcpClient.Connected)
            {
                try
                {
                    NetworkStream serverStream = _tcpClient.GetStream();

                    var tt = new SeerMessage();

                    tt.head = bytesToStructure <SeerMessageHead>(hexStrTobyte(textBox_req_head.Text.Trim()));
                    tt.data = normalStrToHexByte(textBox_req_data.Text.Trim());

                    serverStream.Write(seerMessageHeadToBytes(tt.head), 0, Marshal.SizeOf(tt.head));
                    serverStream.Write(tt.data, 0, tt.data.Length);
                    serverStream.Flush();

                    byte[] inStream = new byte[16];
                    int    tsize    = serverStream.Read(inStream, 0, 16);

                    var recv_head = bytesToStructure <SeerMessageHead>(inStream);



                    byte[] recvbyte = BitConverter.GetBytes(recv_head.length);

                    Array.Reverse(recvbyte);

                    var dsize = BitConverter.ToUInt32(recvbyte, 0);


                    byte[] dataStream = new byte[dsize];
                    int    asize      = serverStream.Read(dataStream, 0, (int)dsize);

                    //Console.WriteLine(BitConverter.ToString(seerMessageHeadToBytes(recv_head)));
                    textBox_recv_head.Text = BitConverter.ToString(seerMessageHeadToBytes(recv_head)).Replace("-", " ");//normalStrToHexStr(Encoding.UTF8.GetString(seerMessageHeadToBytes(recv_head)));

                    string str = Encoding.UTF8.GetString(dataStream);

                    textBox_recv_data.Invoke(new EventHandler(delegate { textBox_recv_data.Text = str; }));
                }
                catch
                {
                    textBox_recv_data.Invoke(new EventHandler(delegate { textBox_recv_data.Text = ""; }));
                    textBox_recv_head.Invoke(new EventHandler(delegate { textBox_recv_head.Text = ""; }));
                }
            }
        }
Exemplo n.º 3
0
        private void button_send_Click(object sender, EventArgs e)
        {
            textBox_recv_data.Invoke(new EventHandler(delegate { textBox_recv_data.Text = "loading..."; }));

            try
            {
                TcpClient client = new TcpClient(textBox_ip.Text.Trim(), Convert.ToInt32(textBox_port.Text.Trim()));
                if (client.Connected)
                {
                    NetworkStream serverStream = client.GetStream();

                    var newmsg = new SeerMessage();

                    newmsg.head = bytesToStructure <SeerMessageHead>(hexStrTobyte(textBox_req_head.Text.Trim()));
                    newmsg.data = normalStrToHexByte(textBox_req_data.Text.Trim());

                    serverStream.Write(seerMessageHeadToBytes(newmsg.head), 0, Marshal.SizeOf(newmsg.head));
                    serverStream.Write(newmsg.data, 0, newmsg.data.Length);
                    serverStream.Flush();

                    byte[] inStream = new byte[16];
                    while (16 != serverStream.Read(inStream, 0, 16))
                    {
                        Thread.Sleep(20);
                    }

                    var recv_head = bytesToStructure <SeerMessageHead>(inStream);

                    byte[] recvbyte = BitConverter.GetBytes(recv_head.length);

                    Array.Reverse(recvbyte);

                    var dsize = BitConverter.ToUInt32(recvbyte, 0);

                    const int   bufferSize = 512;
                    List <byte> datalist   = new List <byte>();
                    int         count      = 0;

                    while (true)
                    {
                        byte[] buffer   = new byte[bufferSize];
                        int    readSize = serverStream.Read(buffer, 0, bufferSize);

                        count += readSize;
                        datalist.AddRange(buffer);

                        if (count == dsize)
                        {
                            break;
                        }

                        Thread.Sleep(10);
                    }

                    textBox_recv_head.Text = BitConverter.ToString(seerMessageHeadToBytes(recv_head)).Replace("-", " ");//normalStrToHexStr(Encoding.UTF8.GetString(seerMessageHeadToBytes(recv_head)));

                    string str = System.Text.Encoding.UTF8.GetString(datalist.ToArray());

                    textBox_recv_data.Invoke(new EventHandler(delegate { textBox_recv_data.Text = str; }));

                    client.Close();
                }
            }
            catch (SocketException)
            {
                textBox_recv_data.Invoke(new EventHandler(delegate { textBox_recv_data.Text = ""; }));
                MessageBox.Show("Connect Error!");
            }
            catch (IOException)
            {
                textBox_recv_data.Invoke(new EventHandler(delegate { textBox_recv_data.Text = ""; }));
                MessageBox.Show("");
            }
        }