Ejemplo n.º 1
0
        private static void Server()
        {
            Console.WriteLine("[Server] Starting Server.");
            TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 2081);

            tcpListener.Start();
            var ent = tcpListener.AcceptTcpClient();

            ServerClient = new SnooperSocketClient()
            {
                Client = ent
            };
            ServerClient.OnDisconnect += ServerClient_OnDisconnect;
            ServerClient.SetSecurityProtocal(new MutualKeyProtocal()
            {
                Key = k, IsServer = true
            });
            ServerClient.Start();

            ServerClient.Channels["Request"].MessageReceived += ServerRequest_Message;
            ServerClient.Channels["$SnooperSec.MutualKeyProtocal.Validate"].MessageReceived += Program_MessageReceived;;
            ServerClient.Channels["Request"].RequestReceived += ServerRequest_Request;

            Console.WriteLine($"[Server] Authed: {ServerClient.Security.ValidateConnection()}");
        }
Ejemplo n.º 2
0
        private static void Client()
        {
            Console.WriteLine("[Client] Connecting to server...");
            TcpClient Client = new TcpClient();

            Console.WriteLine($"[Client] Network Buffer Size: {Client.ReceiveBufferSize}");
            Client.Connect(IPAddress.Parse("127.0.0.1"), 2081);
            Console.WriteLine("[Client] Connected.");
            LocalClient = new SnooperSocketClient()
            {
                Client = Client
            };
            LocalClient.OnDisconnect += LocalClient_OnDisconnect;
            LocalClient.SetSecurityProtocal(new MutualKeyProtocal()
            {
                Key = k, IsServer = false
            });
            LocalClient.Start();
            Console.WriteLine($"[Client] Requesting Auth...");
            Console.WriteLine($"[Client] Authed: {((MutualKeyProtocal)LocalClient.Security).ValidateConnection()}");

            Console.WriteLine("[Client] Sending on open stream");
            LocalClient.Write(new MSGDat()
            {
                Content = "HEYO DERE BOIO!"
            }, null, "$SnooperSec.MutualKeyProtocal.Validate");

            //while(true)
            //{
            //    Console.ReadLine();
            //    LocalClient.Write(new MSGDat() { Content = "HEY!" }, null, "Request");
            //}
            Console.WriteLine("DC...");
            LocalClient.Disconnect();
        }
Ejemplo n.º 3
0
        private static void HandleNewClient(TcpClient Client)
        {
            SnooperSocketClient SocketClient = new SnooperSocketClient(Client);

            SocketClient.SetSecurityProtocal(new MutualKeyProtocal()
            {
                IsServer = true, Key = Password
            });
            SocketClient.Start();
            Thread.Sleep(400);
            if (SocketClient.Security.TryValidateConnection())
            {
                Console.WriteLine("[Server] Client Authed!");
                SocketClient.Channels["Message"].Write(new StatusMessage()
                {
                    Message = "Hello!", Status = true
                });
                SocketClient.Disconnect();
                Client.Dispose();
            }
            else
            {
                Console.WriteLine("[Server] Client Denied.");
                SocketClient.Disconnect();
                Client.Dispose();
            }
        }
Ejemplo n.º 4
0
 private static void Listen()
 {
     while (true)
     {
         TcpClient           NewTCPClient = Listener.AcceptTcpClient();
         SnooperSocketClient NewClient    = new SnooperSocketClient()
         {
             Client = NewTCPClient
         };
         NewClient.Start();
         Clients.Join(NewClient, new userProfile());
         NewClient.OnDisconnect += ClientDisconnected;
     }
 }
Ejemplo n.º 5
0
        public static void Run()
        {
            while (true)
            {
                Console.Write("Password: "******"Message"].MessageReceived += Client_MessageReceived;
                SocketClient.Start();
                //if (SocketClient.Security.TryValidateConnection())
                //{
                //    Console.WriteLine($"[Client] Authorised");

                //    Console.WriteLine("Press any to continue...");
                //    Console.ReadKey();

                //} else
                //{
                //    Console.WriteLine($"[Client] Auth Denied.");
                //}


                Console.WriteLine("Press any to continue...");
                Console.ReadKey();

                Console.WriteLine("[Client] Disconnecting...");
                SocketClient.Disconnect();

                Client.Dispose();
                Console.WriteLine("\n");
            }
        }
Ejemplo n.º 6
0
        private static void Main(string[] args)
        {
            Console.Write("Client/Server [C/S] ");
            string inp = Console.ReadLine();

            Console.Write("Server Address: ");
            IP = Console.ReadLine();
            Console.Write("Port: ");
            MPort = Convert.ToInt32(Console.ReadLine());
            IPEndPoint EP = new IPEndPoint(IPAddress.Parse(IP), MPort);

            if (inp.ToLower() == "s")
            {
                Console.WriteLine("Starting local server...");
                Server.StartServer(EP);
                IPEndPoint LEP     = new IPEndPoint(IPAddress.Parse("127.0.0.1"), MPort);
                TcpClient  TClient = new TcpClient();
                Console.WriteLine("Connecting to local server...");
                TClient.Connect(LEP);
                Console.WriteLine("Conencted.");
                Client = new SnooperSocketClient()
                {
                    Client = TClient
                };
            }
            else
            {
                TcpClient TClient = new TcpClient();
                Console.WriteLine("Connecting to remote server...");
                TClient.Connect(EP);
                Console.WriteLine("Conencted.");
                Client = new SnooperSocketClient()
                {
                    Client = TClient
                };
            }
            Client.Channels["Messages"].MessageReceived += OnMessage;
            Client.Start();
            Console.Write("Username: "******"Login");
            while (true)
            {
                string msg = Console.ReadLine();

                if (msg == "$disconnect")
                {
                    break;
                }
                else if (msg == "$clients")
                {
                    if (Server.Active)
                    {
                        foreach (var cl in Server.Clients)
                        {
                            var a = Server.Clients.GetClientData(cl);
                            Console.WriteLine($"Username: {a.Username}, Logged in: {a.LoggedIn}. IP: {cl.Client.Client.RemoteEndPoint}");
                        }
                        continue;
                    }
                }
                Client.Write(new ClientMessage()
                {
                    Message = msg
                }, null, "Messages");
            }
            Client.Write(new object(), null, "Logout");
            Client.Flush();
            Client.Client.Close();
        }
Ejemplo n.º 7
0
        /// For a better example project, see SSocketChatTest. This is also a simple chat program, however it also uses SnooperSocketClientPools and SnooperPoolChannels to allow for multiple clients.
        /// In tests, with 100 active clients, the server still had 0% idle CPU and only 38MB of RAM usage.

        private static void Main(string[] args)
        {
            Console.Write("SERVER/CLIENT [S/C]");
            string inp = Console.ReadLine();

            if (inp.ToLower() == "s")
            {
                Console.WriteLine("Starting Server...");
                TcpListener Listener = new TcpListener(EP);
                Listener.Start();
                Console.WriteLine("Waiting for client...");
                TCPClient = Listener.AcceptTcpClient();
                Console.WriteLine("Client Connected.");
            }
            else
            {
                Console.WriteLine("Conecting...");
                TCPClient = new TcpClient();
                TCPClient.Connect(EP);
                Console.WriteLine("Connected.");
            }
            Console.WriteLine($"Client: {ClientName != null}");
            Socket = new SnooperSocketClient()
            {
                Client = TCPClient
            };
            LoginData LD = new LoginData()
            {
                Joining = true
            };

            Console.Write("Username: "******"Login");
            Socket.Channels["Login"].MessageReceived    += UserJoined;
            Socket.Channels["Messages"].MessageReceived += OnMessage;
            Socket.Start();

            while (true)
            {
                string msg = Console.ReadLine();

                if (msg == "$exit")
                {
                    LoginData LDD = new LoginData()
                    {
                        Joining = false
                    };
                    Console.Write("Username: "******"Login");
                }
                else
                {
                    Message MSG = new Message()
                    {
                        MessageContent = msg
                    };
                    Socket.Write(MSG, null, "Messages");
                }
            }
        }