Introduction:
In this article,i am going to demonstarte how to connect(sending & Receiving Messages) between two computers uisng UDP protocol.
Main:
In Asp.Net the System.Net.Sockets.UdpClient class helps to achieve this task,See this below simple examble program,
in this program i am going to creating two threads for sending and receiving messages between two computers,
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UDP
{
class netudp
{
private static int localPort;
private static void Main()
{
// Define endpoint where messages are sent.
Console.Write("Connect to IP: ");
string IP = Console.ReadLine();
Console.Write("Connect to port: ");
int port = Int32.Parse(Console.ReadLine());
IPEndPoint remoteEndPoint =
new IPEndPoint(IPAddress.Parse(IP), port);
// Define local endpoint (where messages are received).
Console.Write("Local port for listening: ");
localPort = Int32.Parse(Console.ReadLine());
// Create a new thread for receiving incoming messages.
Thread receiveThread = new Thread(ReceiveData);
receiveThread.IsBackground = true;
receiveThread.Start();
UdpClient client = new UdpClient();
Console.WriteLine("Type message and press Enter to send:");
try
{
string text;
do
{
text = Console.ReadLine();
// Send the text to the remote client.
if (text.Length != 0)
{
// Encode the data to binary using UTF8 encoding.
byte[] data = Encoding.UTF8.GetBytes(text);
// Send the text to the remote client.
client.Send(data, data.Length, remoteEndPoint);
}
} while (text.Length != 0);
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
finally
{
client.Close();
}
// Wait to continue.
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Main method complete. Press Enter");
Console.ReadLine();
}
private static void ReceiveData()
{
UdpClient client = new UdpClient(localPort);
while (true)
{
try
{
// Receive bytes.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
// Convert bytes to text using UTF8 encoding.
string text = Encoding.UTF8.GetString(data);
// Display the retrieved text.
Console.WriteLine(">> " + text);
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
}
}
}
}
using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace UDP { class netudp { private static int localPort; private static void Main() { // Define endpoint where messages are sent. Console.Write("Connect to IP: "); string IP = Console.ReadLine(); Console.Write("Connect to port: "); int port = Int32.Parse(Console.ReadLine()); IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port); // Define local endpoint (where messages are received). Console.Write("Local port for listening: "); localPort = Int32.Parse(Console.ReadLine()); // Create a new thread for receiving incoming messages. Thread receiveThread = new Thread(ReceiveData); receiveThread.IsBackground = true; receiveThread.Start(); UdpClient client = new UdpClient(); Console.WriteLine("Type message and press Enter to send:"); try { string text; do { text = Console.ReadLine(); // Send the text to the remote client. if (text.Length != 0) { // Encode the data to binary using UTF8 encoding. byte[] data = Encoding.UTF8.GetBytes(text); // Send the text to the remote client. client.Send(data, data.Length, remoteEndPoint); } } while (text.Length != 0); } catch (Exception err) { Console.WriteLine(err.ToString()); } finally { client.Close(); } // Wait to continue. Console.WriteLine(Environment.NewLine); Console.WriteLine("Main method complete. Press Enter"); Console.ReadLine(); } private static void ReceiveData() { UdpClient client = new UdpClient(localPort); while (true) { try { // Receive bytes. IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0); byte[] data = client.Receive(ref anyIP); // Convert bytes to text using UTF8 encoding. string text = Encoding.UTF8.GetString(data); // Display the retrieved text. Console.WriteLine(">> " + text); } catch (Exception err) { Console.WriteLine(err.ToString()); } } } } } |
Conclusion:
Hope this helps,
Happy Coding.
Keep posting stuff like this i really like it
I feel a lot more individuals need to read this, incredibly very good info.
Hey may I reference some of the insight found in this post if I reference you with a link back to your site?
I’d come to check with you on this. Which is not something I usually do! I love reading a post that will make people think. Also, thanks for allowing me to comment!
Good blog. How long have you been running this blog? I need to improve mine a bit!
Hi there, I just noticed your RSS feed is not working correctly, Thought I should inform you.
The considerable host’s opinion analysis is unique, lets me have the new understanding to this make any difference
Some people see the cup as half empty. Some people see the cup as half full. I see the cup as too large.
World-wide-web style is an art, I really like it. When looking through posts like these, it can make me go back quite a few a long time in the past and believe about when I began. It is incredible how a lot anything has changed over the several years. Great post
The article is worth reading. The clarity and balance that shines from this blog post. Now-a-days blogs are used everywhere. The knowledge that we recieve from them are unevitable. The attribute required is the power of creativity within yourself through learning, thinking, creating and rigorous study. Therefore the article is truely helpful for the readers. Thank you a lot for writing such a wonderful article. I will wait for your next article with great curiosity.