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.Threading.Tasks;
- 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;
- using System.Net.Sockets;
- using System.Media;
- using System.Windows.Threading;
- namespace IPSend
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- UdpClient sendMe = new UdpClient(60501);
- UdpClient illReceive = new UdpClient(60500);
- Byte[] sendBytes;
- string returnData;
- IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
- public MainWindow()
- {
- InitializeComponent();
- TextRange tr = new TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
- tr.Text = "Messenger";
- tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
- tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.White);
- rtbPast.AppendText(Environment.NewLine);
- flowDoc.Blocks.FirstBlock.TextAlignment = TextAlignment.Center;
- try
- {
- illReceive.BeginReceive(new AsyncCallback(ReceiveText), null);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- private void txtSend_GotFocus(object sender, RoutedEventArgs e)
- {
- txtSend.Text = "";
- }
- private void txtSend_LostFocus(object sender, RoutedEventArgs e)
- {
- if (txtSend.Text == "") txtSend.Text = "Plaats je bericht hier.";
- }
- private void btnSend_Click(object sender, RoutedEventArgs e)
- {
- if (txtSend.Text != "")
- {
- sendMsg();
- txtSend.Text = "Plaats je bericht hier.";
- }
- }
- private void txtSend_KeyDown_1(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Return)
- {
- btnSend.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
- txtSend.RaiseEvent(new RoutedEventArgs(TextBox.GotFocusEvent));
- }
- }
- void ReceiveText(IAsyncResult res)
- {
- try
- {
- byte[] received = illReceive.EndReceive(res, ref RemoteIpEndPoint);
- returnData = Encoding.UTF8.GetString(received);
- InformUser(returnData);
- illReceive.BeginReceive(new AsyncCallback(ReceiveText), null);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- void sendMsg()
- {
- try
- {
- sendMe.Connect("192.168.0.219", 60501);
- // Sends a message to the host to which you have connected.
- sendBytes = Encoding.UTF8.GetBytes(txtSend.Text);
- sendMe.Send(sendBytes, sendBytes.Length);
- sendMe.Close();
- TextRange tr = new TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
- tr.Text = String.Format("{0} - Jij: {1} {2}", DateTime.Now.ToShortTimeString(), txtSend.Text, Environment.NewLine);
- tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
- tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Black);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- void InformUser(string data)
- {
- // InvokeRequired tells you you're not on the correct thread to update the rtbPast
- if (rtbPast.Dispatcher.CheckAccess())
- {
- // Call InformUser(data) again, but on the correct thread
- rtbPast.Dispatcher.Invoke(new Action<string>(InformUser), data);
- // We're done for this thread
- return;
- }
- System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
- System.IO.Stream s = a.GetManifestResourceStream("IPSend.ns.wav");
- SoundPlayer player = new SoundPlayer(s);
- player.Play();
- TextRange tr = new TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
- tr.Text = String.Format("{0} - Pa: {1} {2}", DateTime.Now.ToShortTimeString(), data, Environment.NewLine);
- tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
- tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Black);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement