Advertisement
Guest User

Client

a guest
Nov 8th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Net.Sockets;
  15. using System.Net;
  16. using System.ComponentModel;
  17.  
  18. namespace ViewMySclient
  19. {
  20.     /// <summary>
  21.     /// Interaktionslogik fĂźr MainWindow.xaml
  22.     /// </summary>
  23.     public partial class MainWindow : Window
  24.     {
  25.         public MainWindow()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         //##############
  31.         BackgroundWorker bw = new BackgroundWorker();
  32.         BackgroundWorker bw2 = new BackgroundWorker();
  33.         IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
  34.         bool ipset = false;
  35.         //##############
  36.  
  37.  
  38.  
  39.         private void button1_Click(object sender, RoutedEventArgs e)
  40.         {
  41.             try
  42.             {
  43.                 if (this.textBox2.Text == "")
  44.                 {
  45.                     MessageBox.Show("No Broadcast address!");
  46.                     return;
  47.                 }
  48.                 WorkStart();
  49.                 BroadCastSend();
  50.             }
  51.             catch (Exception ex)
  52.             {
  53.                 MessageBox.Show("Error" + Environment.NewLine + Environment.NewLine + ex + Environment.NewLine + Environment.NewLine, "Error!");
  54.             }
  55.         }
  56.  
  57.         public void BroadCastSend()
  58.         {
  59.             try
  60.             {
  61.                 string TextBoxer = "";
  62.                 string IPBoxer = "";
  63.                 Dispatcher.Invoke(new Action(() => TextBoxer = this.textBox1.Text));
  64.                 Dispatcher.Invoke(new Action(() => IPBoxer = this.textBox2.Text));
  65.                 if (ipset == true)
  66.                 {
  67.                     TextBoxer = "ip " + textBox3.Text;
  68.                     ipset = false;
  69.                 }
  70.                 string myIP = Convert.ToString(Host.AddressList[1]);
  71.  
  72.                 byte[] myWriteBuffer = Encoding.ASCII.GetBytes(TextBoxer);
  73.                 //Socket definieren
  74.                 Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  75.  
  76.                 //EndPoint definieren bzw. Ziel des Broadcastes
  77.                 IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse(IPBoxer), 24710);
  78.  
  79.                 //Optionen auf den Socket binden
  80.                 bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  81.  
  82.                 //Broadcast senden
  83.                 bcSocket.SendTo(myWriteBuffer, iep1);
  84.                
  85.                 //Socket schliessen, nach erfolgreichem Senden des Broadcastes
  86.                 bcSocket.Close();
  87.                 bcSocket.Dispose();
  88.             }
  89.             catch (Exception ex)
  90.             {
  91.                 MessageBox.Show("Error!" + Environment.NewLine + Environment.NewLine + ex);
  92.                 return;
  93.             }
  94.         }
  95.  
  96.         private void WorkStart()
  97.         {
  98.             bw.DoWork += new DoWorkEventHandler(bw_DoWork);
  99.             bw.RunWorkerAsync();
  100.         }
  101.  
  102.         private void bw_DoWork(object sender, DoWorkEventArgs e)
  103.         {
  104.             BackgroundWorker worker = sender as BackgroundWorker;
  105.             bw.WorkerSupportsCancellation = true;
  106.             BroadCastSend();
  107.  
  108.         }
  109.  
  110.  
  111.  
  112.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  113.         {
  114.             if (e.Key == Key.Enter)
  115.             {
  116.  
  117.  
  118.                 button1_Click(this, new RoutedEventArgs());
  119.  
  120.             }
  121.         }
  122.  
  123.  
  124.         private void WorkStart2()
  125.         {
  126.             try
  127.             {
  128.                 bw2.DoWork += new DoWorkEventHandler(bw_DoWork2);
  129.                 bw2.RunWorkerAsync();
  130.             }
  131.             catch (Exception)
  132.             {
  133.                 return;  //Damit bei mehrfachem klicken keine Fehlermeldung auftritt.
  134.             }
  135.         }
  136.  
  137.  
  138.         private void bw_DoWork2(object sender, DoWorkEventArgs e)
  139.         {
  140.             BackgroundWorker worker = sender as BackgroundWorker;
  141.             bw2.WorkerSupportsCancellation = true;
  142.             Receive();
  143.  
  144.         }
  145.  
  146.         public void Receive()
  147.         {
  148.             try
  149.             {
  150.                 Socket sock = new Socket(AddressFamily.InterNetwork,
  151.                            SocketType.Dgram, ProtocolType.Udp);
  152.                 IPEndPoint iep = new IPEndPoint(IPAddress.Any, 24711);
  153.                 sock.Bind(iep);
  154.                 EndPoint ep = (EndPoint)iep;
  155.  
  156.                 byte[] data = new byte[1024];
  157.                 int recv = sock.ReceiveFrom(data, ref ep);
  158.                 string stringData = Encoding.ASCII.GetString(data, 0, recv);
  159.  
  160.                 data = new byte[1024];
  161.                 recv = sock.ReceiveFrom(data, ref ep);
  162.                 stringData = Encoding.ASCII.GetString(data, 0, recv);
  163.                 Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(stringData)));
  164.                 sock.Close();
  165.                 sock.Dispose();
  166.                 Receive();
  167.                 // return;
  168.             }
  169.             catch (Exception ex)
  170.             {
  171.                 MessageBox.Show("Error" + Environment.NewLine + Environment.NewLine + ex);
  172.             }
  173.  
  174.         }
  175.  
  176.         private void Window_Loaded(object sender, RoutedEventArgs e)
  177.         {
  178.             WorkStart2();
  179.         }
  180.  
  181.         private void button2_Click(object sender, RoutedEventArgs e)
  182.         {
  183.             if (this.textBox3.IsEnabled == true)
  184.             {
  185.                 this.textBox3.IsEnabled = false;
  186.                 ipset = true;
  187.                 BroadCastSend();
  188.                 return;
  189.             }
  190.             this.textBox3.IsEnabled = true;
  191.             ipset = false;
  192.  
  193.         }
  194.  
  195.         private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
  196.         {
  197.             if (e.Key == Key.Enter)
  198.             {
  199.  
  200.  
  201.                 button1_Click(this, new RoutedEventArgs());
  202.  
  203.             }
  204.         }
  205.  
  206.  
  207.  
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement