Ejemplo n.º 1
0
        public static void StartListening()
        {
            LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: Server starting"); });

            //IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());//Metod 1
            //IPAddress ipAddress = ipHostInfo.AddressList[0];

            IPAddress[] ipHostInfo = GetHostIPv4();//Metod 2
            IPAddress   ipAddress  = ipHostInfo[0];

            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 50000);

            Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);
                LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: Port binded"); });

                ShowAndTell.Show.Start();
                while (true)
                {
                    allDone.Reset();

                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    allDone.WaitOne();
                }
            } catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Ejemplo n.º 2
0
        private static void Menager(Packet packet, StateObject state)
        {
            if (packet.PacketType == Packet.Packettype.Message && state.nick != null)
            {
                string text = packet.Data[0];
                LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: Message from " + state.nick + " contains: " + text); });

                text = state.nick + ": " + text;
                Packet toSend = new Packet(Packet.Packettype.Message);
                toSend.Data.Add(text);
                Broadcast(toSend);
            }
            else if (packet.PacketType == Packet.Packettype.Registration)
            {
                Packet toSend = new Packet(Packet.Packettype.Registration);
                string text   = packet.Data[0];

                if (!NickExist(text))
                {
                    state.nick = text;
                    toSend.Data.Add("Valid");
                    LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: New registration - " + state.nick); });
                }
                else
                {
                    //Send new request
                    toSend.Data.Add("Invalid");
                    LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: Invalit registration - " + text); });
                }

                Send(state, toSend);
            }
        }
Ejemplo n.º 3
0
        public static void AcceptCallback(IAsyncResult backinfo)
        {
            allDone.Set();

            Socket listner = (Socket)backinfo.AsyncState;
            Socket handler = listner.EndAccept(backinfo);

            LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: New client accepted"); });

            StateObject state = new StateObject(); //Create a client object

            state.workSocket = handler;            //Pass the Socket

            lock (_lock) _clients.Add(state);

            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReciveCallback), state);
        }
Ejemplo n.º 4
0
 private static void ConnectionClose(StateObject state)
 {
     state.workSocket.Close();
     lock (_lock) _clients.Remove(state);
     LANChat.App.Current.Dispatcher.Invoke(() => { ChatPanelServer.AddLogPanel("Log[" + DateTime.Now + "]: Client disconnected - " + state.nick); });
 }