This Blog Has Moved

This blog has moved to http://amalhashim.wordpress.com

Thursday, June 18, 2009

Using C# UDPClient Send and Receive message

Client to receive message sent by server:
   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.Threading;
  11:  using System.Net;
  12:   
  13:  namespace NoticeSystemClient
  14:  {
  15:      public partial class MainForm : Form
  16:      {
  17:          public delegate void ShowMessage(string message);
  18:          public ShowMessage myDelegate;
  19:          Int32 port = 11000;
  20:          UdpClient udpClient = new UdpClient(11000);
  21:          Thread thread;
  22:          public MainForm()
  23:          {
  24:              //CheckForIllegalCrossThreadCalls = false;
  25:              InitializeComponent();
  26:          }
  27:   
  28:          private void MainForm_KeyDown(object sender, KeyEventArgs e)
  29:          {
  30:              if (e.KeyCode == Keys.Escape)
  31:              {
  32:                  thread.Abort();
  33:                  udpClient.Close();
  34:                  Close();
  35:              }
  36:          }
  37:   
  38:          private void ReceiveMessage()
  39:          {                      
  40:              while (true)
  41:              {
  42:                  IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
  43:                  byte[] content = udpClient.Receive(ref remoteIPEndPoint);
  44:   
  45:                  if (content.Length > 0)
  46:                  {
  47:                      string message = Encoding.ASCII.GetString(content);
  48:                      
  49:                      this.Invoke(myDelegate, new object[] { message });
  50:                  }
  51:              }
  52:          }
  53:   
  54:   
  55:          private void ShowMessageMethod(string message)
  56:          {
  57:              richText.Text = message;
  58:          }
  59:   
  60:          private void MainForm_Load(object sender, EventArgs e)
  61:          {            
  62:              myDelegate = new ShowMessage(ShowMessageMethod);
  63:              thread = new Thread(new ThreadStart(ReceiveMessage));
  64:              thread.IsBackground = true;
  65:              thread.Start();
  66:          }
  67:      }
  68:  }

Server:

   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:   
  12:  namespace NoticeSystem
  13:  {
  14:      public partial class MainForm : Form
  15:      {
  16:          UdpClient udpClient = new UdpClient();
  17:          public MainForm()
  18:          {
  19:              InitializeComponent();
  20:          }
  21:   
  22:          private void btnClose_Click(object sender, EventArgs e)
  23:          {
  24:              udpClient.Close();
  25:              Close();
  26:          }
  27:   
  28:          private void btnSend_Click(object sender, EventArgs e)
  29:          {
  30:              Int32 port = 11000;
  31:              IPAddress ip = IPAddress.Parse(txtStartIP.Text.Trim());
  32:              IPEndPoint ipEndPoint = new IPEndPoint(ip,port);
  33:              byte[] content = Encoding.ASCII.GetBytes(richText.Text);
  34:              try
  35:              {
  36:                  int count = udpClient.Send(content, content.Length, ipEndPoint);
  37:                  if (count > 0)
  38:                  {
  39:                      MessageBox.Show("Message has been sent.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  40:                  }
  41:              }
  42:              catch
  43:              {
  44:                  MessageBox.Show("Error occurs.", "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  45:              }
  46:          }
  47:      }
  48:  }

No comments: