473,473 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to convert Byte[] to object expect Using the BinaryFormatter

Hi,

I have created Client/Server application. In that Client side Program
i have used Asynchronous Callback method for send and receive the
stream. In the Receive method I want to make the Buffer (i.e) byte[] to
an object. How can i do this except using BinaryFormatter.

I know what type of object i'm expecting. So is it possible to send
the expected object to ReceiveCallBack method? If it is poosible Then
there i can deserialize and get an object. But how to Pass the Expected
object to the ReceiveCallBack method.If anybody knows solution please
let me know.

private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.receiveSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer,0,StateObject.Buf ferSize,SocketFlags.None,new
AsyncCallback(ReceiveCallback),state);
receiveDone.WaitOne();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback( IAsyncResult ar )
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.receiveSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
//state.sb.Append(Encoding.ASCII.GetString(state.buf fer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.Buf ferSize,0,new
AsyncCallback(ReceiveCallback),state);
}
else
{
// All the data has arrived; put it in response.
if (state.buffer.Length > 1)
{
XmlSerializer xs = new XmlSerializer(state.buffer.GetType());
MemoryStream stream = new MemoryStream(state.buffer);
response = (object)xs.Deserialize(stream);
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Thanks,
Vinoth

Nov 17 '05 #1
0 4248

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
1
by: steve | last post by:
I need to get byte values from an object into a byte in order to write the bytes to file. An object (vSound) is passed to a function. From the Autos window I can see that the Value of vSound is...
7
by: Sergio Florez M. | last post by:
How can I convert a Bitmap to a byte array? I've searched all over and none of the code samples I've managed to find work. Actually, I just need to create a Byte array starting from an Image...
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
2
by: Nikolay Petrov | last post by:
How can I convert variable of type Object to Byte array? TIA
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
15
by: Buddy Home | last post by:
Hello, I'm trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us...
5
by: sklett | last post by:
I wasn't sure how to phrase the subject. Currently I'm storing a file as a byte in a database, when I want to work with the file I download the byte and write the data to a file on disk then...
2
by: Charming12 | last post by:
Hi All, I am stuck with a unique problem, I have a class whose object i need to convert to a byte to send it further. I am using microsoft's Binaryformatter to convert object to byte as per...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.