Delegate not calling function defined under Windows forms ?

Hai ....,
          I have designed a basic client-server chat application (asynchronous mode) in windows forms. Client project i.e client application is having one Interface screen, to send and receive message, and one class file that connects the client to the server, etc., 

          Similarly for server application, there is one interface screen and a class file. The coding section of the server application has one function, which is as follows:


public void CommunicationWindow_AppendMessage(string strMessage)
{
txtAllMessages.Text += strMessage; //self-explanatory
}


         The class (.cs) file of the server application, which calls the above method, is as follows:


public sealed class MessagingServer : IDisposable
{
[b]public delegate void ClientMessage(string message);[/b]

private static MessagingServer server;

private ClientInfo cInfo = null;
private Socket serverSocket;
private Hashtable hashClientSockets;
private bool blnDisposed = false;

private MessagingServer()
{
//initialize the server socket & client sockets
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);

hashClientSockets = new Hashtable();

//create the end point to which this server socket will be binded
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 7777);

//now bind the server socket to this end point
serverSocket.Bind(ipLocal);

//start the server to listen for the client with a max 
//pending queue of length=5
serverSocket.Listen(5);

//define a callback function so that whenever a new client
//connects, the specified function gets called
serverSocket.BeginAccept(new AsyncCallback(OnClientConnected), null);
}

public ClientInfo ClientInfo
{
get
{
return cInfo;
}

set
{
cInfo = value;
}
}

public ClientInfo[] GetConnectedClients()
{
int count = 0;
ClientInfo[] cInfo = new ClientInfo[hashClientSockets.Count];

//get the enumerator object
IDictionaryEnumerator ide = hashClientSockets.GetEnumerator();

//iterate thru each and build client info
while(ide.MoveNext())
{
cInfo[count] = (ClientInfo)ide.Key;
count++;
}
return cInfo;
}

public static MessagingServer GetMessagingServer()
{
if (server == null)
server = new MessagingServer();

return server;
}

private void OnClientConnected(IAsyncResult ar)
{
//complete the BeginAccept() asynchronous call of the server
//by calling the EndAccept. This will also return return a 
//new client socket
Socket clientSocket = serverSocket.EndAccept(ar);

//update the client sockets hash collection with
//this new client socket
hashClientSockets.Add(ClientInfo, clientSocket);

//assign the worket socket to do further processing
//with the connected client
HandleClient(clientSocket);

//now our main socket is free. Therefore, we make it to
//listen for other connecting clients
serverSocket.BeginAccept(new AsyncCallback(OnClientConnected), null);
}

private void HandleClient(Socket clientSocket)
{
//first construct the state object
StateObject so = new StateObject();
so.socket = clientSocket;

//start receiving data from the connected client
clientSocket.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE,
SocketFlags.None, new AsyncCallback(OnDataReceived), so);
}

private void OnDataReceived(IAsyncResult ar)
{
//get the state object
StateObject so = (StateObject)ar.AsyncState;

//get the socket object from the state object
Socket socket = so.socket;

//get the number of bytes read
int bytes = socket.EndReceive(ar);

if (bytes > 0)
{
//append the data read
 so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, bytes));

//continue to receive the left out data
socket.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(OnDataReceived), so);
}
else
{
if (so.sb.Length > 1)
{
//now all the data has been read
        string strAllData = Encoding.ASCII.GetString(so.buffer, 0, bytes);

       [b]frmCommunicationWindow cWnd = frmCommunicationWindow.GetCommunicationWindow();
cWnd.Show();
cWnd.BringToFront();

//create instance for the delegate to be called
ClientMessage msg = new ClientMessage(cWnd.CommunicationWindow_AppendMessage);
msg(strAllData);[/b]
}
}

//we once again wait for any more message that is to
//be handled for the client
HandleClient(socket);
}

public void Dispose()
{
if (blnDisposed == false)
{
cInfo = null;
serverSocket.Close();
serverSocket = null;
hashClientSockets = null;
blnDisposed = true;
}
}
}

                
              The above given is the listing i adopted. The area marked in bold, int the [b]OnDataReceived() [/b] function, you can see how I'm calling the method to display the message. I tried this but nothing appears in the message window.

              Please correct where i'm wrong

Thanx,
-Hemant


Answer this question

Delegate not calling function defined under Windows forms ?

  • Delegate not calling function defined under Windows forms ?