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