C # simple UDP Communication Example and code

Source: Internet
Author: User

1. UDP client

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;namespace WindowsFormsApplication14{    public partial class Form1 : Form    {        UdpClient udpClient;        IPEndPoint ipEndPoint;        public Form1()        {            InitializeComponent();            udpClient = new UdpClient(12345);            ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.241"), 60000);        }        private void button1_Click(object sender, EventArgs e)        {            //byte[] mybyte = Encoding.Default.GetBytes("nihao");                        byte[] mybyte = new byte[4];            mybyte[0] = 0x12;            mybyte[1] = 0x00;            mybyte[2] = 0x34;            mybyte[3] = 0x00;                        udpClient.Send(mybyte, mybyte.Length,ipEndPoint);        }    }}

2. UDP Server

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;namespace WindowsFormsApplication15{    public partial class Form1 : Form    {        UdpClient udpServer;        IPEndPoint ipEndPoint;        public Form1()        {            InitializeComponent();            udpServer = new UdpClient(23456);            ipEndPoint = new IPEndPoint(new IPAddress(0), 0);        }        private void button1_Click(object sender, EventArgs e)        {                        byte[] data = udpServer.Receive(ref ipEndPoint);            MessageBox.Show ( Encoding.Default.GetString(data));        }    }}

3. Number of current data in the read buffer

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; namespace WindowsFormsApplication15 {public partial class Form1: Form {UdpClient udpServer; IPEndPoint ipEndPoint; public Form1 () {InitializeComponent (); udpServer = new UdpClient (23456 ); ipEndPoint = new IPEndPoint (new IPAddress (0), 0);} private void button#click (object sender, EventArgs e) {byte [] outValue = BitConverter. getBytes (0); udpServer. client. IOControl (IOControlCode. dataToRead, null, outValue); MessageBox. show (BitConverter. toInt32 (outValue, 0 )). toString (); // when sending data, it is found that the data is growing, but not more than 8 K. // MessageBox. show (BitConverter. toString (outValue )). toString ());}}}

 

4. The above example is a little complicated. Let's look at the example below. This example also shows how to set up non-blocking Socket communication.

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; namespace WindowsFormsApplication15 {public partial class Form1: Form {UdpClient udpServer; IPEndPoint ipEndPoint; public Form1 () {InitializeComponent (); udpServer = new UdpClient (23456 ); ipEndPoint = new IPEndPoint (new IPAddress (0), 0); udpServer. client. blocking = false; // set to non-Blocking mode} private void button#click (object sender, EventArgs e ){
Related Article

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.