Sockets Beginreceive not working correctly.

D

David

Hello

I'm using Sockets to send a receive data. My issue is when I open up a
socket, I can send and receive data correctly the first try. Upon the
second try without closing anything, I send some data, but receive nothing.

I noticed as I step through the code, during the second receive, I execute
the client.BeginReceive but the debugger goes to the next line without going
into the callback function, as if that Client.BeginRecieve line isn't there.
No errors occur.

Anyone have any ideas, what that maybe. Would I have some something blocking
from the first receive?




Below is some of the sample code I'm using.


// Create the state object.
StateObject state = new StateObject();

try
{
Socket client = Socket();

// Begin receiving the data from the remote device.
// issue a reset on a reset event
Reset();
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new
AsyncCallback(tcpct_ReceiveCallback), state);

// wait for completion up to 5 seconds
WaitOne(5000, false);




tcpct_RecieveCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;

Socket client = Socket()

// Read data from the remote device.
int bytesRead = client.EndReceive(ar);

if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

// Get the rest of the data
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new
AsyncCallback(tcpct_ReceiveCallback), state);
}
 
D

David

I'm only sending the word "hello", the first time I step through the code, I
recieve the data, the second time, I don't. It seems like the line that has
the beginrecieve is ignored.


Peter Duniho said:
I'm using Sockets to send a receive data. My issue is when I open up a
socket, I can send and receive data correctly the first try. Upon the
second try without closing anything, I send some data, but receive
nothing.

There are two likely possibilities:

-- By the time you get to stepping through the code, all of the data
you sent has been sent. In which case, it will all have been received by
your first receive (assuming the buffer's big enough).

-- By the time you get to stepping through the code, the second part
of your data hasn't yet been sent.

In either case, there's no reason to expect your callback method to be
executed.
I noticed as I step through the code, during the second receive, I
execute
the client.BeginReceive but the debugger goes to the next line without
going
into the callback function, as if that Client.BeginRecieve line isn't
there.
No errors occur.

BeginReceive() would not normally execute the callback in any case. The
normal use case involves BeginReceive() returning immediately, and your
callback being executed at some later time, when there's actually data to
be received.
Anyone have any ideas, what that maybe. Would I have some something
blocking
from the first receive?

Without a concise-but-complete code sample, it's impossible to say for
sure why your expectations are incorrect. However, suffice to say that
the likelihood of it actually being the case that "Beginreceive [sic] not
working correctly" is practically non-existent. You are surely doing
something wrong.

By the way, one thing I can see from your code that you're definitely
doing wrong is using a new Encoding instance each time you receive more
data. You should be calling GetDecoder() and using the one Decoder you
get each time you receive bytes, so that it can correctly deal with
multi-byte characters that might have gotten split up during transmission.

Pete
 
D

David

I can send and receive the data fine the first time. I see the callback
fire, I assume it's the delegate functions that is actually acquiring the
data into the state object.

The second time through, I send some data again, but step right past the
BeginRecieve function, thus I never get into the callback, so the buffer is
still empty.

void StartRX()
{

StateObject state = new StateObject;
Socket client = new Socket();

//second time this never fires
client.BeginRecieve(state.buffer,0,StateObject.BufferSize, 0, new
AsyncCallback(tcp_RecieveCallback), state);


if (state.sb.Length < 1)
{
//no data
}
else { //display data to user }


tcp_RecieveCallback(IAsyncResult ar)
{
StateObject state = ( StateObject)ar.AsyncState;
Socket client = Socket();
int bytesread = client.EnfReceieve(ar);
if (bytesread > 0)
{
//more data, so append to the sb (stringbuilder)
state.sb.Append(Encoding.ASCII.Getstring(State.buffer, 0, bytesread));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new
AsyncCallback(tco_RecieveCallback). state);
}
}
 

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

Top