C # UDP Communication

Source: Internet
Author: User

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.sockets;using System.Net;using System.threading;namespace windowsformsapplication1{public partial class Form1:form {private UdpClient SE        Ndudpclient;        Private UdpClient receiveupdclient;            Public Form1 () {InitializeComponent ();            ipaddress[] ips = dns.gethostaddresses (""); Tbxlocalip. Text = Ips[0].            ToString ();            int port = 51883; Tbxlocalport.text = port.            ToString (); Tbxsendtoip.text = Ips[0].           ToString (); Tbxsendtoport.text = port.        ToString ();  private void Form1_Load (object sender, EventArgs e) {control.checkforillegalcrossthreadcalls =        False } private void Button1_Click (object sender, EventArgs e) {if (Tbxmessagesend.text = = sTring.                Empty) {MessageBox.Show ("Send content cannot be empty", "hint");            Return                 }//Select Send mode if (chkbxanonymous.checked = = true) {//anonymous mode (socket bound port is randomly assigned by system)            Sendudpclient = new UdpClient (0); } else {//real-name mode (socket bound to locally specified port) IPAddress Localip = Ipaddress.parse (TB Xlocalip.                Text); IPEndPoint localipendpoint = new IPEndPoint (localip, Int.                Parse (Tbxlocalport.text));            Sendudpclient = new UdpClient (localipendpoint);            } Thread Sendthread = new Thread (SendMessage);        Sendthread.start (Tbxmessagesend.text);            } private void SendMessage (Object obj) {string message = (string) obj;            byte[] sendbytes = Encoding.Unicode.GetBytes (message);            IPAddress Remoteip = Ipaddress.parse (Tbxsendtoip.text); IPEndPoint Remoteipendpoint = new IPEndPoint (REMOTEIP, Int.            Parse (Tbxsendtoport.text)); Sendudpclient.send (Sendbytes, sendbytes.            Length, Remoteipendpoint);            Sendudpclient.close ();        Empty Send message box//resetmessagetext (tbxmessagesend);        } delegate void Showmessageforviewcallback (ListBox ListBox, string text); private void Showmessageforview (ListBox ListBox, string text) {if (listbox.                invokerequired) {Showmessageforviewcallback showmessageforviewcallback = Showmessageforview; ListBox.            Invoke (Showmessageforviewcallback, new object[] {listbox, text});                } else {lstbxMessageView.Items.Add (text);                Lstbxmessageview.selectedindex = lstbxmessageview.items.count-1;            Lstbxmessageview.clearselected ();        }}//delegate void Resetmessagecallback (textbox textbox); private void Resetmessagetext (TextBox textbOX)//{/////Control.invokerequired property represents////True if the control's processing is created on a different thread than the calling thread, otherwise false// if (textbox.        invokerequired)//{//Resetmessagecallback resetmessagecallback = Resetmessagetext; Textbox.        Invoke (Resetmessagecallback, new object[] {textbox}); }//else//{//textbox.        Clear (); Textbox.        Focus ();            }//} private void Button4_Click (object sender, EventArgs e) {//Create receive socket IPAddress Localip = Ipaddress.parse (tbxlocalip.            Text); IPEndPoint localipendpoint = new IPEndPoint (localip, Int.            Parse (Tbxlocalport.text));            Receiveupdclient = new UdpClient (localipendpoint);            Thread receivethread = new Thread (ReceiveMessage);        Receivethread.start (); } private void ReceiveMessage () {IPEndPoint remoteipendpoint = new IPEndPoint (ipaddresS.any, 0);                    while (true) {try {//close receiveudpclient When an exception is generated                    byte[] Receivebytes = receiveupdclient.receive (ref remoteipendpoint);                    String message = Encoding.Unicode.GetString (receivebytes); Displays the message content Showmessageforview (Lstbxmessageview, String.                Format ("{0}[{1}]", Remoteipendpoint, message));                } catch {break; }}} private void Button3_Click (object sender, EventArgs e) {receiveupdclient .        Close ();        } private void Button2_Click (object sender, EventArgs e) {this.lstbxMessageView.Items.Clear (); }    }}

  

C # UDP Communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.