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 { byte[] data = new byte[1024]; string stringData; UdpClient server, client; IPEndPoint send, send1; public Form1() { InitializeComponent(); } private void sendData(UdpClient s, string d) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] data1 = encoding.GetBytes(d); s.Send(data1, data1.Length); } private string receiveData(UdpClient s, IPEndPoint e){ Byte[] data1 = s.Receive(ref e); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetString(data1); } private void button1_Click(object sender, EventArgs e) { server = new UdpClient(textBox1.Text, Convert.ToInt32(textBox2.Text)); send = new IPEndPoint(IPAddress.Any, 2000); string test1 = "1|"+textBox8.Text; sendData(server,test1); } private void button3_Click(object sender, EventArgs e) { sendData(server,"2|" + textBox7.Text); Thread.Sleep(100); textBox4.AppendText(server.Available.ToString()); string d = receiveData(server,send); //textBox4.AppendText(); String[] a = d.Split('|'); textBox6.Text = a[0]; textBox5.Text = a[1]; } private void label1_Click(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { client = new UdpClient(textBox6.Text, Convert.ToInt32(textBox5.Text)); sendData(client, "HELLO!"); timer1.Enabled = true; //string d = receiveData(client, send); //textBox4.AppendText(d); } private void timer1_Tick(object sender, EventArgs e) { if (client.Available > 0) { string d = receiveData(client, send); textBox4.AppendText(d); } } private void button2_Click(object sender, EventArgs e) { sendData(client, textBox3.Text); } } }