Материал из .Net Framework эксперт
Binary UdpClient: send binary data to Udp server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] data = new byte[1024];
string stringData;
UdpClient server = new UdpClient("127.0.0.1", 9050);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
int test1 = 45;
double test2 = 3.14159;
int test3 = -1234567890;
bool test4 = false;
string test5 = "This is a test.";
byte[] data1 = BitConverter.GetBytes(test1);
server.Send(data1, data1.Length);
byte[] data2 = BitConverter.GetBytes(test2);
server.Send(data2, data2.Length);
byte[] data3 = BitConverter.GetBytes(test3);
server.Send(data3, data3.Length);
byte[] data4 = BitConverter.GetBytes(test4);
server.Send(data4, data4.Length);
byte[] data5 = Encoding.ASCII.GetBytes(test5);
server.Send(data5, data5.Length);
server.Close();
}
}
Binary Udp Server: receive binary data from Udp client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
UdpClient newsock = new UdpClient(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
byte[] data1 = newsock.Receive(ref sender);
int test1 = BitConverter.ToInt32(data1, 0);
Console.WriteLine("test1 = {0}", test1);
byte[] data2 = newsock.Receive(ref sender);
double test2 = BitConverter.ToDouble(data2, 0);
Console.WriteLine("test2 = {0}", test2);
byte[] data3 = newsock.Receive(ref sender);
int test3 = BitConverter.ToInt32(data3, 0);
Console.WriteLine("test3 = {0}", test3);
byte[] data4 = newsock.Receive(ref sender);
bool test4 = BitConverter.ToBoolean(data4, 0);
Console.WriteLine("test4 = {0}", test4.ToString());
byte[] data5 = newsock.Receive(ref sender);
string test5 = Encoding.ASCII.GetString(data5);
Console.WriteLine("test5 = {0}", test5);
newsock.Close();
}
}
UdpClient: receive for multicast group
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
UdpClient sock = new UdpClient(9999);
Console.WriteLine("Ready to receive...");
sock.JoinMulticastGroup(IPAddress.Parse("127.0.0.1"), 50);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
byte[] data = sock.Receive(ref iep);
string stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString());
sock.Close();
}
}
UdpClient: Send
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
UdpClient sock = new UdpClient();
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
sock.Send(data, data.Length, iep);
sock.Close();
}
}
Udp connection test
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class MainClass
{
private static void Main()
{
string IP = "127.0.0.1";
int port = 9999;
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
Thread receiveThread = new Thread(ReceiveData);
receiveThread.IsBackground = true;
receiveThread.Start();
UdpClient client = new UdpClient();
try
{
string text;
do
{
text = Console.ReadLine();
if (text.Length != 0)
{
byte[] data = Encoding.UTF8.GetBytes(text);
client.Send(data, data.Length, remoteEndPoint);
}
} while (text.Length != 0);
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
finally
{
client.Close();
}
}
private static void ReceiveData()
{
UdpClient client = new UdpClient(999);
while (true)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
string text = Encoding.UTF8.GetString(data);
Console.WriteLine(">> " + text);
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
}
}
}
Use UdpClient
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
UdpClient udpClient = new UdpClient("127.0.0.1", 9999);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
string welcome = "Hello";
data = Encoding.ASCII.GetBytes(welcome);
udpClient.Send(data, data.Length);
data = udpClient.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
while(true)
{
input = Console.ReadLine();
udpClient.Send(Encoding.ASCII.GetBytes(input), input.Length);
data = udpClient.Receive(ref sender);
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
}
udpClient.Close();
}
}