By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,418 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,418 IT Pros & Developers. It's quick & easy.

Socket server problem

P: n/a
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.Rece iveBuffer, 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.ReadCa llback: 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(SocketMsg Type.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

--
http://www.petedavis.net
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Pete Davis" <pd******@hotmail.com> wrote in message
news:1c******************************@news.meganet news.com...
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.Rece iveBuffer, 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.ReadCa llback: 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(SocketMsg Type.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

--
http://www.petedavis.net


Are you using ManualResetEvents ?
Nov 15 '05 #2

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

Pete

--
http://www.petedavis.net

"James" <James@KimberleyREMOVE_MEdawn.co.uk> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...

"Pete Davis" <pd******@hotmail.com> wrote in message
news:1c******************************@news.meganet news.com...
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.Rece iveBuffer, 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.ReadCa llback: 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(SocketMsg Type.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

--
http://www.petedavis.net


Are you using ManualResetEvents ?

Nov 15 '05 #3

P: n/a
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

--
http://www.petedavis.net

"James" <James@KimberleyREMOVE_MEdawn.co.uk> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...

"Pete Davis" <pd******@hotmail.com> wrote in message
news:1c******************************@news.meganet news.com...
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.Rece iveBuffer, 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.ReadCa llback: 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(SocketMsg Type.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

--
http://www.petedavis.net


Are you using ManualResetEvents ?

Nov 15 '05 #4

P: n/a

"Pete Davis" <pd******@hotmail.com> wrote in message
news:11******************************@news.meganet news.com...
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

--
http://www.petedavis.net

"James" <James@KimberleyREMOVE_MEdawn.co.uk> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...

"Pete Davis" <pd******@hotmail.com> wrote in message
news:1c******************************@news.meganet news.com...
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.Rece iveBuffer, 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.ReadCa llback: 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(SocketMsg Type.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

--
http://www.petedavis.net


Are you using ManualResetEvents ?



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
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.