C#在GMTOOLS 发消息

 private void button1_Click(object sender, EventArgs e)
        {
            byte[] filebyte = transferPatchFile(@"c://Danny.doc");
            byte[] PackHead = BitConverter.GetBytes(filebyte.Length + 16);
            byte[] Query = System.Text.Encoding.Unicode.GetBytes("MODULE_UPLOAD_WORD");
            byte[] ModuleId = BitConverter.GetBytes(12);
            byte[] FileLength = BitConverter.GetBytes(filebyte.Length);
            int index = 0;

            byte[] buff = new byte[filebyte.Length+16];

            Array.Copy(PackHead,0, buff, 0, 4);
            index += 4;
            Array.Copy(Query,0, buff, index, 4);
            index += 4;
            Array.Copy(ModuleId,0, buff, index, 4);
            index += 4;
            Array.Copy(FileLength,0, buff, index, 4);
            index += 4;
            Array.Copy(filebyte,0, buff, index, filebyte.Length);
            send(buff);
        }

        public byte[] readFile(string filePath)
        {
            FileStream fs = null;
            BinaryReader br = null;
            byte[] dllbytes = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open);
                br = new BinaryReader(fs);

                int intLength = (int)fs.Length;
                dllbytes = br.ReadBytes(intLength);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                br.Close();
                fs.Close();
            }
            return dllbytes;
        }
        public byte[] transferPatchFile(string filePath)
        {
            return readFile(filePath);
        }
        /// <summary>
        /// 单线程用户发送消息
        /// </summary>
        /// <param name="msg">消息包</param>
        public void send(byte[] msg)
        {
            try
            {
                networkStream = new NetworkStream(socket);
                if (networkStream.CanWrite)
                {
                    networkStream.Write(msg, 0, msg.Length);
                }
            }
            catch (SocketException ex)
            {
                networkStream.Close();
            }

        }

你可能感兴趣的:(C#在GMTOOLS 发消息)