Brant Williams

Brant Williams

  • NA
  • 11
  • 3.1k

C# Newbie - Asynch Socket Problem

Feb 19 2012 5:45 AM
I am in the process of becoming a self taught programmer...so patience with me...and if I ask any dumb questions...I apologize in advance.  I am trying to be as concise and clear as possible.

1.  There is a server application that is installed on my PC.  This application is an intermediary.  It connects to an internet server, so in that respect it is a client.  But from my perspective, it is a server.  I connect to this "server app" on my PC, with the application I am writing, using socket(s), locally at 127.0.0.1.
2.  My application instantiates a socket, or sockets, configures them, connects to this local server app on my PC...and then communicates.  I have all this working.
3.  The basic structure of the communication between my App, and this local Server is an Async Loop.  The fields/objects/methods are live on the main form of my basic WinForms App. 
4.  Here is the basic structure.  I have exclude minutae like the socket initiation....connection...and event handlers that start events....since I dont want to post a ton of code which will bore you all...
5.  Basically...my problem is I can't seem to figure out how to close the socket without crashing my App....

//******************************************************************************//
public partial class MainForm : Form
{
  private Socket A1Socket;
  private AsyncCallback A1Callback;
  private byte[] a1SocketBuffer = new byte[65536];
  private bool a1BeginReceiveFlag = true;
  private byte[] a1CopyBuffer = new byte[65536];
  bool stopWfdLoop = false;

  public MainForm()
  {InitializeComponent();}


  //Lot of stuff I skipped....

 
  //Event that kicks off data download
  private void SomeButton_Click(object sender, EventArgs e)  //At this point, my socket is up and running, and I want to star the loop that will pull data.
  {
  waitForData(CM.A1Cfg.SocketType);  //The argument CM.A1Cfg.SocketType is just a descriptive string.  In the future it will help differentiate between different socket types.
  }
 
 
  //The waitForData() method.  If the stopWfdLoop flag is set true...this stops the loop.
  private void waitForData(string SocketType)
  {
  if (!stopWfdLoop)
  {
  if (A1Callback == null)
  {
  A1Callback = new AsyncCallback(onReceive);
  }

  if (a1BeginReceiveFlag)  //This flag make sure we never call BeginReceive until we have copied the buffer
  {
  a1BeginReceiveFlag = false;
  A1Socket.BeginReceive(a1SocketBuffer, 0, a1SocketBuffer.Length, SocketFlags.None, A1Callback, SocketType);
  }
  }
  }

 
  //The OnReceive() method...which is automatically called by the .NET framework when data is ready to be copied on my end.
  private void onReceive(IAsyncResult argOr) 
  {
  if (argOr.AsyncState.ToString().Equals("A1"))
  {
  int rxByteCount = A1Socket.EndReceive(argOr);
  for (int i = 0; i < rxByteCount - 1; i++){a1CopyBuffer[i] = a1SocketBuffer[i]; }
  a1BeginReceiveFlag = true;  //OK to set the Flag True again 
 
  //Some processing methods...I skipped....
 
  waitForData(CM.A1Cfg.SocketType);  //This sends us right back into the loop.  So this loop just goes on and on...till the flag kicks us out 
  }
  }

//**************************************************************************************************************//
 
So...again...my problem is when I try to close the socket..the app crashes.  I thought that by having the stopWfdloop flag...and setting it to true...I would stop the loop.  Then I could shut down and stop the sockets....but it does not work.  Here is what I do to try and shut down the socket.

private void DisconnectButton_Click(object sender, EventArgs e)
  {
   stopWfdLoop = true;
   waitForData(CM.A1Cfg.SocketType);  //This idea here is to call the loop once...with the flag set true, to make sure the loop is shut down before we proceed
   A1Socket.Shutdown(SocketShutdown.Both);
   A1Socket.Close();
   A1Socket = null;
  stopWfdLoop = true;
  }

App dies here...

What am I missing? 

Answers (10)