tags:

views:

8

answers:

0

Hi,

I am trying to write a socket interface for my program and I'm finding that it never triggers the acceptCallback. I have a messageSender.exe that runs fine with messageReceiver.exe. I'm adding the following code to my app and trying to get it to communicate with messageSender.exe, but my program never enters into the acceptCallback (I have a breakpoint at the first line of that function). Here is what the code looks like:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace GanttChart.DataAccess
{
   public class StateObject
   {
      public Socket workSocket = null;
      public const int BufferSize = 1024;
      public byte[] buffer = new byte[BufferSize];
      public StringBuilder sb = new StringBuilder();
   }

   public class CommunicationsClient
   {
      private static ManualResetEvent allDone = new ManualResetEvent(false);

      public CommunicationsClient()
      {
         StartListening();
      }

      public void StartListening() 
      {
         IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
         IPEndPoint localEP = new IPEndPoint(ipHostInfo.AddressList[0],2515);
         Console.WriteLine("Local address and port : {0}",localEP.ToString());

         Socket listener = new Socket( localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp );

         try 
         {
            listener.Bind(localEP);
            listener.Listen(10);
            while (true) 
            {
               allDone.Reset();
               Console.WriteLine("Waiting for a connection...");
               listener.BeginAccept( new AsyncCallback(this.acceptSocketClientCallback), listener );
               allDone.WaitOne();
            }
         } 
         catch (Exception e) 
         {
            Console.WriteLine(e.ToString());
         }
         Console.WriteLine( "Closing the listener...");
      }

      public void acceptSocketClientCallback(IAsyncResult ar)
      {
         // Get the socket that handles the client request.
         Socket listener = (Socket) ar.AsyncState;
         Socket handler = listener.EndAccept(ar);

         // Signal the main thread to continue.
         allDone.Set();

         // Create the state object.
         StateObject state = new StateObject();
         state.workSocket = handler;
         handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(CommunicationsClient.readDataCallback), state);
      } 

      public static void readDataCallback(IAsyncResult ar) 
      {
         StateObject state = (StateObject) ar.AsyncState;
         Socket handler = state.workSocket;

         // Read data from the client socket.
         int read = handler.EndReceive(ar);

         // Data was read from the client socket.
         if (read > 0) 
         {
             state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,read));
             handler.BeginReceive(state.buffer,0,StateObject.BufferSize, 0, new AsyncCallback(readDataCallback), state);
         } 
         else 
         {
            if (state.sb.Length > 1) 
            {
                 // All the data has been read from the client;
                 // display it on the console.
                 string content = state.sb.ToString();
                 Console.WriteLine("Read {0} bytes from socket.\n Data : {1}", content.Length, content);
             }
             handler.Close();
         }
      }
   }
}

Thanks in advance for any insight you have to offer, -Michele-