# [C#]BeginReceive and EndReceive?



## warniahs (Dec 16, 2002)

I am kind of confused about this, could someone help me?

private void sock_Received(IAsyncResult iar)
{
int dataLen = 0;
byte[] tempBuffer = new byte[0];
try 
{
dataLen = sock.EndReceive(iar);
// copy the buffer, recreate, and resume receiving.
tempBuffer = new byte[dataLen];
Array.Copy(buffer, 0, tempBuffer, 0, dataLen);
buffer = new byte[BUFFER_LENGTH];
sock.BeginReceive(
buffer,
0,
BUFFER_LENGTH,
SocketFlags.None,
cb_Received,
null);
} 
catch (SocketException ex) 
{
OnError();
string x = "Error: " + ex.Message;
System.Windows.Forms.MessageBox.Show(x, "ShoutBot", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly);
sock.Close();
OnDisconnected();
}
PS.AddPacket(tempBuffer);
}

That is what I have but I always get disconnected. I think it may be overlapping but I am not sure.

Thx in advance for any help.


----------

