B
Bruce
Hi
I have a problem with the BeginReceive method in Socket class while
using it to receive responses from a server. I have reread my code
many times but cannot figure out why the delay is happening.
My scenario is this:
1. I send a request for user1 to a server that will trigger 3
responses. (I call Send() method below)
2. I get the first response (I dont care about next 2)
3. I send a request for the next user (user2). (I call Send() method
again)
4. I get the pending 2 responses for the first request.
5. I get the first response for user2
1 through 4 happen almost at the same time. But between 4 and 5, there
is always ~200 millisecs delay. (The server responds to all requests
immediately)
Now, if I wait for all 3 responses at 2. then everything works fine
and I get all 3 responses within ~10ms (expected). But in the above
scenario, BeginReceive does not trigger the callback for 5. for
atleast 200 milliseconds.
Is there a problem with the way I am using the socket? I think it is
because I am doing a send when server is trying to send something and
because of that, I am having this issue.
~~
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Linger, false);
void Send(Socket socket, byte[] requestBytes) {
socket.BeginSend(requestBytes, 0, requestBytes.Length,
SocketFlags.None,
delegate(IAsyncResult
ar)
{
socket.EndSend(ar);
},
null);
}
void Receive(Socket socket) {
// 1 response contains 10 bytes
socket.BeginReceive(dataBuffer, 0, 10, SocketFlags.None, new
AsyncCallback(Callback), socket)
}
void Callback(IAsyncResult ar) {
/// process response
Receive(socket);
}
I have a problem with the BeginReceive method in Socket class while
using it to receive responses from a server. I have reread my code
many times but cannot figure out why the delay is happening.
My scenario is this:
1. I send a request for user1 to a server that will trigger 3
responses. (I call Send() method below)
2. I get the first response (I dont care about next 2)
3. I send a request for the next user (user2). (I call Send() method
again)
4. I get the pending 2 responses for the first request.
5. I get the first response for user2
1 through 4 happen almost at the same time. But between 4 and 5, there
is always ~200 millisecs delay. (The server responds to all requests
immediately)
Now, if I wait for all 3 responses at 2. then everything works fine
and I get all 3 responses within ~10ms (expected). But in the above
scenario, BeginReceive does not trigger the callback for 5. for
atleast 200 milliseconds.
Is there a problem with the way I am using the socket? I think it is
because I am doing a send when server is trying to send something and
because of that, I am having this issue.
~~
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Linger, false);
void Send(Socket socket, byte[] requestBytes) {
socket.BeginSend(requestBytes, 0, requestBytes.Length,
SocketFlags.None,
delegate(IAsyncResult
ar)
{
socket.EndSend(ar);
},
null);
}
void Receive(Socket socket) {
// 1 response contains 10 bytes
socket.BeginReceive(dataBuffer, 0, 10, SocketFlags.None, new
AsyncCallback(Callback), socket)
}
void Callback(IAsyncResult ar) {
/// process response
Receive(socket);
}