Using C# UDPClient Send and Receive message

No.of Views4119
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : CSharp , Miscellaneous
Using C# UDPClient Send and Receive message
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


The Client Code
{codecitation class="brush: csharp; gutter: true;" width="500px"}

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.Threading;
using System.Net;


namespace NoticeSystemClient
{
public partial class MainForm : Form
{
public delegate void ShowMessage(string message);
public ShowMessage myDelegate;
Int32 port = 11000;
UdpClient udpClient = new UdpClient(11000);
Thread thread;
public MainForm()
{
//CheckForIllegalCrossThreadCalls = false;
InitializeComponent();


private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
thread.Abort();
udpClient.Close();
Close();
}
}


private void ReceiveMessage()
{                     
while (true)
{
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
byte[] content = udpClient.Receive(ref remoteIPEndPoint);
if (content.Length > 0)
{
string message = Encoding.ASCII.GetString(content);
this.Invoke(myDelegate, new object[] { message });
}
}
}

private void ShowMessageMethod(string message)
{
richText.Text = message;
}


private void MainForm_Load(object sender, EventArgs e)
{         
myDelegate = new ShowMessage(ShowMessageMethod);
thread = new Thread(new ThreadStart(ReceiveMessage));
thread.IsBackground = true;
thread.Start();
}
}
}

{/codecitation}

The Server Side Code:

{codecitation class="brush: csharp; gutter: true;" width="500px"}
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 NoticeSystem
{
public partial class MainForm : Form
{
UdpClient udpClient = new UdpClient();
public MainForm()
{
InitializeComponent();
}

private void btnClose_Click(object sender, EventArgs e)
{
udpClient.Close();
Close();
}


private void btnSend_Click(object sender, EventArgs e)
{
Int32 port = 11000;
IPAddress ip = IPAddress.Parse(txtStartIP.Text.Trim());
IPEndPoint ipEndPoint = new IPEndPoint(ip,port);
byte[] content = Encoding.ASCII.GetBytes(richText.Text);
try
{
int count = udpClient.Send(content, content.Length, ipEndPoint);
if (count > 0)
{
MessageBox.Show("Message has been sent.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
MessageBox.Show("Error occurs.", "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
{/codecitation}

Thank you
Amal
 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top