Reputation: 21
How can I send an array of bytes (byte[]
) over wireless?
I am currently working on a client/server chat application. I have implemented various other features e.g profile management but the problem I'm facing is trying to send an image over wireless.
I already know how to convert an image into byte format and saving/retrieving from a database, but when it comes to sending it in TCP/IP it doesn't work with the class am using (StreamWriter
).
What classes do I need to send the byte stream? Without interfering with his profile details?
(in my chat register form)
This is my algorithm: concatenate user information e.g his username, password, location, gender, status and embed server symbols e.g(%,^,~,`) so the server can break his information apart and save it in the data base.
Everything was working just fine until I tried concatenating the byte array (byte[]
i.e image data), with his information.
My solution: I will send his information separately i.e all strings will be together and his image will stand alone when I want to send his profile details to the server.
My classes:am using stream Writer to write user information to the server. please show me an example using a good class e.g (binary writer) to send his profile picture. If I can get the binary code on the server I would really appreciate it, thanks a lot.
Upvotes: 0
Views: 730
Reputation: 621
TcpClient
will work.
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, port);
Stream stream = tcpClient.GetStream();
byte[] testString = Encoding.ASCII.GetBytes("test");
stream.Write(testString, 0, testString.Length);
stream.Close();
tcpClient.Close();
That will send the string
"test". You can apply the same logic to different data formats.
byte[] hexMsg = new byte[] { 0x1A, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00 };
stream.Write(hexMsg, 0, hexMsg.Length);
That will send some hex
values across the stream.
Upvotes: 2