Ejemplo n.º 1
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[200000];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                var userMessage = JsonConvert.DeserializeObject <User>(dataFromClient);
                //dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                clientsList.Add(userMessage.UserName, clientSocket);

                broadcast(userMessage.UserName + " Joined ", userMessage.Message, false);

                Console.WriteLine(userMessage.UserName + " Joined chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, userMessage.UserName, clientsList);
            }
            //clientSocket.Close();
            //serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName());

            IPAddress ipAddress = ipHostEntry.AddressList[0];

            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8888);

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

            serverSocket.Start();
            Console.WriteLine("Chat Server uruchomiony ....");
            Console.WriteLine("nasłuchuje na adresie: " + ipEndPoint.Address.ToString());
            counter = 0;
            while ((true))
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();

                byte[] bytesFrom      = new byte[clientSocket.ReceiveBufferSize];
                string dataFromClient = null;

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

                clientsList.Add(dataFromClient, clientSocket);

                broadcast(dataFromClient + " dołączył do czatu ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " dołączył do czatu ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //listens to the client for information to be sent
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

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

                byte[] bytesFrom      = new byte[(int)clientSocket.ReceiveBufferSize];
                string dataFromClient = null;
                //string board = "000000000";



                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 + " Joined chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
Ejemplo n.º 4
0
        //TODO: FIX BUG WHEN CLIENT DISCONNECTS, HANDLE IT CORRECTLY.
        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)bytesFrom.Length);
                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();
        }
Ejemplo n.º 5
0
        private static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Title = hostname;
            #region Port Setup
            var port = 8888;
            var check = false;
            string port_awnser = "";
            do
            {
                Console.WriteLine("Welcome to kwskii's Server Chat!\n " +
                              "\n" +
                              "Do you want the server to initialize at port 8888(Y\\N)");
                port_awnser = Console.ReadLine().ToUpper();
                check = validateAwnser(new[] {"Y", "N"}, port_awnser);
                if (check && port_awnser.StartsWith("Y"))
                {
                    Console.WriteLine("Ok starting server at " + port);
                }
                else if (!check)
                {   
                    Console.Clear();
                    Console.WriteLine("That's not a valid awnser!\nPlease enter Y or N: ");
                }
            } while (!check);

            char awnser = port_awnser[0];

            if (awnser == 'N')
            {
               Console.WriteLine("Choose your custom port for the server");
                bool isNumeric = false;
                do
                {
                    port_awnser = Console.ReadLine().ToUpper();
                    isNumeric = int.TryParse(port_awnser, out port);
                    if (isNumeric)
                        Console.WriteLine("Ok starting server at " + port);
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("That's not a valid number!\nPlease enter your chosen port: ");
                    }
                } while (!isNumeric);
            }
#endregion
            portNumber = port;

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

            Console.Clear();
            for (int dots = 0; dots <= 5; ++dots)
            {   if (dots == 0)
                    serverSocket.Start();
                Console.Write("\r{0}Launching Server{0}", new string('*', dots));
                System.Threading.Thread.Sleep(100);
            }

            Console.WriteLine("\n\nServer is up and running!" +
                              "\nHostname : " + hostname +
                              "\nPort : " + portNumber +
                              "\nIP : " + GetPublicIp());


            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);

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

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