コード例 #1
0
 private void btnConnect_Click(object sender, EventArgs e)
 {
     // If we are not currently connected but awaiting to connect
     if (Connected == false)
     {
         // Initialize the connection
         InitializeConnection();
     }
     else // We are connected, thus disconnect
     {
         CloseConnection("Disconnected from Chat Server");
         liUsers = new List <string>();
         ClientForm.SetControlPropertyThreadSafe(this.onlineUsers, "DataSource", new List <String>());
     }
 }
コード例 #2
0
        private void ReceiveMessages()
        {
            // Receive the response from the server
            srReceiver = new StreamReader(tcpServer.GetStream());
            // If the first character of the response is 1, connection was successful
            string ConResponse = srReceiver.ReadLine();

            // If the first character is a 1, connection was successful
            if (ConResponse[0] == '1')
            {
                // Update the form to tell it we are now connected
                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "Connected Successfully!" });
                String      hostName = Dns.GetHostName();
                IPAddress[] addr     = Dns.GetHostEntry(hostName).AddressList;
                foreach (IPAddress a in addr)
                {
                    if (a.ToString().Split('.')[0] == ipAddr.ToString().Split('.')[0])
                    {
                        myIp = a.ToString();
                    }
                }
            }
            else // If the first character is not a 1 (probably a 0), the connection was unsuccessful
            {
                string Reason = "Not Connected: ";
                // Extract the reason out of the response message. The reason starts at the 3rd character
                Reason += ConResponse.Split('\"')[1];
                // Update the form with the reason why we couldn't connect
                this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason });
                // Exit the method
                return;
            }
            // While we are successfully connected, read incoming lines from the server
            while (Connected)
            {
                // Show the messages in the log TextBox
                try
                {
                    if (tcpServer.Available > 0)
                    {
                        String   res    = srReceiver.ReadLine();
                        String[] tokens = res.Split('\"');
                        switch (tokens[0])
                        {
                        case "":
                            switch (tokens[1])
                            {
                            case "1":
                                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { tokens[2] });

                                break;

                            case "2":
                                for (int i = 2; i < tokens.Length; i++)
                                {
                                    if (!liUsers.Contains(tokens[i]) && this.UserName != tokens[i])
                                    {
                                        liUsers.Add(tokens[i]);
                                        this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { tokens[i] + " has joined the server." });
                                    }
                                }
                                ClientForm.SetControlPropertyThreadSafe(this.onlineUsers, "DataSource", new List <String>());
                                ClientForm.SetControlPropertyThreadSafe(this.onlineUsers, "DataSource", liUsers);
                                break;

                            case "3":
                                if (liUsers.Contains(tokens[2]))
                                {
                                    liUsers.Remove(tokens[2]);
                                }
                                ClientForm.SetControlPropertyThreadSafe(this.onlineUsers, "DataSource", new List <String>());
                                ClientForm.SetControlPropertyThreadSafe(this.onlineUsers, "DataSource", liUsers);
                                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { tokens[2] + " has left the server." });
                                break;
                            }
                            break;

                        default:
                            switch (tokens[1])
                            {
                            case "1":
                                if (privateWindows.Contains(tokens[0]))
                                {
                                    try
                                    {
                                        ((PrivateChat)privateWindows[tokens[0]]).recievedMessage(tokens[2]);
                                    }
                                    catch
                                    {
                                        //MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK);
                                        privateWindows.Remove(tokens[0]);
                                        this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "*" + tokens[0] + ": " + tokens[2] });
                                    }
                                }
                                else
                                {
                                    this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "*" + tokens[0] + ": " + tokens[2] });
                                }
                                //((PrivateChat)privateWindows[tokens[0]]).recievedMessage(tokens[2]);
                                break;

                            case "4":
                                if (ChatClient.ClientForm.serverBusy == true)
                                {
                                    MessageBox.Show("A request from " + tokens[0] + " was cancelled as the server is already busy!");
                                    break;
                                }
                                switch (tokens[6])
                                {
                                case "0":
                                    string[] filePath = tokens[4].Split('\\');
                                    string   fileName = filePath[filePath.Length - 1];
                                    if (MessageBox.Show("Recieve file " + fileName + " from User " + tokens[0] + "?",
                                                        "File Transfer Requested", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                    {
                                        this.Invoke(new FileRecieveCallback(this.startFileRecieve), new object[] { res });
                                    }
                                    break;

                                case "1":
                                    if (MessageBox.Show("Recieve Seismic Cube from " + tokens[0] + "?",
                                                        "Seismic Cube Transfer Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                    {
                                        this.Invoke(new FileRecieveCallback(this.startFileRecieve), new object[] { res });
                                    }
                                    break;
                                }

                                break;

                            case "5":
                                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "Started sending file..." });
                                FTSender sender = new FTSender(tokens[2], tokens[3], tokens[4], this);
                                Thread   thread = new Thread(new ThreadStart(sender.SendFile));
                                thread.Start();
                                break;

                            case "7":
                                GeoCommunication.FTSender.recieved = true;
                                break;

                            case "o":           // For recieving objects
                                this.Invoke(new FileRecieveCallback(this.startObjectRecieve), new object[] { res });
                                break;

                            case "O":
                                ObjSender sender2 = new ObjSender(tokens[3], tokens[4], sendObject);
                                Thread    thread2 = new Thread(new ThreadStart(sender2.SendObject));
                                thread2.Start();
                                break;

                            default:
                                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { res });
                                break;
                            }
                            break;
                        }
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine("Disconnected.." + e.ToString());
                }
            }
        }