示例#1
0
        private void Select()
        {
            ArrayList hex = new ArrayList();
            ///1. Poll the Server sockChannel; if true then accept the new connection.
            ///2. Poll the Client sockChannel; if true then receive data from Clients.

            if (Server.Poll(0, SelectMode.SelectRead))
            {
                int i = Client.Add(new SocketChannel());
                ((SocketChannel)Client[i]).sockChannel = Server.Accept ();
                String debug = "Client " + i + " connected.";
                LogDebug (debug);
            }

            for (int i = 0; i < Client.Count; i++)
            {
                //check for incoming data
                if (((SocketChannel)Client[i]).sockChannel.Poll(0, SelectMode.SelectRead))
                {
                    //receive incoming data
                    //if (((SocketChannel)Client[i]).Recv(ref MessageBuffer) > 0)
                    if (((SocketChannel)Client[i]).Recv(ref MessageBuffer, 0, SocketHeaderLength.getLength(MsgHeaderLenFormat)) > 0)
                    //if (((SocketChannel)Client[i]).Recv(ref MessageBuffer, 0, 2) > 0)
                    {
                        ASCIIEncoding AE = new ASCIIEncoding();
                        byte[] ByteArray = AE.GetBytes(MessageBuffer);
                        int len = SocketHeaderLength.getEndianByteToInt(MsgLenOrder_Recv, ByteArray);
                        LogDebug ("Client sending ...");
                        LogDebug ("Client Len Message - " + len);
                        if (((SocketChannel)Client[i]).Recv(ref MessageBuffer, 0, len) > 0)
                        {
                            LogDebug ("Client Message : " + MessageBuffer );
                            ProcessMessage proses = new ProcessMessage(MessageBuffer);
                            String respon = proses.getOutputProcess();
                            Send(respon, (SocketChannel)Client[i]);
                        }
                        else
                        {
                            ((SocketChannel)Client[i]).sockChannel.Shutdown(SocketShutdown.Both);
                            ((SocketChannel)Client[i]).sockChannel.Close();
                            Client.RemoveAt (i);
                            LogDebug ("Client disconnected.");
                        }
                    }
                    //recv returned <= 0; close the socket
                    else
                    {
                        ((SocketChannel)Client[i]).sockChannel.Shutdown(SocketShutdown.Both);
                        ((SocketChannel)Client[i]).sockChannel.Close();
                        Client.RemoveAt (i);
                        LogDebug ("Client disconnected.");
                    }
                }

            }
        }
        private static void select()
        {
            ArrayList hex = new ArrayList();
            ///1. Poll the Server sockChannel; if true then accept the new connection.
            ///2. Poll the Client sockChannel; if true then receive data from Clients.

            if (Server.Poll(0, SelectMode.SelectRead))
            {
                int i = Client.Add(new SocketChannel());
                ((SocketChannel)Client[i]).sockChannel = Server.Accept ();
                Console.WriteLine("Client {0} connected.", i  );
            }

            for (int i = 0; i < Client.Count; i++)
            {
                //check for incoming data
                if (((SocketChannel)Client[i]).sockChannel.Poll(0, SelectMode.SelectRead))
                {
                    //receive incoming data
                    //if (((SocketChannel)Client[i]).Recv(ref rln) > 0)
                    if (((SocketChannel)Client[i]).Recv(ref rln, 0, SocketHeaderLength.getLength(MsgHeaderLenFormat)) > 0)
                    {
                        Console.WriteLine("Client sending {0} - ", rln  );
                        ASCIIEncoding AE = new ASCIIEncoding();
                        byte[] ByteArray = AE.GetBytes(rln);

                        int len = SocketHeaderLength.getEndianByteToInt(MsgLenOrder_Recv, ByteArray);
                        Console.WriteLine("Client Len Message : {0} ", len);
                        if (((SocketChannel)Client[i]).Recv(ref rln, 0, len) > 0)
                        {
                            Console.WriteLine("Client Message : {0} ", rln );
                            ProcessMessage proses = new ProcessMessage(rln);
                            String respon = proses.getOutputProcess();
                            send(respon, (SocketChannel)Client[i]);
                        }
                        else
                        {
                            ((SocketChannel)Client[i]).sockChannel.Shutdown(SocketShutdown.Both);
                            ((SocketChannel)Client[i]).sockChannel.Close();
                            Client.RemoveAt (i);
                            Console.WriteLine("Client {0} disconnected.", i);
                        }
                        Console.WriteLine( "\r\n" );
                    }
                    //recv returned <= 0; close the socket
                    else
                    {
                        ((SocketChannel)Client[i]).sockChannel.Shutdown(SocketShutdown.Both);
                        ((SocketChannel)Client[i]).sockChannel.Close();
                        Client.RemoveAt (i);
                        Console.WriteLine("Client {0} disconnected.", i);
                    }
                }

            }
        }