I am working on a client/server application with packets ... that's have an id and packet length so I can receive and understand incoming or outing packets easy ... everything works as well.
But now I'm just trying to send an image from client to server ... I need to make it in packet as I do with others ... how can I do that ?
You need to serialize and deserialize data same as any information you send.
You need to break whatever you're sending down into its base data, send it to the client/server which then needs to process that data back into whatever type of data it needs to be. It's no different than structuring a packet really.
An image or any other type of file is just bytes stored on your computer. When you send an image (that is either stored physically as a file or in memory) you take those bytes and send. It's no different than sending any other type of data.
I would suggest using the UDP protocol for the socket that sends the image, if you're going to send multiple images fast over time. If it's once or just not a lot stick with TCP for safety.
Also to answer your last question.
Can't you really figure out how to do this?
Code:
socket.Send(ImageToByte(_image));
Suggestion send the size before. (Use 4 bytes for the size.)
okay guys i know how to send image and i can use this code
PHP Code:
System.Drawing.Bitmap myimg = new System.Drawing.Bitmap(OriginalImage);
System.Drawing.Image f = (System.Drawing.Image)myimg;
MemoryStream ms = new MemoryStream();
f.save(ms, System.Drawing.Imaging.ImageFormat.jpeg);
client.Send(ms.ToArray());
but now i want to make it as a packet class ... can anybody show me a simple image packet class ?
okay guys i know how to send image and i can use this code
PHP Code:
System.Drawing.Bitmap myimg = new System.Drawing.Bitmap(OriginalImage); System.Drawing.Image f = (System.Drawing.Image)myimg; MemoryStream ms = new MemoryStream(); f.save(ms, System.Drawing.Imaging.ImageFormat.jpeg); client.Send(ms.ToArray());
but now i want to make it as a packet class ... can anybody show me a simple image packet class ?
First of all why do you make a bitmap from an image and then converts the bitmap back to an image. Get rid of the bitmap.
Secondly you're not disposing any data. Image data (Bitmaps etc. always has to be disposed after usage, otherwise you'll end up with memory leaks.) and the same goes for streams in this case your memory stream.
Either you call Dispose() or you wrap them inside of a using statement.
Ex.
Code:
using (var ms = new MemoryStream()) {
// do shit with ms ....
}
First of all why do you make a bitmap from an image and then converts the bitmap back to an image. Get rid of the bitmap.
Secondly you're not disposing any data. Image data (Bitmaps etc. always has to be disposed after usage, otherwise you'll end up with memory leaks.) and the same goes for streams in this case your memory stream.
Either you call Dispose() or you wrap them inside of a using statement.
Ex.
Code:
using (var ms = new MemoryStream()) {
// do **** with ms ....
}
Thank you for your help.
I wish if you tell me how to send image in a packet class ... just like 1001 Conquer online packet which have packet id and packet length ... so we can control them easily. ..
I can create a packet classes and send them to server or client ... but with string, int, double .... etc ... but how to send image bytes instead of string or uint or anything else ... that's and I know the image size is bigger than maximum bytes of packet length in tcp servers ....
// Size is 6 + image bytes, because 4 for packet size and 2 for packet type
Packet.WriteUInt32((uint)imageBytes.Length + 6); // use 4 bytes for image size ...
Packet.WriteUInt16(PacketType.Image);
Packet.WriteBytes(imageBytes);
Just stop yourself, seriously. You obviously don't understand anything about the fundamentals of programming. And people, please, enough with the spoonfeeding.
// Size is 6 + image bytes, because 4 for packet size and 2 for packet type
Packet.WriteUInt32((uint)imageBytes.Length + 6); // use 4 bytes for image size ...
Packet.WriteUInt16(PacketType.Image);
Packet.WriteBytes(imageBytes);
thanks man, with your help now i can make this packet for the client :
PHP Code:
public class Image : Writer, Interfaces.IPacket
{
public byte[] Buffer;
public byte[] ImageBytes;
public Image()
{
}
public void Deserialize(byte[] buffer)
{
Buffer = buffer;
}
and i receive the packet 1003 in the server but with different lengths , so i don't switch lengths ... so how the image packet class will be in the server to get the only image bytes from the whole packet to reconvert the bytes to image again ?
is there a way or i have to trim first 6 bytes from the packet [ Header ] or what ?
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.
[VB] TCP Packet als Bytes senden 04/25/2014 - .NET Languages - 3 Replies Guten Abend,
ich versuche einem Server per TCP ein Packet für einen Request von meinen Accountdaten zu schicken, die benötigten Packets habe ich bereits gefiltert, analysiert und nachgebaut, einziges Problem ist, der Header des Packets, welches ich an den Server schicke ist ein Kauderwelsch wenn ich ihn als Ascii oder Dezimalzahl darstelle, das heißt er muss als ein Bytearray verschickt werden.
Mein Problem ist jetzt, dass die anderen Daten als String gespeichert werden und ich zwar die...
How to parse Image Code Packet? 03/17/2012 - SRO Coding Corner - 2 Replies My clientless get (opcode=2322) image code packet after login~
Does anyone know how to parse it and make it show on screen?
Searched through the forum but lacking of such information~
(Winsock hook) replace/filter packet bytes 11/18/2010 - General Coding - 3 Replies Hi guys
Please, can someone explain me or help me about my winsock hook?!
I need to change the first and second bytes of the packet I've received...
example:
I'm receiving the packet:
2 Bytes oder 4 Bytes ? 02/13/2010 - Kal Online - 3 Replies Erm wenn ich nach cooldowns schaue für Mockery mit der UCE
such ich dann mit 2 Bytes oder 4 Bytes ??
Interlude 11 bytes init packet wtf?? 02/07/2008 - Lineage 2 - 0 Replies Hello!
in some interlude servers, first packet(init) length is about 11 bytes, when im expecting a more huge packet(about 186 bytes) containing:
protocolVersion (Integer)
sessionId(Integer)
rsaPublicKey (byte)
blowfishKey (byte)
gg1 (byte)
gg2 (byte)
gg3 (byte)