コード例 #1
0
        static void Main(string[] args)
        {
            try {
                string    msg  = "";
                IPAddress ipAd = IPAddress.Parse("172.20.16.136"); //use local m/c IP address, and use the same in the client
                //IPAddress ipAd = IPAddress.Parse("192.168.56.1");
                TcpListener myList = new TcpListener(ipAd, 8005);

                myList.Start();

                Console.WriteLine("The server is running at port 8005...");
                Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");


                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                byte[] b = new byte[s.SendBufferSize];
                int    k = s.Receive(b);
                Console.WriteLine("Recieved...");
                for (int i = 0; i < k; i++)
                {
                    msg += Convert.ToChar(b[i]);
                }

                Console.Write(msg);
                Information info = new Information();
                Object      obj  = DeSerializeAnObject(msg, info.GetType());
                Information inf  = (Information)obj;
                Console.WriteLine("-------------- " + inf.UserName + " " + inf.Password + " -------------");

                DBoperations.Users data = new DBoperations.Users();
                int result = data.isUserExist(inf.UserName, inf.Password);
                if (result > 0)
                {
                    Console.WriteLine("User found!! :)");
                }
                else
                {
                    Console.WriteLine("User Not found!! :(");
                }

                //  ASCIIEncoding asen = new ASCIIEncoding();
                //  s.Send(asen.GetBytes("The string was recieved by the server."));
                //  Console.WriteLine("\\nSent Acknowledgement");


                s.Close();

                myList.Stop();
            }

            catch (Exception e) {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
コード例 #2
0
        private static DBoperations.Users data = new DBoperations.Users(); // get instance of DB


        private static void handelRequest(Object arg)
        {
            // get requests and handels them
            Socket s = (Socket)arg;
            string msg;

            byte[] bReq    = new byte[s.SendBufferSize]; // declere byte array to recieve a massage
            int    reqSize = s.Receive(bReq);            // recieve the req type and get it's size

            string typeMsg = "";

            for (int i = 0; i < reqSize; i++) // convert the message  from byte to string
            {
                typeMsg += Convert.ToChar(bReq[i]);
            }

            Console.WriteLine("^^^^^^^^^^ " + typeMsg + " ^^^^^^^^^^");

            if (typeMsg.Equals("Log In"))
            {
                byte[] b = new byte[s.SendBufferSize]; // recieve the user details in bytes
                int    k = s.Receive(b);
                Console.WriteLine("Recieved...");
                msg = "";
                for (int i = 0; i < k; i++) // convert to string
                {
                    msg += Convert.ToChar(b[i]);
                }
                Array.Clear(b, 0, b.Length);
                Console.Write(msg);
                Information info = new Information();                        // create an instance of client information
                Object      obj  = DeSerializeAnObject(msg, info.GetType()); // convert from string to information object (2 rows)
                Information inf  = (Information)obj;
                Console.WriteLine("-------------- " + inf.UserName + " " + inf.Password + " -------------");

                int result = data.isUserExist(inf.UserName, inf.Password);

                if (result > 0)                                                                              // user exists
                {
                    data.UpdateDetailsSignIn(inf.UserName, inf.Password, inf.IpAddress, inf.Port, inf.Path); // update user's ip, port and path in DB
                    data.SetStatus(inf.UserName, inf.Password, "enable");                                    // set status to enable - available to download from
                    Console.WriteLine("User found!! :)");
                    b[0] = 1;                                                                                // return answer from server to client. 1 = found.

                    s.Send(b);
                }
                else  // user not exists
                {
                    Console.WriteLine("User Not found!! :(");
                    b[0] = 0; // return answer from server to client. 0 = not found.
                    s.Send(b);
                }
                s.Close();
            }

            else if (typeMsg.Equals("Files")) // show files in FileManager's table
            {
                Console.WriteLine("searching for files");
                sendFilesList(s);
                s.Close();
            }

            else if (typeMsg.Equals("Search")) // search for a file in FileManager's table
            {
                SearchFile(s, "Search");       // call func with search req
                s.Close();
            }

            else if (typeMsg.Equals("Resources"))
            {
                SearchFile(s, "Resources"); // call func with Resources req
                s.Close();
            }

            else if (typeMsg.Equals("Insert"))
            {
                Console.WriteLine("---------- insert request -----------");
                insertFileToDB(s);
                s.Close();
            }

            else if (typeMsg.Equals("Close"))
            {
                Console.WriteLine("User Log out");
                logOutUser(s);
                s.Close();
            }
        }