예제 #1
0
        public void Start()
        {
            //Socket is responsible for the connection between the server & client
            Socket socket;
            Packet packetSend = new Packet();

            packetSend.nickname = nickname;

            socket = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp
                );


            Console.WriteLine("Connecting To The Server...");
            //socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddress), 420));
            socket.Connect(new IPEndPoint(ipToConnect, portToConnect));
            Console.WriteLine("Connected To The Server!");
            socket.Blocking = false;

            /*Console.WriteLine("Enter your nick name");
             * packetSend.nickname = Console.ReadLine();*/

            socket.Send(ASCIIEncoding.ASCII.GetBytes(nickname));


            int colorNumber = ChatColorsClass.PrintGetColorInfoFromUser();

            packetSend.textColor = ChatColorsClass.GetIndexOfColor(colorNumber);

            Console.WriteLine("Type your message...");

            while (true)
            {
                try
                {
                    if (Console.KeyAvailable)
                    {
                        Console.ForegroundColor = packetSend.textColor;
                        ConsoleKeyInfo key = Console.ReadKey();

                        if (key.Key == ConsoleKey.Enter)
                        {
                            packetSend.message = packetSend.nickname + ": " + packetSend.message;
                            socket.Send(BinaryFormatterClass.ObjectToByteArray(packetSend));
                            Console.WriteLine(packetSend.message);
                            packetSend.message = "";
                        }
                        else
                        {
                            if (key.Key == ConsoleKey.Backspace)
                            {
                                if (packetSend.message.Length >= 1)
                                {
                                    packetSend.message = packetSend.message.Remove(packetSend.message.Length - 1, 1);
                                    Console.Write($"\r{new string(' ', (Console.WindowWidth - 1))}\r");
                                    Console.Write($"\r{packetSend.message}");
                                }
                            }
                            else
                            {
                                packetSend.message += key.KeyChar;
                            }
                        }
                    }

                    //RECIEVING THE MESSAGE

                    Byte[] recieveBuffer = new byte[1024];
                    int    receivedBytes = socket.Receive(recieveBuffer);
                    Packet packetRecieve = (Packet)BinaryFormatterClass.ByteArrayToObject(recieveBuffer);

                    Console.Write($"\r{new string(' ',(Console.WindowWidth - 1))}\r");

                    Console.ForegroundColor = packetRecieve.textColor;
                    Console.WriteLine(packetRecieve.message);

                    Console.ForegroundColor = packetSend.textColor;
                    Console.WriteLine(packetSend.message);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode != SocketError.WouldBlock)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }
        }
예제 #2
0
        public void Start()
        {
            //Listening Socket
            listeningSocket = new Socket(
                AddressFamily.InterNetwork,         //IPV4
                SocketType.Stream,                  //Type Stream-2way connection
                ProtocolType.Tcp                    //Protocol TCP
                );

            listeningSocket.Blocking = false;

            //bind to the client
            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, port));
            Console.WriteLine("Waiting For Connection...");
            listeningSocket.Listen(10); //listen

            while (true)
            {
                try //keep trying to listen
                {
                    Socket clientSocket  = listeningSocket.Accept();
                    Byte[] recieveBuffer = new byte[1024];
                    int    receivedBytes = clientSocket.Receive(recieveBuffer);
                    string nickname      = ASCIIEncoding.ASCII.GetString(recieveBuffer);
                    nickname = nickname.Substring(0, receivedBytes);
                    Console.WriteLine(nickname + " has connected to the server!");

                    clientSocketNickname.Add(clientSocket, nickname);
                    clientSockets.Add(clientSocket);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode != SocketError.WouldBlock)
                    {
                        Console.WriteLine(ex);
                    }
                }

                for (int i = 0; i < clientSockets.Count; i++)
                {
                    try
                    {
                        Byte[] recieveBuffer = new byte[1024];
                        int    received      = clientSockets[i].Receive(recieveBuffer);

                        for (int j = 0; j < clientSockets.Count; j++)
                        {
                            if (i != j)
                            {
                                clientSockets[j].Send(recieveBuffer, received, SocketFlags.None);
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.ConnectionAborted ||
                            ex.SocketErrorCode == SocketError.ConnectionReset)
                        {
                            //SENDING TO SERVER
                            Socket disconnectedSocket = clientSockets[i];
                            string nickname           = clientSocketNickname[disconnectedSocket];
                            Console.WriteLine(nickname + " Disconnected!");

                            clientSockets[i].Close();
                            clientSocketNickname.Remove(disconnectedSocket);
                            clientSockets.RemoveAt(i);

                            //SENDING TO CLIENTS
                            Packet disconnectedPacket = new Packet();
                            disconnectedPacket.nickname  = "Server";
                            disconnectedPacket.message   = nickname + " Disconnected!";
                            disconnectedPacket.textColor = ConsoleColor.Red;

                            for (int j = 0; j < clientSockets.Count; j++)
                            {
                                clientSockets[j].Send(BinaryFormatterClass.ObjectToByteArray(disconnectedPacket)); //??
                            }
                        }

                        if (ex.SocketErrorCode != SocketError.WouldBlock)
                        {
                            if (ex.SocketErrorCode != SocketError.ConnectionAborted ||
                                ex.SocketErrorCode != SocketError.ConnectionReset)
                            {
                                Console.WriteLine(ex);
                            }
                        }
                    }
                }
            }
        }