Server based on AsyncCallback not running as expected
-
Hi All I have a server that starts up as part of my application (when the application InitializeComponent()), as below:
public Window1() { InitializeComponent(); ...other code //Start the server... connectionAccept(); }
This calls the below code:private void connectionAccept() { try { listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint LocalIP = new IPEndPoint(IPAddress.Any, 2112); listener.Bind(LocalIP); listener.Listen(0); listener.BeginAccept(new AsyncCallback(OnClientConnect), null); }//End of try catch (SocketException se) { MessageBox.Show(se.Message); }//End of catch }// End of connectionAccept method //On client call.... public void OnClientConnect(IAsyncResult asyn) { try { otherListener[numberOfClients] = listener.EndAccept(asyn); WaitForData(otherListener[numberOfClients]); ++numberOfClients; String strData = Convert.ToString(numberOfClients); MessageBox.Show(strData); listener.BeginAccept(new AsyncCallback(OnClientConnect), null); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n"); }//End of first catch catch (SocketException se) { MessageBox.Show(se.Message); }//End of second catch }//End of OnClientConnect method public class SocketPacket { public System.Net.Sockets.Socket currentSoc; public byte[] dataBuffer = new byte[0]; }//End of Socket packet public void WaitForData(System.Net.Sockets.Socket soc) { try { if (pfnListnerCallBack == null) { pfnListnerCallBack = new AsyncCallback(OnDataReceived); SocketPacket socPkt = new SocketPacket(); socPkt.
-
Hi All I have a server that starts up as part of my application (when the application InitializeComponent()), as below:
public Window1() { InitializeComponent(); ...other code //Start the server... connectionAccept(); }
This calls the below code:private void connectionAccept() { try { listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint LocalIP = new IPEndPoint(IPAddress.Any, 2112); listener.Bind(LocalIP); listener.Listen(0); listener.BeginAccept(new AsyncCallback(OnClientConnect), null); }//End of try catch (SocketException se) { MessageBox.Show(se.Message); }//End of catch }// End of connectionAccept method //On client call.... public void OnClientConnect(IAsyncResult asyn) { try { otherListener[numberOfClients] = listener.EndAccept(asyn); WaitForData(otherListener[numberOfClients]); ++numberOfClients; String strData = Convert.ToString(numberOfClients); MessageBox.Show(strData); listener.BeginAccept(new AsyncCallback(OnClientConnect), null); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n"); }//End of first catch catch (SocketException se) { MessageBox.Show(se.Message); }//End of second catch }//End of OnClientConnect method public class SocketPacket { public System.Net.Sockets.Socket currentSoc; public byte[] dataBuffer = new byte[0]; }//End of Socket packet public void WaitForData(System.Net.Sockets.Socket soc) { try { if (pfnListnerCallBack == null) { pfnListnerCallBack = new AsyncCallback(OnDataReceived); SocketPacket socPkt = new SocketPacket(); socPkt.