Socket server problem

P

Pete Davis

I've written an async socket server app and I'm having an issue with the
EndReceive() call.

My begin receive call is:

sockState.RemoteSocket.BeginReceive(sockState.ReceiveBuffer, 0,
SocketState.BufferSize, 0, new AsyncCallback(this.ReadCallback), sockState);

The ReadCallback() method begins:

private void ReadCallback(IAsyncResult asyncResult)
{
Debug.WriteLine("SocketServer.ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.AsyncState;
int bytesRead = socketState.RemoteSocket.EndReceive(asyncResult);
Debug.WriteLine(String.Format("SocketServer.ReadCallback: End receive with
{0} bytes", bytesRead));

The first Debug.WriteLine() gets called when I connect, but EndReceive seems
to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 7451);
sock.Connect(ep);
HelloMsg hm = (HelloMsg)
SocketMessageFactory.CreateSocketMessage(SocketMsgType.HELLO);
sock.Send(hm.ToBuffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete
 
J

James

Pete Davis said:
I've written an async socket server app and I'm having an issue with the
EndReceive() call.

My begin receive call is:

sockState.RemoteSocket.BeginReceive(sockState.ReceiveBuffer, 0,
SocketState.BufferSize, 0, new AsyncCallback(this.ReadCallback), sockState);

The ReadCallback() method begins:

private void ReadCallback(IAsyncResult asyncResult)
{
Debug.WriteLine("SocketServer.ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.AsyncState;
int bytesRead = socketState.RemoteSocket.EndReceive(asyncResult);
Debug.WriteLine(String.Format("SocketServer.ReadCallback: End receive with
{0} bytes", bytesRead));

The first Debug.WriteLine() gets called when I connect, but EndReceive seems
to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 7451);
sock.Connect(ep);
HelloMsg hm = (HelloMsg)
SocketMessageFactory.CreateSocketMessage(SocketMsgType.HELLO);
sock.Send(hm.ToBuffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete

Are you using ManualResetEvents ?
 
P

Pete Davis

I have a manual reset event for the Accept (which is also async). Do I need
it for the receive as well?

Pete
 
P

Pete Davis

Ok, so that was the problem. I added a manual reset event for the receive
operation and it now works. Thanks a ton.

Now I have a new problem. One I suspected I might have and I'm confused on
this. According to the documentation:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/cpref/html/frlrfSystemNetSock
etsSocketClassEndReceiveTopic.htm

on EndReceive(), at least from looking at the sample code (which granted,
doesn't necessarily mean anything), it appears that by continuing to call
BeginReceive() and EndReceive(), I should eventually get an EndRevceive()
that returns 0 bytes at the end of my data.

That's not happening. In this sample case, I'm sending only a 16 byte
message. EndReceive() gets called once for the 16 byte message and then it
blocks the second time.

I need to know when a message has been completely sent. I could interpret
the data as it comes in and start keeping a tally of how much data has come
across (the first 2 ints in the message are a message type and a message
length). So based on the length, I should know how many bytes are waiting to
be received.

I'd rather not have to do this partial decoding of the data, but if I
have to, obviously I will.

Pete
 
J

James

Pete Davis said:
Ok, so that was the problem. I added a manual reset event for the receive
operation and it now works. Thanks a ton.

Now I have a new problem. One I suspected I might have and I'm confused on
this. According to the documentation:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/cpref/html/frlrfSystemNetSock
etsSocketClassEndReceiveTopic.htm

on EndReceive(), at least from looking at the sample code (which granted,
doesn't necessarily mean anything), it appears that by continuing to call
BeginReceive() and EndReceive(), I should eventually get an EndRevceive()
that returns 0 bytes at the end of my data.

That's not happening. In this sample case, I'm sending only a 16 byte
message. EndReceive() gets called once for the 16 byte message and then it
blocks the second time.

I need to know when a message has been completely sent. I could interpret
the data as it comes in and start keeping a tally of how much data has come
across (the first 2 ints in the message are a message type and a message
length). So based on the length, I should know how many bytes are waiting to
be received.

I'd rather not have to do this partial decoding of the data, but if I
have to, obviously I will.

Pete

Are you Setting the Receive Manual Reset Event after each Recieve? - You
shouldn't - only Set the MSR when bytes == 0

does this help?

JJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Socket Server Problems 4

Top