Exemplo n.º 1
0
        private void Connectbutton_Click(object sender, EventArgs e)
        {
            client = new TcpClient();
            IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(ClientIPtextbox.Text), int.Parse(ClientPorttextbox.Text));

            noOfConnections++;
            txtConnections.Text = noOfConnections.ToString();
            try
            {
                client.Connect(IpEnd);

                if (client.Connected)
                {
                    ChatScreentextbox.AppendText("Connected to server" + "\n");

                    STW           = new StreamWriter(client.GetStream());
                    STR           = new StreamReader(client.GetStream());
                    STW.AutoFlush = true;
                    backgroundWorker1.RunWorkerAsync();
                    backgroundWorker2.WorkerSupportsCancellation = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
Exemplo n.º 2
0
        // allows the user to connect to a hosting/server IP address in order to enter the chatri
        private void Connectbutton_Click(object sender, EventArgs e)
        {
            client = new TcpClient(); // the variable client is overwritten with a new instance of TcpClient

            // Represents a network endpoint as an IP address and a port number.
            IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(ClientIPtextbox.Text), int.Parse(ClientPorttextbox.Text));

            try
            {
                client.Connect(IpEnd);

                if (client.Connected)
                {
                    // Lets the user know that they have connected to the local server
                    ChatScreentextbox.AppendText("Connected to server" + "\n");
                    noOfConnections++;
                    txtConnections.Text      = noOfConnections.ToString();
                    myStreamWriter           = new StreamWriter(client.GetStream());
                    myStreamReader           = new StreamReader(client.GetStream());
                    myStreamWriter.AutoFlush = true;                     // flushes the buffer
                    backgroundWorker1.RunWorkerAsync();                  // starts backgroundWorker1
                    backgroundWorker2.WorkerSupportsCancellation = true; // allows backgroundworker2 to be cancelled asynchronously
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
Exemplo n.º 3
0
 private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
 {
     if (client.Connected)
     {
         STW.WriteLine(TextToSend);
         this.ChatScreentextbox.Invoke(new MethodInvoker(delegate()
         {
             ChatScreentextbox.AppendText("Player:" + TextToSend + "\n");
         }));
     }
     else
     {
         MessageBox.Show("Sending failed");
     }
     backgroundWorker2.CancelAsync();
 }
Exemplo n.º 4
0
        // Starts a server/hosting service for other clients to connect
        private void Startbutton_Click(object sender, EventArgs e)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(ServerPorttextbox.Text)); // uses the IP address in the textbox to be listened to

            listener.Start();                                                                         // starts listening if its an IP address
            client                   = listener.AcceptTcpClient();                                    // accepts clients listening on IP address
            myStreamReader           = new StreamReader(client.GetStream());
            myStreamWriter           = new StreamWriter(client.GetStream());
            myStreamWriter.AutoFlush = true;

            backgroundWorker1.RunWorkerAsync();
            backgroundWorker2.WorkerSupportsCancellation = true;

            // increases number of connections and informs server side that a user is in the chatroom
            noOfConnections++;
            txtConnections.Text = noOfConnections.ToString();
            ChatScreentextbox.AppendText("A user has connected!");
        }
Exemplo n.º 5
0
 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
 {
     while (client.Connected)
     {
         try
         {
             recieve = STR.ReadLine();
             this.ChatScreentextbox.Invoke(new MethodInvoker(delegate()
             {
                 ChatScreentextbox.AppendText("Opponent:" + recieve + "\n");
             }));
             recieve = "";
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message.ToString());
         }
     }
 }
Exemplo n.º 6
0
 private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
 {
     // if you've connected to the server
     if (client.Connected)
     {
         //enables easy and efficient text output
         myStreamWriter.WriteLine(messageSend);
         this.ChatScreentextbox.Invoke(new MethodInvoker(delegate()
         {
             // send message stating who sent it and their message
             ChatScreentextbox.AppendText("player:" + messageSend + "\n");
         }));
     }
     else
     {
         // error handling message
         MessageBox.Show("SENDING MESSAGE FAILED...");
     }
     backgroundWorker2.CancelAsync();
 }
Exemplo n.º 7
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //
            while (client.Connected)
            {
                try
                {
                    // Stream
                    messageRecieve = myStreamReader.ReadLine();

                    this.ChatScreentextbox.Invoke(new MethodInvoker(delegate()
                    {
                        // send message stating who sent it and their message -
                        ChatScreentextbox.AppendText("opponent:" + messageRecieve + "\n");
                    }));
                    messageRecieve = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }