Reputation: 175
I am trying to cast IAsyncResult.AsyncState to class StateObject. But it is giving a cast error since AsyncState is of type Socket. I need to get the byte data from the result. I have just started a Server project and i am not familiar with that.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
This is the OnReceiveHandlerClass
private void Receive(IAsyncResult result)
{
StateObject ss = (StateObject)result.AsyncState;
......
}
Thanks in advance..
Upvotes: 0
Views: 1780
Reputation: 175
I was getting the error because i was sending a different object in the BeginReceive method in case of sending StateObject
Correct method:
Socket.BeginReceive(StateObject.DataBuffer, 0, (int)StateObject.DataLength,
SocketFlags.None, new AsyncCallback(this.Receive),StateObject);
The last parameter was supposed to be of type StateObject and i send an object as i was not known what to send for that parameter.
Upvotes: 1