Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

UDP client

By: Tomasz_svk on Jan 16th, 2013  |  syntax: C#  |  size: 2.60 KB  |  views: 179  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net.Sockets;
  10. using System.Net;
  11. using System.Threading;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         byte[] data = new byte[1024];
  18.         string stringData;
  19.         UdpClient server, client;
  20.         IPEndPoint send, send1;
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void sendData(UdpClient s, string d)
  28.         {
  29.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  30.             Byte[] data1 = encoding.GetBytes(d);
  31.             s.Send(data1, data1.Length);
  32.         }
  33.  
  34.         private string receiveData(UdpClient s, IPEndPoint e){
  35.             Byte[] data1 = s.Receive(ref e);
  36.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  37.             return encoding.GetString(data1);
  38.         }
  39.  
  40.         private void button1_Click(object sender, EventArgs e)
  41.         {
  42.        
  43.             server = new UdpClient(textBox1.Text, Convert.ToInt32(textBox2.Text));
  44.  
  45.             send  = new IPEndPoint(IPAddress.Any, 2000);
  46.  
  47.             string test1 = "1|"+textBox8.Text;
  48.  
  49.             sendData(server,test1);
  50.  
  51.         }
  52.  
  53.         private void button3_Click(object sender, EventArgs e)
  54.         {
  55.             sendData(server,"2|" + textBox7.Text);
  56.             Thread.Sleep(100);
  57.             textBox4.AppendText(server.Available.ToString());
  58.             string d = receiveData(server,send);
  59.             //textBox4.AppendText();
  60.             String[] a = d.Split('|');
  61.             textBox6.Text = a[0];
  62.             textBox5.Text = a[1];
  63.         }
  64.  
  65.         private void label1_Click(object sender, EventArgs e)
  66.         {
  67.  
  68.         }
  69.  
  70.         private void button4_Click(object sender, EventArgs e)
  71.         {
  72.             client = new UdpClient(textBox6.Text, Convert.ToInt32(textBox5.Text));
  73.  
  74.             sendData(client, "HELLO!");
  75.             timer1.Enabled = true;
  76.             //string d = receiveData(client, send);
  77.             //textBox4.AppendText(d);
  78.         }
  79.  
  80.         private void timer1_Tick(object sender, EventArgs e)
  81.         {
  82.             if (client.Available > 0)
  83.             {
  84.                 string d = receiveData(client, send);
  85.                 textBox4.AppendText(d);
  86.             }
  87.         }
  88.  
  89.         private void button2_Click(object sender, EventArgs e)
  90.         {
  91.             sendData(client, textBox3.Text);
  92.         }
  93.     }
  94. }