自己即是服务器又是客户端 ,在发消息只需要改成对方ip和端口号即可
前提对方必须开启服务器 socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.107.72"), 8080));
控件:Button,TextBox,RichTextBox
- public Form1()
- {
- InitializeComponent();
- //1创建一个服务器 绑定的是ip和端口号 192.168.107.83, 8080
- // 张三的终端 以后谁想跟张三聊的时候 发这个ip和端口号
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- socket.Bind(new IPEndPoint(IPAddress.Any, 8080));
- startReceive();
- }
- Socket socket;
- void startReceive()
- {
- byte[] bs = new byte[1024];
- Task.Run(() =>
- {
- while (true)
- {
- int count = socket.Receive(bs);
- string s = Encoding.UTF8.GetString(bs,0, count);
- this.Invoke((Action)(() =>
- {
- richTextBox1.AppendText(s + "\t\n");
- }));
-
- }
- });
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //发消息的一定要注意iphe端口号
- socket.SendTo(Encoding.UTF8.GetBytes(this.textBox1.Text),
- new IPEndPoint(IPAddress.Parse("192.168.107.73"), 8082));
- }

组播需要使用组播地址,在 IPv4 中它的范围从 224.0.0.0 到 239.255.255.255,
并被划分为局部链接多播地址、预留多播地址和管理权限多播地址三类
224.0.0.0 ~ 224.0.0.255: 局部链接多播地址:是为路由协议和其它用途保留的地址,
只能用于局域网中,路由器是不会转发的地址 224.0.0.0 不能用,是保留地址
224.0.1.0 ~ 224.0.1.255: 为用户可用的组播地址(临时组地址),可以用于 Internet 上的。
224.0.2.0 ~ 238.255.255.255: 用户可用的组播地址(临时组地址),全网范围内有效
239.0.0.0 ~ 239.255.255.255: 为本地管理组播地址,仅在特定的本地范围内有效
- public Form1()
- {
- InitializeComponent();
- }
- Socket socket;
- private void button1_Click(object sender, EventArgs e)
- {
- //1创建socket对象
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-
- //2 绑定ip和端口号
- socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.107.83"), 8080));
- //3 加入组播地址
- //SetSocketOption 添加套接字可配置选项
- //参数1.支持协议类型,
- //参数2 添加组播地址的功能
- //参数3 要组播的地址
- socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption(IPAddress.Parse("224.0.0.188"))
- );
-
- //4以后大家要是接受的话 接受组内发来的消息的时候 必须添接受组内代码
-
- //5 接收数据
- startReceive();
- }
- void startReceive()
- {
- new Thread(() =>
- {
- byte[] buffer = new byte[1024];
- while (true)
- {
- int count = socket.Receive(buffer);
- string s = Encoding.UTF8.GetString(buffer, 0, count);
- richTextBox1.Invoke((Action)(() =>
- {
- richTextBox1.AppendText(s + "\t\n");
- }));
- }
- }).Start();
- }
-
- //发送消息
- private void button2_Click(object sender, EventArgs e)
- {
- //发消息指定组地址进行发送,以后要求接受消息端口号和此处端口号保持一致
- socket.SendTo(Encoding.UTF8.GetBytes(this.textBox1.Text),
- new IPEndPoint(IPAddress.Parse("224.0.0.188"), 10086)
- );
- }
两个按钮(客户端打开接收消息,发送),textbox,richtextBox
- public Form1()
- {
- InitializeComponent();
- }
-
- Socket socket;
- private void button1_Click(object sender, EventArgs e)
- {
- //1创建socket对象
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-
- //2 绑定ip和端口号
- socket.Bind(new IPEndPoint(IPAddress.Any, 10086));
-
- //3 设置组地址 对客户端加入指定组播地址内
- socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption(IPAddress.Parse("224.0.1.188")));
-
-
- //4 接受消息
- Task.Run(() =>
- {
- while (true)
- {
- byte[] buffer = new byte[1024];
- //int count = socket.Receive(buffer);
-
- EndPoint ip = new IPEndPoint(IPAddress.None, 0); //定义endpoint类型变量,终端类型
- //ReceiveFrom()接受数据的方法 从哪个终端发来的消息
- //参数3是发来消息的地址 类型是endpoint
- int count = socket.ReceiveFrom(buffer, 0, ref ip);
- IPEndPoint i1 = ip as IPEndPoint; // 把endpoint类型 转成IPEndPoint
-
- string s = Encoding.UTF8.GetString(buffer, 0, count);
-
- richTextBox1.Invoke((Action)(() =>
- {
- richTextBox1.AppendText(i1.Address+":"+s + "\t\n");
- richTextBox1.SelectionStart=richTextBox1.Text.Length;
- richTextBox1.ScrollToCaret();
- }));
-
- }
-
- });
- }
- private void button2_Click(object sender, EventArgs e)
- {
- //socket.SendTo(Encoding.UTF8.GetBytes("hello world"),
- // new IPEndPoint(IPAddress.Parse("192.168.107.83"), 8080));
-
- if (Encoding.UTF8.GetBytes(this.textBox1.Text).Length>1024)
- {
- return;
- }
- socket.SendTo(Encoding.UTF8.GetBytes(this.textBox1.Text),
- new IPEndPoint(IPAddress.Parse("224.0.1.188"), 10086));
- }