// Reciver Control Program // Coded by UTAH // Create 2014/12/30 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Net; using System.Threading.Tasks; using System.Windows.Forms; namespace SUPRXCLV00 { public partial class Form1 : Form { // COMMON DATA AREAS char UDPopen_FLG = ' '; string remoteHost; // IP ADDRESS Int32 remotePort = 10040; // PROT-NO // UDP init // UDP USE System.Net.Sockets.UdpClient UDP = new System.Net.Sockets.UdpClient(10040); // UDP CHECK PROC private delegate void Delegate_RcvData(string Rdata, Int32 Rleng); public Form1() { InitializeComponent(); } // UDP read PROC public void UDP_read_PROC() { string rcvMsg; // UDP read while (UDPopen_FLG == 'O') { try { System.Text.Encoding enc = System.Text.Encoding.UTF8; Console.WriteLine("UDP_read started"); System.Net.IPEndPoint remoteEP = null; byte[] rcvBytes = UDP.Receive(ref remoteEP); rcvMsg = enc.GetString(rcvBytes); //textBoxUDPrdata.Text = rcvMsg; Console.WriteLine("RCV Data:{0}", rcvMsg); Console.WriteLine("RCV adrs:{0}/Port-NO:{1}", remoteEP.Address, remoteEP.Port); Invoke(new Delegate_RcvData(UDPproc), new Object[] { rcvMsg, rcvMsg.Length }); } catch (Exception ex) { //MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); UDPopen_FLG = 'C'; // close } } Console.WriteLine("end of UDP_read"); } // UDP RECIVE DATA PROC private void UDPproc(string Rdata, Int32 Rleng) { Console.WriteLine("UDP Data:{0}", Rdata); textBoxUDPrdata.Text = Rdata; } // UDP WRITE DATA PROC private void UDPsend(string Sdata) { if (UDPopen_FLG == 'O') // UDP opened ? { System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] sendBytes = enc.GetBytes(Sdata); UDP.Send(sendBytes, sendBytes.Length); } } // UDP connect PROC private void button_UDPConn_Click(object sender, EventArgs e) { if (UDPopen_FLG == ' ') // UDP open ? { // use UTF8 code System.Text.Encoding enc = System.Text.Encoding.UTF8; // remote IP & Port-no remoteHost = textBox_UDPIPadr.Text; UDP.Connect(remoteHost, remotePort); // Make task for UDP_read_PROC // Make object System.Threading.Thread UDP_task = new System.Threading.Thread( new System.Threading.ThreadStart(UDP_read_PROC)); // run UDP_task UDP_task.Start(); button_UDPConn.Text = "stop"; UDPopen_FLG = 'O'; // Open } else { // clode UDP UDP.Close(); button_UDPConn.Text = "end "; UDPopen_FLG = 'C'; // Close Application.Exit(); } } private void buttonUDPsend_Click(object sender, EventArgs e) { //送信データを指定する UDPsend(textBoxUDPsdata.Text); } } // Form1 END } // name space end