Exemple #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try {
                sendDone.Set();
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                StateObject state  = (StateObject)ar.AsyncState;
                Socket      client = state.workSocket;
                state.sb = new StringBuilder();
                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));

                    string content = state.sb.ToString();

                    if (content.IndexOf("}end") > -1)
                    {
                        // All the data has been read from the
                        // client. Display it on the console.
                        Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
                        // Echo the data back to the client.
                        int begin = content.IndexOf("beg{");
                        int ende  = content.IndexOf("}end");

                        string contentOhneHeaderUndTailer = "";
                        for (int j = begin + 4; j < ende; j++)
                        {
                            contentOhneHeaderUndTailer += content[j];
                        }
                        int aktion = -1;

                        aktion = Int32.Parse("" + contentOhneHeaderUndTailer[0]);
                        if (aktion == (int)aktionEnum.chatMessage)
                        {
                            string[] aufgeteilteNachricht = content.Split('☻');
                            string   empfangenerChatText  = aufgeteilteNachricht[3];
                            foreach (StateObject item in Form1.chatObjekte)
                            {
                                if (item.peerName.Contains(aufgeteilteNachricht[1]))
                                {
                                    try {
                                        item.chatForm.Invoke((MethodInvoker) delegate {
                                            item.chatForm.Activate();
                                            item.chatForm.chatText.Text           = item.chatForm.chatText.Text + Environment.NewLine + Environment.NewLine + aufgeteilteNachricht[1] + ": " + empfangenerChatText;
                                            item.chatForm.chatText.SelectionStart = item.chatForm.chatText.Text.Length;
                                            item.chatForm.chatText.ScrollToCaret();
                                        });
                                    } catch (Exception ex) {
                                        Console.WriteLine(ex.ToString());
                                        try {
                                            ChatForm chatformNeu = new ChatForm();
                                            chatformNeu.Text          = "Chat mit " + aufgeteilteNachricht[1];
                                            item.chatForm             = chatformNeu;
                                            chatformNeu.chatText.Text = item.chatForm.chatText.Text + Environment.NewLine + Environment.NewLine + aufgeteilteNachricht[1] + ": " + empfangenerChatText;
                                            chatformNeu.ShowDialog();
                                            item.chatForm.Activate();
                                            //Application.Run(chatform);
                                        } catch (Exception ex2) {
                                            Console.WriteLine(ex2.ToString());
                                        }
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
        private void chatButton_Click(object sender, EventArgs e)
        {
            string NameDesAusgewaehltenPeers = "";

            try{
                NameDesAusgewaehltenPeers = peersListe.SelectedItems[0].SubItems[1].Text;
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }

            if (IpOfSelectedPeer.Equals("") || NameDesAusgewaehltenPeers.Equals(""))
            {
                MessageBox.Show("Es wurde kein Peer ausgewählt...", "Achtung", MessageBoxButtons.OK);
                return;
            }



            bool        gefunden  = false;
            StateObject tmpObjekt = null;

            foreach (StateObject item in chatObjekte)
            {
                if (item.peerName.Equals(NameDesAusgewaehltenPeers))
                {
                    tmpObjekt = item;
                    gefunden  = true;
                    break;
                }
            }

            if (gefunden)
            {
                //ChatForm chatform = new ChatForm();
                try {
                    ChatForm chatformNeu = new ChatForm();
                    chatformNeu.Text   = "Chat mit " + tmpObjekt.peerName;
                    tmpObjekt.chatForm = chatformNeu;
                    chatformNeu.Show();

                    //Application.Run(chatform);
                } catch (Exception ex) {
                    Console.WriteLine(ex.ToString());
                }

                //chatform.Text = "Chat mit " + tmpObjekt.peerName;
                //chatform.Show();
                //tmpObjekt.chatForm = chatform;
            }
            else
            {
                ChatForm chatform = new ChatForm();
                chatform.Text = "Chat mit " + NameDesAusgewaehltenPeers;
                chatform.Show();
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(IpOfSelectedPeer), peersPort);
                Socket     client   = new Socket(SocketType.Stream, ProtocolType.Tcp);

                client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
                //connectDone.WaitOne();

                StateObject state = new StateObject();
                state.workSocket = client;
                state.chatForm   = chatform;
                state.peerName   = NameDesAusgewaehltenPeers;
                chatObjekte.Add(state);
                Thread.Sleep(100);
                try {
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackChat), state);
                } catch (Exception ex) {
                    Console.WriteLine(ex.ToString());
                }

                Thread.Sleep(100);
                //sendDone.WaitOne();
                sendThreadChat(client);
            }
        }