Ejemplo n.º 1
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 {
            }
        }
Ejemplo n.º 2
0
        public byte[] CombinePackets()
        {
            ByteArray[] packetBytes = new ByteArray[packets.Count];
            int         totalSize   = 0;

            for (int i = 0; i < packets.Count; i++)
            {
                packetBytes[i] = new ByteArray(ByteEncoder.StringToByteArray(packets[i].PacketString));
                totalSize     += packetBytes[i].Length() + GetPacketSegmentHeaderSize();
            }
            byte[] packet   = new byte[totalSize];
            int    position = 0;

            for (int i = 0; i < packetBytes.Length; i++)
            {
                // Add the size of the packet segment
                Array.Copy(ByteArray.IntToByteArray(packetBytes[i].Length()), 0, packet, position, 4);
                position += 4;
                // Add the packet data
                Array.Copy(packetBytes[i].ToArray(), 0, packet, position, packetBytes[i].Length());
                position += packetBytes[i].Length();
            }
            return(packet);
        }
Ejemplo n.º 3
0
 public static void SendData(IPacket packet, bool compress, bool encrypt)
 {
     SendData(ByteEncoder.StringToByteArray(packet.PacketString), compress, encrypt, false);
 }