include image bytes into packet c#

01/15/2015 16:54 abdeen#16
Quote:
Originally Posted by Super Aids View Post
Well a 16 bit int length will only work with relative small images. If you want to use regular sized / a little big images you have to use a 32 bit int for the length of it.

Which means an uint for the length.

But I give up....
thanks any way for your help...

.................................................. .............................

i am working on client server app all and i need to send image from client to server so i made a packet class with the packet id, packet length and image bytes ... then i can send the class packet to server ... its fine and i receive the packet with the packet id but i need to get the image bytes from the packet to convert it back again to an image ..that i need to send image packet class from client to server and here is the code for client :

PHP Code:
public class Image WriterInterfaces.IPacket
{
public 
byte[] Buffer;
public 
byte[] ImageBytes;

public 
Image()
{
}

public 
void Deserialize(byte[] buffer)
{
  
Buffer buffer;
}

public 
byte[] ToArray()
{
  
Buffer = new byte[(ImageBytes) + 6];
  
Writer.WriteUInt16(10030 Buffer);
  
Writer.WriteUint16((ushort)(Buffer.Length), 2Buffer);
  
Writer.WriteBytes(ImageBytes4Buffer);
  return 
Buffer;
}

and here is writer class :

PHP Code:
public class Writer
{
    public static 
void WriteStringWithLength(string argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        
int till buffer.Length offset;
        
till Math.Min(arg.Lengthtill);
        
buffer[offset] = (byte)arg.Length;
        
offset++;
        
ushort i 0;
        while (
till)
        {
            
buffer[(ushort)(offset)] = (byte)arg[i];
            
= (ushort)(1);
        }
    }
    public static 
void WriteString(string argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        if (
buffer.Length >= offset arg.Length)
        {
            
unsafe
            
{
 
#if UNSAFE
                
fixed (byteBuffer buffer)
                {
                    
ushort i 0;
                    while (
arg.Length)
                    {
                        *((
byte*)(Buffer offset i)) = (byte)arg[i];
                        
i++;
                    }
                }
 
#else
                
ushort i 0;
                while (
arg.Length)
                {
                    
buffer[(ushort)(offset)] = (byte)arg[i];
                    
= (ushort)(1);
                }
  
#endif
            
}
        }
    }
    public static 
void WriteByte(byte argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        
buffer[offset] = arg;
    }
    public static 
void WriteBytes(byte[] argint offsetbyte[] buffer)
    {
        foreach (
byte arg2 in arg)
        {
            if (
buffer == null)
                return;
            if (
offset buffer.Length 1)
                return;
            
buffer[offset] = arg2;
        }
    }
    public static 
void WriteBoolean(bool argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        
WriteByte(arg == true ? (byte): (byte)0offsetbuffer);
    }
    public static 
void WriteUInt16(ushort argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        if (
buffer.Length >= offset sizeof(ushort))
        {
            
unsafe
            
{
  
#if UNSAFE
                
fixed (byteBuffer buffer)
                {
                    *((
ushort*)(Buffer offset)) = arg;
                }
 
#else
                
buffer[offset] = (byte)arg;
                
buffer[offset 1] = (byte)(arg >> 8);
 
#endif
            
}
        }
    }
    public static 
void WriteUInt32(uint argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        if (
buffer.Length >= offset sizeof(uint))
        {
            
unsafe
            
{
 
#if UNSAFE
                
fixed (byteBuffer buffer)
                {
                    *((
uint*)(Buffer offset)) = arg;
                }
 
#else
                
buffer[offset] = (byte)arg;
                
buffer[offset 1] = (byte)(arg >> 8);
                
buffer[offset 2] = (byte)(arg >> 16);
                
buffer[offset 3] = (byte)(arg >> 24);
 
#endif
            
}
        }
    }
    public static 
void WriteInt32(int argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        if (
buffer.Length >= offset sizeof(uint))
        {
            
unsafe
            
{
 
#if UNSAFE
                
fixed (byteBuffer buffer)
                {
                    *((
int*)(Buffer offset)) = arg;
                }
#else
                
buffer[offset] = (byte)(arg);
                
buffer[offset 1] = (byte)(arg >> 8);
                
buffer[offset 2] = (byte)(arg >> 16);
                
buffer[offset 3] = (byte)(arg >> 24);
#endif
            
}
        }
    }
    public static 
void WriteUInt64(ulong argint offsetbyte[] buffer)
    {
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        if (
buffer.Length >= offset sizeof(ulong))
        {
            
unsafe
            
{
#if UNSAFE
                
fixed (byteBuffer buffer)
                {
                    *((
ulong*)(Buffer offset)) = arg;
                }
#else
                
buffer[offset] = (byte)(arg);
                
buffer[offset 1] = (byte)(arg >> 8);
                
buffer[offset 2] = (byte)(arg >> 16);
                
buffer[offset 3] = (byte)(arg >> 24);
                
buffer[offset 4] = (byte)(arg >> 32);
                
buffer[offset 5] = (byte)(arg >> 40);
                
buffer[offset 6] = (byte)(arg >> 48);
                
buffer[offset 7] = (byte)(arg >> 56);
#endif
            
}
        }
    }
    public static 
void WriteStringList(List<stringargint offsetbyte[] buffer)
    {
        if (
arg == null)
            return;
        if (
buffer == null)
            return;
        if (
offset buffer.Length 1)
            return;
        
buffer[offset] = (byte)arg.Count;
        
offset++;
        foreach (
string str in arg)
        {
            
buffer[offset] = (byte)str.Length;
            
WriteString(stroffset 1buffer);
            
offset += str.Length 1;
        
}
}

and i am using this to get packet id and length in server :

PHP Code:
ushort PacketID BitConverter.UInt16(packet0);
ushort Length BitConverter.UInt16(packet2); 
PHP Code:
so i want to know how is the image packet class will go in server side ??

i am not sure of writeBites void in writer class ... and how to make it read specific image bytes to use this bytes to convert it to an image 
PHP Code:
        public static void WriteBytes(byte[] argint offsetbyte[] buffer)
    {
        foreach (
byte arg2 in arg)
        {
            if (
buffer == null)
                return;
            if (
offset buffer.Length 1)
                return;
            
buffer[offset] = arg2;
        }
    } 
01/15/2015 18:27 Super Aids#17
Unfortunately C# arrays aren't dynamic and cannot change size or append by default. You have to allocate a new array and copy the old data into it long with the new.

Look into Buffer.BlockCopy or memcpy.
01/15/2015 18:52 abdeen#18
Quote:
Originally Posted by Super Aids View Post
Unfortunately C# arrays aren't dynamic and cannot change size or append by default. You have to allocate a new array and copy the old data into it long with the new.

Look into Buffer.BlockCopy or memcpy.

any sample code ?

is the writeBytes void is valid ?
01/15/2015 19:07 Super Aids#19
No it's not and msdn.
01/15/2015 19:46 Best Coder 2014#20
Quote:
Originally Posted by Super Aids View Post
Unfortunately C# arrays aren't dynamic and cannot change size or append by default. You have to allocate a new array and copy the old data into it long with the new.
You could always use the [Only registered and activated users can see links. Click Here To Register...]
01/15/2015 20:14 Super Aids#21
Quote:
Originally Posted by Best Coder 2014 View Post
You could always use the [Only registered and activated users can see links. Click Here To Register...]
Resize just allocates a new array and calls Array.Copy(), which is essentially the same as what I said. I don't know about the performance of Array.Copy() vs Buffer.BlockCopy() vs memcpy().
01/15/2015 20:19 abdeen#22
i edited the write bytes to become like :

PHP Code:
public static void WriteBytes(byte[] argint offsetbyte[] buffer)
    {
        foreach (
byte arg2 in arg)
        {
            if (
buffer == null)
                return;
            if (
offset buffer.Length 1)
                return;
            
buffer[offset++] = arg2;
        }
    } 
is it valid ?

if so ... how to split packet in server when receive it and get only image bytes ?
01/15/2015 20:36 Super Aids#23
Jfc.
01/15/2015 20:53 abdeen#24
Quote:
Originally Posted by Super Aids View Post
Jfc.
Thanks