Hi All,
I have used the available code snippet from microsoft for socket
communication.
But the following code throws an error mentioned above. Please check the
code and help me asap.
private static void ReceiveCallback(IAsyncResult IResult)
{
try
{
// Retrieve the state object and the client socket from the
asynchronous state
//object.
StateObject objState = (StateObject) IResult.AsyncState;
Socket socClient = objState.msocSocket;
// Read data from the remote device.
int intBytesRead = socClient.EndReceive(IResult);
if(intBytesRead > 0)
{
// There might be more data, so store the data received so far.
objState.msbdDataReceived.Append(Encoding.ASCII.GetString
(objState.mbytBuffer, 0, intBytesRead));
// Get the rest of the data.
socClient.BeginReceive(objState.mbytBuffer, 0,
StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallback), objState);
}
else
{
// All the data has arrived; put it in response.
if (objState.msbdDataReceived.Length > 1)
{
mstrResponse = objState.msbdDataReceived.ToString();
}
// Signal that all bytes have been received.
mevtReceiveDone.Set();
}
}
catch(Exception objException)
{
throw objException;
}
}
The problem is that BeginReceive works as long as the Server return data
i.e. for example: Server return A 1st time, B 2nd time and if there is no
more data for the third time and if BeginReceive method is called, it throws
request timeout error.
Thanks in advance.
faktujaa
|