示例#1
0
 private byte[] CreatePacketHeader(int packetSize, MessageType messageType)
 {
     byte[] array = new byte[GetHeaderSize()];
     array[0] = 255;
     ByteEncoder.IntToByteArray(packetSize).CopyTo(array, 1);
     array[5] = (byte)messageType;
     return(array);
 }
示例#2
0
        public void SendFile(string filePath)
        {
            try {
                if (socket.Connected)
                {
                    FileInfo      fileInfo  = new FileInfo(filePath);
                    NetworkStream ns        = new NetworkStream(socket);
                    byte[]        nameBytes = ByteEncoder.StringToByteArray(Path.GetFileName(filePath));
                    ByteArray     header    = new ByteArray(CreatePacketHeader(4 + nameBytes.Length + (int)fileInfo.Length, MessageType.FileTransfer));

                    FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                    int fullHeaderLength = header.Length() + 4 + nameBytes.Length;

                    int    len = (int)fileInfo.Length;
                    byte[] b   = new byte[fullHeaderLength];

                    header.ToArray().CopyTo(b, 0);
                    ByteEncoder.IntToByteArray(nameBytes.Length).CopyTo(b, header.Length());
                    nameBytes.CopyTo(b, header.Length() + 4);

                    int offset    = fullHeaderLength;
                    int bytesRead = 0;
                    ns.Write(b, 0, b.Length);
                    //socket.Send(b, 0, b.Length, SocketFlags.None);
                    b = new Byte[1024];
                    while (bytesRead < len)
                    {
                        bytesRead += file.Read(b, 0, (b.Length + bytesRead < len) ? b.Length : (len - bytesRead));
                        //socket.Send(b, 0, b.Length, SocketFlags.None);
                        //socket.Send(b, 0, b.Length, SocketFlags.None);
                        ns.Write(b, 0, b.Length);
                        if (offset != 0)
                        {
                            offset = 0;
                        }
                        b = new byte[1024];
                        file.Seek(bytesRead, SeekOrigin.Begin);
                    }
                    ns.Close();
                    ns.Dispose();
                }
            } catch {
            }
        }
示例#3
0
        private byte[] CreatePacketHeader(int packetSize, MessageType messageType, byte[] customHeader)
        {
            byte[] array = new byte[GetHeaderSize()];
            array[0] = 255;
            ByteEncoder.IntToByteArray(packetSize).CopyTo(array, 1);
            array[5] = (byte)messageType;
            int n = 6;

            for (int i = 0; i < customHeader.Length; i++)
            {
                if (n + i < array.Length)
                {
                    array[n + i] = customHeader[i];
                }
                else
                {
                    break;
                }
            }
            return(array);
        }