Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Server based on AsyncCallback not running as expected

Server based on AsyncCallback not running as expected

Scheduled Pinned Locked Moved C#
csharpsysadmindebugging
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • T Tichaona J

      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.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      You need to call BeginReceive() in your OnDataReceived() method. You do that to reset it. You will need to also consider if you don't want to do that, such as when the app is exiting.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups