Beispiel #1
0
        static public string StartServer()
        {
            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the
            // host running the application.
            string      hostName   = Dns.GetHostName();
            IPHostEntry ipHostInfo = Dns.Resolve(hostName); // obsolete but it works

            if (CSavedIPs.Subs == "")
            {
                string subs = "";
                foreach (IPAddress i in ipHostInfo.AddressList)
                {
                    if (subs != "")
                    {
                        subs += ",";
                    }
                    string ip  = i.ToString();
                    int    loc = ip.IndexOf('.');
                    loc   = ip.IndexOf('.', loc + 1);
                    loc   = ip.IndexOf('.', loc + 1);
                    subs += ip.Substring(0, loc);
                }
                CSavedIPs.ChangeSubList(subs);
            }

            Task.Factory.StartNew(() => StartListening(ipHostInfo));

            return(CSavedIPs.Subs);
        }
Beispiel #2
0
 private void buttonGoConnection_Click(object sender, EventArgs e)
 {
     CSavedIPs.ChangeSubList(this.textBoxSubs.Text);
     Sock.SetSock(this.userName, this.myIP, this.textBoxSubs.Text);
     Sock.StartSearching(this);
     appendText(richTextBoxChatOut, "Searching..." + Environment.NewLine, Color.LightGreen);
     this.buttonGoConnection.Enabled = false;
 }
Beispiel #3
0
        private void Chatter_FormClosing(object sender, FormClosingEventArgs e)
        {
            CSavedIPs.ChangeSubList(this.textBoxSubs.Text);
            this.isExit = true;
            // temp file cleanup
            try
            {
                foreach (string key in linkList.Keys)
                {
                    if (File.Exists(linkList[key]))
                    {
                        File.Delete(linkList[key]);
                    }
                }
            }
            catch
            { }

            Sock.KillTasks();
        }
Beispiel #4
0
        public Chatter()
        {
            InitializeComponent();

            CSavedIPs.ReadFile();

            this.comboBoxUsers.SelectedIndex = 0;

            userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            this.groupBoxTop.Text = "Name: " + userName;

            this.textBoxSubs.Text = Sock.StartServer();

            string[] split   = this.textBoxSubs.Text.Split(',');
            string   ipFirst = (split != null && split.Count() > 0) ? split[0] : "10.0.0";

            Sock.NewData            += Sock_NewDataReceivedEventHandler;
            Sock.Ack                += Sock_Ack;
            myIP                     = Sock.GetIPAddress(ipFirst);
            this.groupBoxBottom.Text = "My IP: " + myIP;
        }
Beispiel #5
0
        static private void messageHandling(CMessageHandler m, MessageEventArgs mea)
        {
            if (mea == null)
            {
            }
            else if (mea.Valid && !String.IsNullOrWhiteSpace(mea.TextFromFriend))
            {
                ipBuddyFullNameLookup[mea.FriendIP] = mea.FriendName;
                OnNewDataReceived(mea);
                OnAckReceived(mea);

                // Update good ip list as needed
                CSavedIPs.AppendIP(mea.FriendIP);

                // Signal that all bytes have been received.
                m.ReceiveDone.Set();
            }
            else
            {
                OnAckReceived(mea);
            }
        }
Beispiel #6
0
        static public void StartClientSide(Socket sender, string buddyIp)
        {
            beginThread();

            debug("Client thread launched");
            int errors = 0;

            try
            {
#if !DEBUG_LOOPBACK
                if (!checkForRunningThread(buddyIp))
                {
                    endThread();
                    debug("Client ignored this ip: " + buddyIp);
                    return;
                }
#endif


                if (!ipBuddyMessages.ContainsKey(buddyIp))
                {
                    ipBuddyMessages.Add(buddyIp, new Queue <string>());
                }

                if (!ipBuddyIsTyping.ContainsKey(buddyIp))
                {
                    ipBuddyIsTyping.Add(buddyIp, 0);
                }

                ipBuddyMessages[buddyIp].Enqueue(CMessageHandler.GenerateMessage(myPCName, myIP, "Connected!"));

                CMessageHandler m = new CMessageHandler("ClientMH", buddyIp, myIP);

                // Enter the working loop
                while (stayAlive)
                {
                    if (ipBuddyMessages[buddyIp].Count > 0)
                    {
                        lock (ipBuddyMessages[buddyIp])
                        {
                            try
                            {
                                m.AddMessageToSend(ipBuddyMessages[buddyIp].Dequeue());
                            }
                            catch { }
                        }
                    }

                    MessageEventArgs         mea;
                    CMessageHandler.MsgState state = m.ProcessStates(out mea);
                    messageHandling(m, mea);

                    // Write to Server as needed
                    if (state == CMessageHandler.MsgState.ReadyForRemote || state == CMessageHandler.MsgState.SendAck)
                    {
                        debug("Client sent out msg to " + buddyIp);
                        send(sender, m.MessageReady, m);
                    }
                    else
                    {
                        if (myTypingStatus > 0)
                        {
                            send(sender, "\t", m);
                        }
                        else
                        {
                            send(sender, " ", m);
                        }
                        Thread.Sleep(150); // slow down pinging
                    }

                    if (!receive(sender, m))
                    {
                        if (errors++ >= 3)
                        {
                            debug("Client error " + buddyIp);
                            break;
                        }
                    }
                    else
                    {
                        errors = 0;
                    }

                    CSavedIPs.AppendIP(buddyIp);
                }

                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch (Exception e)
            {
                debug("Client exception for " + buddyIp + e.ToString());
                if (_debug != null && ipBuddyFullNameLookup.ContainsKey(buddyIp))
                {
                    _debug.InjectTestMessage("User aborted: " + ipBuddyFullNameLookup[buddyIp]);
                }
            }

            lock (ipsInUse)
            {
                if (ipsInUse.Contains(buddyIp))
                {
                    ipsInUse.Remove(buddyIp);
                }
            }
            //lock (ipBuddyMessages)
            //{
            //    if (ipBuddyMessages.ContainsKey(buddyIp))
            //        ipBuddyMessages.Remove(buddyIp);
            //}

            endThread();
        }
Beispiel #7
0
        static private void StartServerSide(Socket handler, string buddyIp)
        {
            beginThread();

            Thread.Sleep(50);
            debug("Server thread launched");
            int errors = 0;

            try
            {
                CMessageHandler m = new CMessageHandler("ServerMH", buddyIp, myIP);

                if (!ipBuddyMessages.ContainsKey(buddyIp))
                {
                    ipBuddyMessages.Add(buddyIp, new Queue <string>());
                }

                if (!ipBuddyIsTyping.ContainsKey(buddyIp))
                {
                    ipBuddyIsTyping.Add(buddyIp, 0);
                }

                ipBuddyMessages[buddyIp].Enqueue(CMessageHandler.GenerateMessage(myPCName, myIP, "Connected!"));

                handler.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false);

                // An incoming connection needs to be processed.
                while (stayAlive)
                {
                    if (ipBuddyMessages[buddyIp].Count > 0)
                    {
                        lock (ipBuddyMessages[buddyIp])
                        {
                            try
                            {
                                m.AddMessageToSend(ipBuddyMessages[buddyIp].Dequeue());
                            }
                            catch { }
                        }
                    }

                    MessageEventArgs         mea;
                    CMessageHandler.MsgState state = m.ProcessStates(out mea);
                    messageHandling(m, mea);

                    // Write to Client as needed
                    if (state == CMessageHandler.MsgState.ReadyForRemote || state == CMessageHandler.MsgState.SendAck)
                    {
                        debug("Server sent out msg to " + buddyIp);

                        send(handler, m.MessageReady, m);
                    }
                    else
                    {
                        if (myTypingStatus > 0)
                        {
                            send(handler, "\t", m);
                        }
                        else
                        {
                            send(handler, " ", m);
                        }
                        Thread.Sleep(150); // slow down pinging
                    }

                    if (!receive(handler, m))
                    {
                        if (errors++ >= 3)
                        {
                            debug("Server error " + buddyIp);
                            break;
                        }
                    }
                    else
                    {
                        errors = 0;
                    }

                    CSavedIPs.AppendIP(buddyIp);
                }
            }
            catch (Exception e)
            {
                debug("Server exception for " + buddyIp + e.ToString());
                if (_debug != null && ipBuddyFullNameLookup.ContainsKey(buddyIp))
                {
                    _debug.InjectTestMessage("User aborted: " + ipBuddyFullNameLookup[buddyIp]);
                }
            }

            if (handler != null)
            {
                handler.Shutdown(SocketShutdown.Both);
                handler.Disconnect(false);
                handler.Close();
                //handler.Dispose();
            }


            lock (ipsInUse)
            {
                if (ipsInUse.Contains(buddyIp))
                {
                    ipsInUse.Remove(buddyIp);
                }
            }
            //lock (ipBuddyMessages)
            //{
            //    if (ipBuddyMessages.ContainsKey(buddyIp))
            //        ipBuddyMessages.Remove(buddyIp);
            //}

            endThread();
        }