//**************发送端******************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading;namespace Socket广播发送端 { public partial class Form1 : Form { private IPEndPoint enLoacp; private Socket mySocket; private Thread Mythread; private byte[] sendbuff; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); enLoacp = new IPEndPoint(IPAddress.Broadcast, 9050); mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
http://www.sohozu.com/2005/5-24/1305423866.Html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace Socket广播发送端
{
public partial class Form1 : Form
{
private IPEndPoint enLoacp;
private Socket mySocket;
private Thread Mythread;
private byte[] sendbuff;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
enLoacp = new IPEndPoint(IPAddress.Broadcast, 9050);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
Mythread = new Thread(new ThreadStart(Recive));
Mythread.Name = "a";
Mythread.Start();
}
private void Recive()
{
try
{
while (true)
{
try
{
sendbuff = Encoding.Default.GetBytes("现在时间:" + DateTime.Now.ToLongTimeString());
mySocket.SendTo(sendbuff, enLoacp);
Thread.Sleep(2000);
}
catch (Exception ex)
{ }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Socket广播
{
public partial class Form1 : Form
{
private IPEndPoint enLoacp;
private Socket mySocket;
private Thread Mythread;
private EndPoint ep;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
enLoacp = new IPEndPoint(IPAddress.Any, 9050);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
mySocket.Bind(enLoacp);
ep = (EndPoint)enLoacp;
Mythread = new Thread(new ThreadStart(Recive));
Mythread.Name = "a";
Mythread.Start();
}
private void Recive()
{
try
{
while (true)
{
try
{
if (mySocket == null)
{
Thread.Sleep(1000);
continue;
}
else
{
if (listBox1.Items.Count > 10)
{
listBox1.Items.Clear();
}
byte[] b=new byte[1024];
int size = mySocket.ReceiveFrom(b,ref ep);
string strbuff = Encoding.Default.GetString(b,0,size);
listBox1.Items.Add("收到广播消息:" + strbuff);
}
}
catch(Exception ex)
{
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Mythread != null & Mythread.ThreadState != ThreadState.Unstarted && Mythread.ThreadState != ThreadState.Stopped)
{
Mythread.Abort();
}
if (mySocket != null)
{
mySocket.Close();
}
}
}
}
那Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)这种形式也应该叫广播吧?