/// <summary> /// Get the new data and send it out to all other connections. /// Note: If not data was recieved the connection has probably /// died. /// </summary> /// <param name="ar"></param> public void OnRecievedData(IAsyncResult ar) { client = (SocketChatClient)ar.AsyncState; byte [] aryRet = client.GetRecievedData(ar); // If no data was recieved then the connection is probably dead if (aryRet.Length < 1) { //WriteConsoleData( "Client " + client.Sock.RemoteEndPoint + " disconnected" ); client.Sock.Close(); m_aryClients.Remove(client); return; } // Send the recieved data to all clients (including sender for echo) foreach (SocketChatClient clientSend in m_aryClients) { try { clientSend.Sock.Send(aryRet); } catch { // If the send fails the close the connection clientSend.Sock.Close(); m_aryClients.Remove(client); return; } } client.SetupRecieveCallback(this); }
/// <summary> /// Add the given connection to our list of clients /// Note we have a new client connection /// Send a welcome to the new client /// Setup a callback to recieve data /// </summary> /// <param name="sockClient">Connection to keep</param> public void NewConnection(Socket sockClient) { // Program blocks on Accept() until a client connects. //SocketChatClient client = new SocketChatClient( listener.AcceptSocket() ); SocketChatClient client = new SocketChatClient(sockClient); m_aryClients.Add(client); Console.WriteLine("Client {0}, joined", client.Sock.RemoteEndPoint); // Get current date and time. DateTime now = DateTime.Now; String strDateLine = "You are connected to RAS CORE " + now.ToString("G"); // Convert to byte array and send. Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray()); client.Sock.Send(byteDateLine, byteDateLine.Length, 0); client.SetupRecieveCallback(this); }