Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Net.Sockets;
- using System.Net;
- using System.ComponentModel;
- namespace ViewMySclient
- {
- /// <summary>
- /// Interaktionslogik fĂźr MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- //##############
- BackgroundWorker bw = new BackgroundWorker();
- BackgroundWorker bw2 = new BackgroundWorker();
- IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
- bool ipset = false;
- //##############
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (this.textBox2.Text == "")
- {
- MessageBox.Show("No Broadcast address!");
- return;
- }
- WorkStart();
- BroadCastSend();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error" + Environment.NewLine + Environment.NewLine + ex + Environment.NewLine + Environment.NewLine, "Error!");
- }
- }
- public void BroadCastSend()
- {
- try
- {
- string TextBoxer = "";
- string IPBoxer = "";
- Dispatcher.Invoke(new Action(() => TextBoxer = this.textBox1.Text));
- Dispatcher.Invoke(new Action(() => IPBoxer = this.textBox2.Text));
- if (ipset == true)
- {
- TextBoxer = "ip " + textBox3.Text;
- ipset = false;
- }
- string myIP = Convert.ToString(Host.AddressList[1]);
- byte[] myWriteBuffer = Encoding.ASCII.GetBytes(TextBoxer);
- //Socket definieren
- Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- //EndPoint definieren bzw. Ziel des Broadcastes
- IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse(IPBoxer), 24710);
- //Optionen auf den Socket binden
- bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
- //Broadcast senden
- bcSocket.SendTo(myWriteBuffer, iep1);
- //Socket schliessen, nach erfolgreichem Senden des Broadcastes
- bcSocket.Close();
- bcSocket.Dispose();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error!" + Environment.NewLine + Environment.NewLine + ex);
- return;
- }
- }
- private void WorkStart()
- {
- bw.DoWork += new DoWorkEventHandler(bw_DoWork);
- bw.RunWorkerAsync();
- }
- private void bw_DoWork(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker worker = sender as BackgroundWorker;
- bw.WorkerSupportsCancellation = true;
- BroadCastSend();
- }
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- button1_Click(this, new RoutedEventArgs());
- }
- }
- private void WorkStart2()
- {
- try
- {
- bw2.DoWork += new DoWorkEventHandler(bw_DoWork2);
- bw2.RunWorkerAsync();
- }
- catch (Exception)
- {
- return; //Damit bei mehrfachem klicken keine Fehlermeldung auftritt.
- }
- }
- private void bw_DoWork2(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker worker = sender as BackgroundWorker;
- bw2.WorkerSupportsCancellation = true;
- Receive();
- }
- public void Receive()
- {
- try
- {
- Socket sock = new Socket(AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp);
- IPEndPoint iep = new IPEndPoint(IPAddress.Any, 24711);
- sock.Bind(iep);
- EndPoint ep = (EndPoint)iep;
- byte[] data = new byte[1024];
- int recv = sock.ReceiveFrom(data, ref ep);
- string stringData = Encoding.ASCII.GetString(data, 0, recv);
- data = new byte[1024];
- recv = sock.ReceiveFrom(data, ref ep);
- stringData = Encoding.ASCII.GetString(data, 0, recv);
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(stringData)));
- sock.Close();
- sock.Dispose();
- Receive();
- // return;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error" + Environment.NewLine + Environment.NewLine + ex);
- }
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- WorkStart2();
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- if (this.textBox3.IsEnabled == true)
- {
- this.textBox3.IsEnabled = false;
- ipset = true;
- BroadCastSend();
- return;
- }
- this.textBox3.IsEnabled = true;
- ipset = false;
- }
- private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- button1_Click(this, new RoutedEventArgs());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement