Παναγιώτη ευχαριστώ για τη λεπτομερέστατη εξήγηση και τα σχόλια. Παρ'όλα αυτά επειδή είμαι newbie .net προγραμματιστής μου σκάει το πρόγραμμα όταν στέλνω δεδομένα με έναν test udp client. Συγκεκριμένα σκάει με αυτό το μήνυμα. Τι εννοεί οτι το αντικείμενο αυτό ανοίκει σε διαφορετικό thread? Ο κώδικας μου είναι σε ένα :Window μέσα και ο listener ξεκινάει σε ένα κουμπι. console έχω ονομάσει ένα textbox control που χρησιμοποιώ για να παρουσιάζω διάφορα μηνύματα.
o κώδικας μου βρίσκεται εδω:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 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.Windows.Media.Media3D;
using WpfCubeControl; using System.Drawing;
namespace Wpf3DApp { /// <summary> /// Interaction logic for CubeWin.xaml /// </summary> public partial class CubeWin : Window { public class UdpState { public UdpClient Client { get; set; } public IPEndPoint EndPoint { get; set; } }
IPEndPoint point = new IPEndPoint(IPAddress.Loopback, 9091); UdpClient listener;
public CubeWin() {
InitializeComponent();
cubeControl1.CubeColor = System.Windows.Media.Color.FromRgb(255, 70, 0); cubeControl1.Render(); }
private void ReceiveCallback(IAsyncResult ar) { UdpState s = (UdpState)ar.AsyncState; UdpClient listener = s.Client; IPEndPoint endpoint = s.EndPoint;
if (listener.Client != null) {
byte[] message = listener.EndReceive(ar, ref endpoint); string receiveString = Encoding.Unicode.GetString(message); SetText(receiveString, message.Length); listener.BeginReceive(this.ReceiveCallback, s); } } delegate void SetTextCallback(string text, int length);
private void startbutton_Click(object sender, RoutedEventArgs e) { if (listener == null) listener = new UdpClient(9091); UdpState state = new UdpState { Client = listener, EndPoint = point }; listener.BeginReceive(this.ReceiveCallback, state); console.Text += "Listening for connections on port 9091"; }
private void SetText(string text, int length) { console.Text += String.Format("\r\n{0} - {1} bytes", text, length);
}
} } |
Jubei