2010-01-18 9 views
0

Je cherchais depuis longtemps une solution à ce problème. J'ai essayé beaucoup de choses, y compris BeginReceive(), mais tout en vain. Il doit y avoir un moyen de faire cela, faire un UDP-Client recevoir un appel qui n'est pas bloquant ET thread sécurisé. J'essaie de recevoir un message et de l'écrire dans une zone de texte enrichi.Le client UDP non-bloquant reçoit un appel sécurisé-filaire

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using System.IO; 

namespace Chat 
{ 
    public partial class Form1 : Form 
    { 
     public bool contfunctioning = true; public bool changedsplay = false; 
     public string enteredchats, msg, chatstring, Chats; 

     UdpClient subscriber = new UdpClient(8899); 
     UdpClient publisher = new UdpClient("230.0.0.100", 8898); 


     public Form1() 
     { 
      InitializeComponent(); 
     } 


     private void btnConnect_Click(object sender, EventArgs e) 
     { 

      string ConnectIPAddress; 
      ConnectIPAddress = txtboxIP.Text; 
      IPAddress addr = IPAddress.Parse(ConnectIPAddress); 

      MessageBox.Show("Subscribing to chat server on " + ConnectIPAddress + ".", ConnectIPAddress); 

      EndPoint ep = null; 

      // This is where The UDPClient subscriber needs to Begin.Receive() 


     } 

     private void btnSend_Click(object sender, EventArgs e) 
     { 
      enteredchats = txtboxUsr.Text + " " + txtboxentertxt.Text; 
      txtboxentertxt.Clear(); 

      msg = String.Format(enteredchats); 
      sdata = Encoding.ASCII.GetBytes(msg); 
      publisher.Send(sdata, sdata.Length); 
     } 

    } 
} 

Assez de code, oui? Merci d'avance pour toutes les réponses.

+0

Comment avez-vous essayé 'BeginReceive'? C'est thread-safe si vous l'utilisez correctement. –

+0

Quel problème avez-vous exactement? Utiliser BeginReceive fonctionnerait parfaitement ici. –

+0

Est-ce moi ou est-ce que l'extrait de code contient BeginReceive quelque part? –

Répondre

2

J'ai trouvé que l'utilisation d'un Timer() m'a permis de contourner ce problème.

using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.Net.Sockets; 
using System.IO; 
using System.Threading; 
using System.Diagnostics; 


namespace Chat 
{ 
    public partial class Form1 : Form 
    { 


     public bool contfunctioning = true; public bool changedsplay = false; public bool setupntwrk = false; 
     public string enteredchats, msg, chatstring, Chats, lastchatstring; 

     UdpClient subscriber = new UdpClient(8899); 
     UdpClient publisher = new UdpClient("230.0.0.100", 8898); 

     System.Windows.Forms.Timer timer1use = new System.Windows.Forms.Timer(); 



     public Form1() 
     { 
      InitializeComponent(); 
     } 



     private void btnConnect_Click(object sender, EventArgs e) 
     { 
      Thread rcvchats = new Thread(ReceiveChats); 
      rcvchats.Start(); 

      timer1use.Interval = 1000; 
      timer1use.Start(); 
     } 

     private void btnSend_Click(object sender, EventArgs e) 
     { 
      enteredchats = txtboxUsr.Text + ": " + txtboxentertxt.Text; 
      txtboxentertxt.Clear(); 

      msg = String.Format(enteredchats); 
      byte[] sdata = Encoding.ASCII.GetBytes(msg); 
      publisher.Send(sdata, sdata.Length); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      if (chatstring != lastchatstring) 
      dsplay.AppendText("\r\n" + chatstring); 

      lastchatstring = chatstring; 


     } 

     public void ReceiveChats() 
     { 
      while (true) 
      { 
       if (setupntwrk == false) 
       { 
        string ConnectIPAddress; 
        ConnectIPAddress = txtboxIP.Text; 
        IPAddress addr = IPAddress.Parse(ConnectIPAddress); 
        MessageBox.Show("Subscribing to chat server on " + ConnectIPAddress + ".", ConnectIPAddress); 
        subscriber.JoinMulticastGroup(addr); 

        setupntwrk = true; 
       } 

       IPEndPoint ep = null; 
       chatstring = Encoding.ASCII.GetString(subscriber.Receive(ref ep)); 

       Thread.Sleep(1000); 
      } 

     } 

     private void btnHost_Click(object sender, EventArgs e) 
     { 
      Process starthost = new Process(); 
      starthost.StartInfo.FileName = "C:\\ChatServ.exe"; 
      starthost.Start(); 

     } 
    } 
} 
+4

Pourquoi la downvote? C'est légal de répondre à ma propre question, en fait, il y a un badge pour ça. – Bloodyaugust