Advertisement
Guest User

Untitled

a guest
Oct 29th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Net;
  16. using System.Net.Sockets;
  17. using System.Media;
  18. using System.Windows.Threading;
  19.  
  20. namespace IPSend
  21. {
  22.     /// <summary>
  23.     /// Interaction logic for MainWindow.xaml
  24.     /// </summary>
  25.     public partial class MainWindow : Window
  26.     {
  27.         UdpClient sendMe = new UdpClient(60501);
  28.         UdpClient illReceive = new UdpClient(60500);
  29.         Byte[] sendBytes;
  30.         string returnData;
  31.         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  32.  
  33.         public MainWindow()
  34.         {
  35.             InitializeComponent();
  36.             TextRange tr = new TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
  37.             tr.Text = "Messenger";
  38.             tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
  39.             tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.White);
  40.             rtbPast.AppendText(Environment.NewLine);
  41.             flowDoc.Blocks.FirstBlock.TextAlignment = TextAlignment.Center;
  42.             try
  43.             {
  44.                 illReceive.BeginReceive(new AsyncCallback(ReceiveText), null);
  45.             }
  46.             catch (Exception e)
  47.             {
  48.                 MessageBox.Show(e.ToString());
  49.             }
  50.         }
  51.  
  52.         private void txtSend_GotFocus(object sender, RoutedEventArgs e)
  53.         {
  54.             txtSend.Text = "";
  55.         }
  56.  
  57.         private void txtSend_LostFocus(object sender, RoutedEventArgs e)
  58.         {
  59.             if (txtSend.Text == "") txtSend.Text = "Plaats je bericht hier.";
  60.         }
  61.  
  62.         private void btnSend_Click(object sender, RoutedEventArgs e)
  63.         {
  64.             if (txtSend.Text != "")
  65.             {
  66.                 sendMsg();
  67.                 txtSend.Text = "Plaats je bericht hier.";
  68.             }
  69.         }
  70.  
  71.         private void txtSend_KeyDown_1(object sender, KeyEventArgs e)
  72.         {
  73.             if (e.Key == Key.Return)
  74.             {
  75.                 btnSend.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
  76.                 txtSend.RaiseEvent(new RoutedEventArgs(TextBox.GotFocusEvent));
  77.             }
  78.         }
  79.  
  80.         void ReceiveText(IAsyncResult res)
  81.         {
  82.             try
  83.             {
  84.                 byte[] received = illReceive.EndReceive(res, ref RemoteIpEndPoint);
  85.                 returnData = Encoding.UTF8.GetString(received);
  86.  
  87.                 InformUser(returnData);
  88.  
  89.                 illReceive.BeginReceive(new AsyncCallback(ReceiveText), null);
  90.             }
  91.             catch (Exception e)
  92.             {
  93.                 MessageBox.Show(e.ToString());
  94.             }
  95.         }
  96.  
  97.         void sendMsg()
  98.         {
  99.             try
  100.             {
  101.                 sendMe.Connect("192.168.0.219", 60501);
  102.  
  103.                 // Sends a message to the host to which you have connected.
  104.                 sendBytes = Encoding.UTF8.GetBytes(txtSend.Text);
  105.                 sendMe.Send(sendBytes, sendBytes.Length);
  106.                 sendMe.Close();
  107.  
  108.                 TextRange tr = new TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
  109.                 tr.Text = String.Format("{0} - Jij: {1} {2}", DateTime.Now.ToShortTimeString(), txtSend.Text, Environment.NewLine);
  110.                 tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
  111.                 tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Black);
  112.             }
  113.             catch (Exception e)
  114.             {
  115.                 MessageBox.Show(e.ToString());
  116.             }
  117.         }
  118.  
  119.         void InformUser(string data)
  120.         {
  121.             // InvokeRequired tells you you're not on the correct thread to update the rtbPast
  122.             if (rtbPast.Dispatcher.CheckAccess())
  123.             {
  124.                 // Call InformUser(data) again, but on the correct thread
  125.                 rtbPast.Dispatcher.Invoke(new Action<string>(InformUser), data);
  126.  
  127.                 // We're done for this thread
  128.                 return;
  129.             }
  130.  
  131.             System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
  132.             System.IO.Stream s = a.GetManifestResourceStream("IPSend.ns.wav");
  133.             SoundPlayer player = new SoundPlayer(s);
  134.             player.Play();
  135.  
  136.             TextRange tr = new TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
  137.             tr.Text = String.Format("{0} - Pa: {1} {2}", DateTime.Now.ToShortTimeString(), data, Environment.NewLine);
  138.             tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
  139.             tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Black);
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement