using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class Broadcst
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
sock.SendTo(data, iep1);
sock.SendTo(data, iep2);
sock.Close();
}
}
Bad Broadcast
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class BadBroadcast {
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
sock.SendTo(data, iep);
sock.Close();
}
}
Receive Broadcast
/*
C# Network Programming
by Richard Blum
Publisher: Sybex
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class RecvBroadcst
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive...");
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref ep);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
sock.Close();
}
}
Create Metafile
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
class CreateMetafile: Form
{
Metafile mf;
public static void Main()
{
Application.Run(new CreateMetafile());
}
public CreateMetafile()
{
ResizeRedraw = true;
Graphics grfx = CreateGraphics();
IntPtr ipHdc = grfx.GetHdc();
mf = new Metafile(“CreateMetafile.emf”, ipHdc);
grfx.ReleaseHdc(ipHdc);
grfx.Dispose();
grfx = Graphics.FromImage(mf);
grfx.FillEllipse(Brushes.Blue, 60, 20, 20, 20);
grfx.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
for (int y = 0; y < cy; y += mf.Height)
for (int x = 0; x < cx; x += mf.Width)
grfx.DrawImage(mf, x, y, mf.Width, mf.Height);
}
}
[/csharp]
Create Metafile (Reload)
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
class CreateMetafileReload : Form {
const string strMetafile = “CreateMetafileReload.emf”;
public static void Main() {
Application.Run(new CreateMetafileReload());
}
public CreateMetafileReload() {
ResizeRedraw = true;
if (!File.Exists(strMetafile)) {
Graphics grfx = CreateGraphics();
IntPtr ipHdc = grfx.GetHdc();
Metafile mf = new Metafile(strMetafile, ipHdc);
grfx.ReleaseHdc(ipHdc);
grfx.Dispose();
grfx = Graphics.FromImage(mf);
grfx.DrawEllipse(Pens.Black, 0, 0, 100, 100);
grfx.FillEllipse(Brushes.Blue, 60, 20, 20, 20);
grfx.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120);
grfx.Dispose();
}
}
protected override void OnPaint(PaintEventArgs pea) {
DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
Metafile mf = new Metafile(strMetafile);
for (int y = 0; y < cy; y += mf.Height) for (int x = 0; x < cx; x += mf.Width) grfx.DrawImage(mf, x, y, mf.Width, mf.Height); } } [/csharp]
Create Metafile (Memory)
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
class CreateMetafileMemory: Form
{
MemoryStream ms = new MemoryStream();
public static void Main()
{
Application.Run(new CreateMetafileMemory());
}
public CreateMetafileMemory()
{
ResizeRedraw = true;
Graphics grfx = CreateGraphics();
IntPtr ipHdc = grfx.GetHdc();
Metafile mf = new Metafile(ms, ipHdc);
grfx.ReleaseHdc(ipHdc);
grfx.Dispose();
grfx = Graphics.FromImage(mf);
grfx.FillEllipse(Brushes.Gray, 0, 0, 100, 100);
grfx.DrawEllipse(Pens.Black, 0, 0, 100, 100);
grfx.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120);
grfx.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
ms.Position = 0;
Metafile mf = new Metafile(ms);
for (int y = 0; y < cy; y += mf.Height) for (int x = 0; x < cx; x += mf.Width) grfx.DrawImage(mf, x, y, mf.Width, mf.Height); } } [/csharp]
Metafile Page Units
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Windows.Forms;
class MetafilePageUnits: Form
{
Metafile mf;
public static void Main()
{
Application.Run(new MetafilePageUnits());
}
public MetafilePageUnits()
{
ResizeRedraw = true;
Graphics grfx = CreateGraphics();
IntPtr ipHdc = grfx.GetHdc();
mf = new Metafile("MetafilePageUnits.emf", ipHdc);
grfx.ReleaseHdc(ipHdc);
grfx.Dispose();
grfx = Graphics.FromImage(mf);
grfx.Clear(Color.White);
grfx.PageUnit = GraphicsUnit.Pixel;
Pen pen = new Pen(Color.Black, grfx.DpiX / 72);
grfx.DrawRectangle(pen, 0, 0, grfx.DpiX, grfx.DpiY);
grfx.PageUnit = GraphicsUnit.Inch;
grfx.PageScale = 0.01f;
pen = new Pen(Color.Black, 100f / 72);
grfx.DrawRectangle(pen, 25, 25, 100, 100);
grfx.Dispose();
}
protected void OnPaint(PaintEventArgs pea)
{
grfx.DrawImage(mf, 0, 0);
}
}

