示例#1
0
        static void Main(string[] args)
        {
            //Sets up the server
            initializeUserCrediential();
            Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);
            serverSocket            = new TcpListener(IPAddress.Parse("127.0.0.1"), 19278);
            TcpClient clientSocket = default(TcpClient);
            int       counter      = 0;

            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");
            counter = 0;
            //Looks for new clients that connect
            while ((true))
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();

                NetworkStream networkStream = clientSocket.GetStream();

                currentlyLoggedInHash.Add("" + counter, clientSocket);

                handleClinet client = new handleClinet();
                client.startClient(clientSocket, "" + counter, currentlyLoggedInHash);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            //Creates a TcpListener object to listen for incoming connections on computer's IP address
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 80);
            //intialize TcpClient to null
            TcpClient clientSocket = default(TcpClient);
            //keep track of connecting client's IP Address
            string client_IP = "";

            //start server and begin listening
            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");

            //create a new thread for my commands to run on
            Thread me = new Thread(RemoveClient);

            me.Start();

            while ((true))
            {
                //accept pending request
                clientSocket = serverSocket.AcceptTcpClient();

                //record down it's IP Address
                client_IP = ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString();
                //intialize a buffer
                byte[] bytesFrom = new byte[70000];
                //initialize string to store name of client
                string dataFromClient = null;

                //get byte data from accepted clientSocket
                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                //convert bytes from buffer into string, store in dataFromClient
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("\0"));

                //add client into hashtable. string, TcpClient
                clientsList.Add(clientSocket, dataFromClient);

                //broadcast that this client has joined
                broadcast(dataFromClient + " Joined ", dataFromClient, false);
                Console.WriteLine(dataFromClient + " Joined chat room. IP -  " + client_IP);

                //create new handleClient object to handle client's messages that are sent
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            //Creates a TcpListener object to listen for incoming connections on computer's IP address 
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 12345);
            //intialize TcpClient to null
            TcpClient clientSocket = default(TcpClient);
            //keep track of connecting client's IP Address
            string client_IP = "";

            //start server and begin listening 
            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");

            //create a new thread for my commands to run on
            Thread me = new Thread(RemoveClient);
            me.Start();

            while ((true))
            {
                //accept pending request
                clientSocket = serverSocket.AcceptTcpClient();

                //record down it's IP Address
                client_IP = ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString();
                //intialize a buffer
                byte[] bytesFrom = new byte[70000];
                //initialize string to store name of client
                string dataFromClient = null;

                //get byte data from accepted clientSocket
                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                //convert bytes from buffer into string, store in dataFromClient
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("\0"));

                //add client into hashtable. string, TcpClient
                clientsList.Add(clientSocket, dataFromClient);

                //broadcast that this client has joined
                broadcast(dataFromClient + " Joined ", dataFromClient, false);
                Console.WriteLine(dataFromClient + " Joined chat room. IP -  " + client_IP);

                //create new handleClient object to handle client's messages that are sent
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");
            counter = 0;
            while ((true))
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();

                byte[] bytesFrom      = new byte[10025];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);

                broadcast(dataFromClient + " Joined ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " Joined chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);

                clientSocket.Close();
                serverSocket.Stop();



                Console.WriteLine("exit");
                Console.ReadLine();
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            // Establish the local endpoint for the socket.
            // The DNS name of the computer
            // running the listener is "host.contoso.com".
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress myIp in ipHostInfo.AddressList)
            {
                Console.WriteLine(string.Format("myIp: {0} myFam: {1}", myIp, myIp.AddressFamily));
            }
            IPAddress  ipAddress     = ipHostInfo.AddressList[2];
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1337);

            Console.WriteLine(string.Format("My IP ADDRESS: {0}", localEndPoint));
            TcpListener serverSocket = new TcpListener(localEndPoint);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while (true)
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }
示例#6
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while (true)
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }
示例#7
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient clientSocket = default(TcpClient);
            int counter = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while (true)
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }
示例#8
0
文件: Program.cs 项目: Mortion/Uses
        static void Main(string[] args)

        {

            TcpListener serverSocket = new TcpListener(8888);

            TcpClient clientSocket = default(TcpClient);

            int counter = 0;



            serverSocket.Start();

            Console.WriteLine("Chat Server Started ....");

            counter = 0;

            while ((true))

            {

                counter += 1;

                clientSocket = serverSocket.AcceptTcpClient();



                byte[] bytesFrom = new byte[10025];

                string dataFromClient = null;



                NetworkStream networkStream = clientSocket.GetStream();

                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));



                clientsList.Add(dataFromClient, clientSocket);



                broadcast(dataFromClient + " Joined ", dataFromClient, false);



                Console.WriteLine(dataFromClient + " Joined chat room ");

                handleClinet client = new handleClinet();

                client.startClient(clientSocket, dataFromClient, clientsList);

            }



            clientSocket.Close();

            serverSocket.Stop();

            Console.WriteLine("exit");

            Console.ReadLine();


     

        }
        static void Main(string[] args)
        {
            System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
            start.FileName    = @"\ServerTest.exe";
            start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            TcpListener serverSocket = new TcpListener(54206);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");
            counter = 0;
            while ((true))
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();



                byte[] bytesFrom      = new byte[10025];
                string dataFromClient = null;
                string clientPubKey   = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                networkStream.Flush();

                //lets take a new CSP with a new 2048 bit rsa key pair
                var csp = new RSACryptoServiceProvider(2048);

                //how to get the private key
                Globals.privKey = csp.ExportParameters(true);

                //and the public key ...
                var pubKey = csp.ExportParameters(false);

                //converting the public key into a string representation
                string pubKeyString;
                {
                    //we need some buffer
                    var sw = new System.IO.StringWriter();
                    //we need a serializer
                    var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
                    //serialize the key into the stream
                    xs.Serialize(sw, pubKey);
                    //get the string from the stream
                    pubKeyString = sw.ToString();
                }
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(pubKeyString + "$");

                networkStream.Write(outStream, 0, outStream.Length);

                Thread.Sleep(500);


                networkStream.Read(bytesFrom, 0, bytesFrom.Length);

                clientPubKey = System.Text.Encoding.ASCII.GetString(bytesFrom);
                clientPubKey = clientPubKey.Substring(0, clientPubKey.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);

                var per = new Person();
                per.hash      = clientSocket.GetHashCode();
                per.initials  = dataFromClient.Substring(1);
                per.publicKey = clientPubKey;
                personList.Add(per);

                broadcast("3**" + dataFromClient.Substring(1) + " Joined**", dataFromClient, false);

                Console.WriteLine(dataFromClient.Substring(1) + " Joined chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, "0" /*dataFromClient.Substring(1)*/, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
示例#10
0
        static void Main(string[] args)
        {
            activeUsers = new List <String>();
            Console.WriteLine("Getting roles...");
            roles = new Dictionary <string, HashSet <string> >();
            string[] roleLines = File.ReadAllLines(path + "roles.txt");
            int      en;

            foreach (string l in roleLines)
            {
                string[]         line = l.Split(';');
                HashSet <string> r    = new HashSet <string>();
                en = 0;
                foreach (string l2 in line)
                {
                    if (en == 0)
                    {
                        ;
                    }
                    //r.name = l2;
                    else if (Permissions.Contains(l2))
                    {
                        r.Add(l2);
                    }
                    en++;
                }
                roles.Add(line[0], r);
            }
            Console.WriteLine("Getting users...");
            string[] userLines = File.ReadAllLines(path + "users.txt");
            users = new Dictionary <string, HashSet <string> >();
            foreach (string l in userLines)
            {
                string[]         line = l.Split(';');
                HashSet <string> u    = new  HashSet <string>();
                en = 0;
                foreach (string l2 in line)
                {
                    if (en == 0)
                    {
                        ;
                    }
                    // u.name = l2;
                    else
                    {
                        //foreach (KeyValuePair<string,HashSet<string>> r in roles)
                        //{
                        if (roles.ContainsKey(l2))
                        {
                            u.Add(l2);
                        }
                        //}
                    }
                    en++;
                }
                users.Add(line[0], u);
            }

            Console.WriteLine("Preparing server application...");

            List <string> temp_u = new List <string>();

            foreach (KeyValuePair <string, HashSet <string> > u in users)
            {
                temp_u.Add(u.Key);
            }

            Int32       port         = 13000;
            IPAddress   localAddr    = IPAddress.Parse("127.0.0.1");
            TcpListener serverSocket = new TcpListener(localAddr, port);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            Byte[] bytesTo = null;
            //String[] split = null;

            serverSocket.Start();
            Console.WriteLine("Server started at " + DateTime.Now.ToString("MMM d yyyy HH:mm:ss"));
            counter = 0;
            while ((true))
            {
                try
                {
                    counter     += 1;
                    clientSocket = serverSocket.AcceptTcpClient();

                    byte[] bytesFrom      = new byte[1002500];
                    string dataFromClient = null;

                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                    //clientsList.Add(dataFromClient, clientSocket);
                    Console.WriteLine(dataFromClient);
                    String[] splitMsg = dataFromClient.Split('|');
                    //broadcast(dataFromClient + " Joined ", dataFromClient, false);
                    if (activeUsers.Contains(dataFromClient))
                    {
                        bytesTo = System.Text.Encoding.ASCII.GetBytes("ALREADY_LOGGED$");
                        networkStream.Write(bytesTo, 0, bytesTo.Length);
                        networkStream.Flush();
                        clientSocket = null;
                    }
                    else if (!temp_u.Contains(splitMsg[0]))
                    {
                        bytesTo = System.Text.Encoding.ASCII.GetBytes("INCORRECT_LOGIN$");
                        networkStream.Write(bytesTo, 0, bytesTo.Length);
                        networkStream.Flush();
                        clientSocket = null;
                    }
                    else
                    {
                        if (!users[splitMsg[0]].Contains(splitMsg[1]))
                        {
                            bytesTo = System.Text.Encoding.ASCII.GetBytes("INCORRECT_ROLE$");
                            networkStream.Write(bytesTo, 0, bytesTo.Length);
                            networkStream.Flush();
                            clientSocket = null;
                        }
                        else
                        {
                            bytesTo = System.Text.Encoding.ASCII.GetBytes("OK$");
                            networkStream.Write(bytesTo, 0, bytesTo.Length);
                            networkStream.Flush();
                            activeUsers.Add(dataFromClient);
                            Console.WriteLine(dataFromClient + " logged in ");
                            handleClinet client = new handleClinet();
                            client.startClient(clientSocket, splitMsg[0], splitMsg[1], activeUsers);//, clientsList);
                        }
                    }
                }
                catch { }
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }