/// <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) { SocketChatClient client = (SocketChatClient)ar.AsyncState; byte [] aryRet = client.GetRecievedData(ar); // If no data was recieved then the connection is probably dead if (aryRet.Length < 1) { Console.WriteLine("Client {0}, disconnected", client.Sock.RemoteEndPoint); 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); Byte[] zeroEnd = new Byte[1]; zeroEnd[0] = (Byte)0; clientSend.Sock.Send(zeroEnd); } catch { // If the send fails the close the connection Console.WriteLine("Send to client {0} failed", client.Sock.RemoteEndPoint); 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 friend /// Send a welcome to the new client /// Setup a callback to recieve data /// </summary> /// <param name="sockClient">Connection to keep</param> //public void NewConnection( TcpListener listener ) 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 = "Welcome to the AFLAX Chat Server (" + 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); Byte[] zeroEnd = new Byte[1]; zeroEnd[0] = (Byte)0; client.Sock.Send(zeroEnd); client.SetupRecieveCallback(this); }
private ArrayList m_aryClients = new ArrayList(); // List of Client Connections #endregion Fields #region Methods /// <summary> /// Add the given connection to our list of clients /// Note we have a new friend /// Send a welcome to the new client /// Setup a callback to recieve data /// </summary> /// <param name="sockClient">Connection to keep</param> //public void NewConnection( TcpListener listener ) 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 = "Welcome to the AFLAX Chat Server (" + 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 ); Byte[] zeroEnd = new Byte[1]; zeroEnd[0] = (Byte) 0; client.Sock.Send(zeroEnd); client.SetupRecieveCallback( this ); }