Пример #1
0
        static void Main(string[] args)
        {
            string    ip   = GetLocalIPAddress();
            IPAddress ipAd = IPAddress.Parse(ip);
            //Initialize listener on given IP and port
            TcpListener myList = new TcpListener(ipAd, 8001);

            //Start Listening at the specified port
            myList.Start();
            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
            //Endless listening for the clients to connect
            while (true)
            {
                try
                {
                    Console.WriteLine("Waiting for a connection.....");

                    //Start waiting for client to connect and
                    //return a socket for communciation between server and client
                    Socket s = myList.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                    //Reading information from client
                    byte[]        buffer   = new byte[1000];
                    int           k        = s.Receive(buffer);
                    List <string> dataList = new List <string>();
                    dataList = message_decode(buffer);
                    for (int i = 0; i < dataList.Count; i++)
                    {
                        Console.WriteLine(dataList[i]);
                    }
                    //MessageFactory factory = new MessageFactory(dataList);
                    IMessage messageInstance = MessageFactory.GetMessageInstance(dataList);
                    dataList.Clear();
                    IDBConnect dbConnector = new databaseCommunicator();
                    messageInstance.setDBConnectInstance(dbConnector);
                    dataList = messageInstance.execute();
                    byte[] r = message_format(dataList);
                    for (int i = 0; i < dataList.Count; i++)
                    {
                        Console.WriteLine(dataList[i]);
                    }
                    //Console.WriteLine(Encoding.UTF8.GetString(r));
                    s.Send(r);
                    //This is also nessesary part
                    //but for this case, when we have
                    //endless listening in a single-threaded
                    //application. So I just commented it.

                    /*s.Close();
                     * myList.Stop();*/
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error..... " + e.StackTrace);
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            string ip = GetLocalIPAddress();
            IPAddress ipAd = IPAddress.Parse(ip);
            //Initialize listener on given IP and port
            TcpListener myList = new TcpListener(ipAd, 8001);
            //Start Listening at the specified port
            myList.Start();
            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
            //Endless listening for the clients to connect
            while (true)
            {
                try
                {
                    Console.WriteLine("Waiting for a connection.....");

                    //Start waiting for client to connect and
                    //return a socket for communciation between server and client
                    Socket s = myList.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                    //Reading information from client
                    byte[] buffer = new byte[1000];
                    int k = s.Receive(buffer);
                    List<string> dataList = new List<string>();
                    dataList = message_decode(buffer);
                    for (int i = 0; i < dataList.Count; i++)
                        Console.WriteLine(dataList[i]);
                    //MessageFactory factory = new MessageFactory(dataList);
                    IMessage messageInstance = MessageFactory.GetMessageInstance(dataList);
                    dataList.Clear();
                    IDBConnect dbConnector = new databaseCommunicator();
                    messageInstance.setDBConnectInstance(dbConnector);
                    dataList = messageInstance.execute();
                    byte[] r = message_format(dataList);
                    for (int i = 0; i < dataList.Count; i++)
                        Console.WriteLine(dataList[i]);
                    //Console.WriteLine(Encoding.UTF8.GetString(r));
                    s.Send(r);
                    //This is also nessesary part
                    //but for this case, when we have
                    //endless listening in a single-threaded
                    //application. So I just commented it.
                    /*s.Close();
                    myList.Stop();*/
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error..... " + e.StackTrace);
                }
            }
        }