예제 #1
0
 private static void Disconnect()
 {
     stream?.Close();
     client?.Close();
     Environment.Exit(0);
 }
예제 #2
0
 static void Disconnect()
 {
     reader?.Close();
     writer?.Close();
     client?.Close();
 }
예제 #3
0
 public void Close()
 {
     stream?.Close();
     client?.Close();
 }
예제 #4
0
 /// <summary>
 /// Closes the stream and the client connection once the window gets closed / exited.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     stream?.Close();
     client?.Close();
 }
예제 #5
0
파일: Form1.cs 프로젝트: Team-on/works
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     stream?.Close();
     tcp?.Close();
 }
예제 #6
0
        private void getMessage()
        {
            try
            {
                while (true)
                {
                    serverStream = clientSocket.GetStream();
                    byte[] inStream = new byte[10025];
                    serverStream.Read(inStream, 0, inStream.Length);
                    List <string> parts = null;

                    if (!SocketConnected(clientSocket))
                    {
                        MessageBox.Show("You've been Disconnected");
                        ctThread.Abort();
                        clientSocket.Close();
                        btnConnect.Enabled = true;
                    }

                    parts = (List <string>)ByteArrayToObject(inStream);
                    switch (parts[0])
                    {
                    case "userList":
                        getUsers(parts);
                        break;

                    case "gChat":
                        readData = "" + parts[1];
                        msg();
                        break;

                    case "pChat":
                        managePrivateChat(parts);
                        break;
                    }

                    if (readData[0].Equals('\0'))
                    {
                        readData = "Reconnect Again";
                        msg();

                        this.Invoke((MethodInvoker) delegate // To Write the Received data
                        {
                            btnConnect.Enabled = true;
                        });

                        ctThread.Abort();
                        clientSocket.Close();
                        break;
                    }
                    chat.Clear();
                }
            }
            catch (Exception e)
            {
                ctThread.Abort();
                clientSocket.Close();
                btnConnect.Enabled = true;
                Console.WriteLine(e);
            }
        }
예제 #7
0
        public void RunClient()
        {
            TcpClient client = null;

            //instantiate TcpClient for sending data to server
            try
            {
                //Create TcpClient for sending data to server
                client = new TcpClient();
                client.Connect("127.0.0.1", 50000);

                //Get NetworkStream associated with TctClient
                output = client.GetStream();

                //Create objects for writing and reading across stream
                writer = new BinaryWriter(output);
                reader = new BinaryReader(output);


                //Loop until server signals termination
                do
                {
                    //Processing phase
                    try
                    {
                        //Read message from server
                        message = reader.ReadString();
                        if (message.Equals("SERVER >>> Successful Authentication"))
                        {
                            HideLoginPanels();
                            ShowOperationsPanel();
                        }
                        //Check user permissions
                        if (message.Equals("SERVER >>> Cannot make encryption from guest account."))
                        {
                            MessageBox.Show("Cannot make encryptions from guest account.", "Unauthorized Operation");
                        }
                        //Check user card
                        if (message.Equals("SERVER >>> Invalid card number"))
                        {
                            MessageBox.Show("The card number you have entered is incorrect. Check if this is your card number.", "Invalid Card Number");
                        }
                        //Chech number of times user have encrypted his card
                        if (message.Equals("SERVER >>> Cannot make more than 12 encryptions"))
                        {
                            MessageBox.Show("You cannot make more than 12 ecryptions per card.", "Invalid Operation");
                        }
                        //Check permissions for decrypt
                        if (message.Equals("SERVER >>> Cannot decrypt this card."))
                        {
                            MessageBox.Show("You have no permissions to decrypt this card. Check if this is your card encryption.", "Invalid Operation");
                        }
                        //Get encryption/Decryption
                        if (Regex.IsMatch(message, @"^[0-9]+$"))
                        {
                            DisplayOutput(message);
                        }
                    }
                    catch (Exception)
                    {
                        System.Environment.Exit(System.Environment.ExitCode);
                    }
                } while (message != "SERVER>>> TERMINATE");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString(), "Connection Error",
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                //Close connection
                writer?.Close();
                reader?.Close();
                output?.Close();
                client?.Close();

                System.Environment.Exit(System.Environment.ExitCode);
            }
        }
예제 #8
0
 public void unConnect()
 {
     tc.Close();
 }