Example #1
0
        /// <summary>
        /// Funktion zum Verarbeiten einer Server-Anfrage
        /// </summary>
        /// <param name="message">Nachricht des Servers</param>
        private void handleMessage(string message)
        {
            NonBlockingConsole.WriteLine("Starting Handling");
            switch (message.Substring(0, 1))
            {
            case "c":
                int    x            = int.Parse(message.Substring(1, 1));
                int    y            = int.Parse(message.Substring(2, 1));
                Button currentField = field(x, y);
                this.Invoke((MethodInvoker) delegate
                {
                    currentField.Text = message.Substring(3, 1);
                });
                break;

            case "p":
                setButtonsEnabled(true);
                this.Invoke((MethodInvoker) delegate
                {
                    toolStripStatusLabel.Text = "Bereit";
                });
                break;

            case "e":
                setButtonsEnabled(false);
                this.Invoke((MethodInvoker) delegate
                {
                    toolStripStatusLabel.Text = "Spiel beendet";
                });
                endGame(message.Substring(1, 1));
                break;

            default:
                this.Invoke((MethodInvoker) delegate
                {
                    toolStripStatusLabel.Text = "Kommunikationsfehler";
                });
                break;
            }
            NonBlockingConsole.WriteLine("Finished Handling");
        }
Example #2
0
 private void buttonVerbinden_Click(object sender, EventArgs e)
 {
     initSocket(ref socket);
     if (myIp == null)
     {
         using (var form = new IpChooserDialog(getLocalIPs()))
         {
             var result = form.ShowDialog();
             myIp = form.chosenAddress;
             port = int.Parse(textBoxPort.Text.ToString());
             IPEndPoint epLocal = new IPEndPoint(myIp, localPort);
             socket.Bind(epLocal);
         }
     }
     try
     {
         string    remoteIp        = textBoxIp.Text.ToString();
         IPAddress ipAddressremote = IPAddress.Parse(remoteIp);
         remoteEP = new IPEndPoint(ipAddressremote, port);
         socket.Connect(remoteEP);
         NonBlockingConsole.WriteLine("Connected to remote EP " + remoteEP.ToString());
         byte[] buffer = new byte[1024];
         socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(MessageCallBack), buffer);
         SendPlayerName(textBoxName.Text.ToString(), ref socket);
         buttonVerbinden.Enabled   = false;
         toolStripStatusLabel.Text = "Warten";
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString() + "\nVerbindung fehlgeschlagen.");
         this.Invoke((MethodInvoker) delegate
         {
             toolStripStatusLabel.Text = "Fehler";
         });
     }
 }