I'm currently working on a small program for home use (mainly because I'm too lazy to run down the stairs every time I want to tell my dad something).
Now as for my question:
How can I get this code to call my ReceiveText
method as soon as the udpclient receives a message from a peer? (Note: There's only going to be 2 pc's running this, having both ip's hardcoded into it)
The above question has meanwhile been answered thanks to Ahmed Ilyas, however I've run into a snag which is featured on the link he posted:
The message is being sent just fine like this:
sendMe.Connect("192.168.0.121", 60500);
sendBytes = Encoding.ASCII.GetBytes(txtSend.Text);
sendMe.Send(sendBytes, sendBytes.Length);
sendMe.Close();
The problem seems to lie at the receiving end, from what I read from the exception it seems to be occurring when I try to actually use the data that was sent so I can have it properly formatted etc. for my Rich Text Box.
Here's the code which receives & parses the incoming data:
UdpClient sendMe = new UdpClient(60500);
UdpClient illReceive = new UdpClient(60501);
Byte[] sendBytes;
string returnData;
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
...
try
{
illReceive.BeginReceive(new AsyncCallback(RecieveText), null);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
...
void ReceiveText(IAsyncResult res)
{
try
{
System.Reflection.Assembly a = _
System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("IPSend.ns.wav");
SoundPlayer player = new SoundPlayer(s);
player.Play();
byte[] received = illReceive.EndReceive(res, ref RemoteIpEndPoint);
returnData = Encoding.UTF8.GetString(received);
TextRange tr = new _
TextRange(rtbPast.Document.ContentEnd, rtbPast.Document.ContentEnd);
tr.Text = String.Format("{0} - Yorrick: {1} {2}", _
DateTime.Now.ToShortTimeString(), returnData, Environment.NewLine);
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Black);
illReceive.BeginReceive(new AsyncCallback(RecieveText), null);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
PS: I'm using the MessageBox.Show for now, simply so I can clearly take a look at the exception thrown, from what I understand from those, it seems to be a problem somewhere in the ReceiveText
method.
The exception being thrown:
(Translation for the 1st line: "The call thread can not open this object because it is owned by another thread.")
New exception after trying C.Evenhuis' code (After changing it for WPF):
And the code behind this exception:
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 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);
}
PS: If you need to take a look at my entire code, I've posted it on Pastebin, mainly because it's a bit too long to actually post it in here: Entire code on Pastebin