谁有Socket广播的例子???

blackem 2006-11-01 01:36:24
Socket向指定的一批IP发送信息,只要这个功能就行了
...全文
232 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
blackem 2006-11-01
  • 打赏
  • 举报
回复
虚心求教ing....
hebeijg 2006-11-01
  • 打赏
  • 举报
回复
up
blackem 2006-11-01
  • 打赏
  • 举报
回复
用UDPClient进行收发才叫广播么??
那Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)这种形式也应该叫广播吧?
ken_flash 2006-11-01
  • 打赏
  • 举报
回复
MSDN自带的就有 自己去查一下UDPClient
blackem 2006-11-01
  • 打赏
  • 举报
回复
那请问怎样的才叫广播???
我理解的广播形式,就是由一台主机,向所有客户端发送消息,而不用和每个客户端建立连接
可能我的理解有误,那你说一下广播应该是什么样的???
lifeixie 2006-11-01
  • 打赏
  • 举报
回复
那叫群发,不叫广播
blackem 2006-11-01
  • 打赏
  • 举报
回复
做了个简单的例子,发送端可以向所有IP的接收端发送广播消息,程序很粗糙只是一个思路,仅供参考
blackem 2006-11-01
  • 打赏
  • 举报
回复
//********************接收端*********************
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();
}
}
}
}
blackem 2006-11-01
  • 打赏
  • 举报
回复
//**************发送端********************
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);
}
}
Onlyzhugf 2006-11-01
  • 打赏
  • 举报
回复
简单的多人聊天(C#.Socket).
http://www.sohozu.com/2005/5-24/1305423866.Html
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-11-01 01:36
社区公告

让您成为最强悍的C#开发者