예제 #1
0
        /// <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 " + client.Sock.RemoteEndPoint.ToString() + " joined");
            _logger.WriteLog("Client " + client.Sock.RemoteEndPoint.ToString() + " joined");
            // Get current date and time.
            DateTime now         = DateTime.Now;
            String   strDateLine = "Welcome " + now.ToString("G") + "\n\r";

            _logger.WriteLog("Sending Welcome Message... ");
            // 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);
        }
예제 #2
0
        /// <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 " + client.Sock.RemoteEndPoint.ToString() + " joined" );
            _logger.WriteLog("Client " + client.Sock.RemoteEndPoint.ToString() + " joined");
            // Get current date and time.
            DateTime now = DateTime.Now;
            String strDateLine = "Welcome " + now.ToString("G") + "\n\r";
            _logger.WriteLog("Sending Welcome Message... " );
            // 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);
        }
예제 #3
0
        public void OnReecieveData(IAsyncResult ar)
        {
            //Socket sock = (Socket)ar.AsyncState;
            SocketChatClient client = (SocketChatClient)ar.AsyncState;
            Socket           sock   = client.Sock;

            byte[] aryRet = client.GetRecievedData(ar);
            Indus.ASN_1.Services.BER.Decoder decoder = new Indus.ASN_1.Services.BER.Decoder(aryRet);
            string decoded = decoder.Decode();

            _logger.WriteLog(decoded);

            // If no data was recieved then the connection is probably dead
            if (aryRet.Length < 1)
            {
                Console.WriteLine("Client " + client.Sock.RemoteEndPoint.ToString() + " disconnected");
                client.Sock.Close();
                m_aryClients.Remove(client);
                return;
            }
            //_logger.WriteLog("Data Received at Server --Sending to all clients--You can use a session ID to send to one specific client");

            //Send only to the one who send data
            foreach (SocketChatClient clientSend in m_aryClients)
            {
                if (client.Sock.RemoteEndPoint == clientSend.Sock.RemoteEndPoint)
                {
                    try
                    {
                        //Command Handling demponstrated here
                        if (decoded.Contains("GetServerDirectory_Request"))
                        {
                            string[] lD = _iecserver.GetServerDirectory(Indus.ACSI.Core.ObjectClassEnum.LogicalDevice);
                            Indus.ASN_1.Services.BER.Encoder encode = new Indus.ASN_1.Services.BER.Encoder(lD[0]);
                            clientSend.Sock.Send(encode.Encode());
                        }
                        else
                        {
                            Indus.ASN_1.Services.BER.Encoder encode = new Indus.ASN_1.Services.BER.Encoder("Response");
                            clientSend.Sock.Send(encode.Encode());
                        }
                    }
                    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;
                    }
                }
            }


            // Broadcast:Send the recieved data to all clients (including sender for echo)


            //foreach (SocketChatClient clientSend in m_aryClients)
            //{
            //    try
            //    {
            //        if clientSend.Sock.Send(aryRet);
            //    }
            //    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;
            //    }
            //}
            ServerDataReceived();
            client.SetupRecieveCallback(this);
        }