UDP协议下内网与公网IP进行发送消息,一对多.且选择不同的客户端发送消息
客戶端代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace p2p
{
??? public partial class Form1 : Form
??? {
??????? private Thread th;
??????? private UdpClient tcpl;
??????? public bool listenerRun = true;
??????? Socket s;
??????? //listenerRun為true,表示可以接受連接請(qǐng)求,false則為結(jié)束程序
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????????? Control.CheckForIllegalCrossThreadCalls = false;
??????? }
??????? public void Stop()
??????? {
??????????? tcpl.Close();
??????????? th.Abort();//終止線程
??????????? MessageBox.Show("結(jié)束監(jiān)聽....");
??????? }
??????? private void Form1_Load(object sender, EventArgs e)
??????? {
??????? }
??????? private void button1_Click(object sender, EventArgs e)
??????? {
?????????? th = new Thread(new ThreadStart(Listen));//新建一個(gè)用于監(jiān)聽的線程
?????????? th.Start();//打開新線程
??????? }
??????? private void Listen()
??????? {
??????????? try
??????????? {
??????????????? tcpl = new UdpClient(5656);//在5656端口新建一個(gè)TcpListener對(duì)象
??????????????? IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5656);
??????????????? MessageBox.Show("正在監(jiān)聽中....");
???????????????? while (listenerRun)//開始監(jiān)聽
???????????????? {
???????????????????? Byte[] stream = new Byte[80];
???????????????????? stream = tcpl.Receive(ref groupEP);
???????????????????
???????????????????? MessageBox.Show(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));???????????????
??????????????? }
??????????? }
??????????? catch (Exception ex)
??????????? {
???????????? MessageBox.Show(ex.Message);
??????????? }
????????????
??????? }
??????? private void button3_Click(object sender, EventArgs e)
??????? {
??????????? th = new Thread(new ThreadStart(Send));//新建一個(gè)用于監(jiān)聽的線程
??????????? th.Start();//打開新線程
??????? }
??????? public void Send()
??????? {
??????????? try
??????????? {
???????????????? s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
???????????????? IPAddress broadcast = IPAddress.Parse(this.textBox3.Text.Trim());
???????????????? byte[] sendbuf = Encoding.UTF8.GetBytes(this.textBox2.Text.Trim());
??????????????? IPEndPoint ep = new IPEndPoint(broadcast, 5270);
??????????????? s.Connect(ep);
??????????????? s.SendTo(sendbuf, ep);
??????????????? while (true)
??????????????? {
??????????????????? byte[] data = new byte[2048];
??????????????????? int rect = s.Receive(data);
??????????????????? byte[] chat = new byte[rect];
??????????????????? Buffer.BlockCopy(data, 0, chat, 0, rect);
??????????????????? textBox1.AppendText(System.Text.Encoding.Default.GetString(chat));
??????????????? }?
?????????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? MessageBox.Show(ex.Message);
??????????? }
??????????? finally
??????????? {
??????????????
??????????? }
????????????
??????? }
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? Stop();
??????? }
??? }
}?
服務(wù)端代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Collections;
namespace p2pserver
{
??? public partial class Form1 : Form
??? {
??????? private Thread th;
??????? private UdpClient tcpl;
??????? public bool listenerRun = true;
??????? private ArrayList pclient;//
??????? IPEndPoint groupEP;
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????????? this.button2.Enabled = false;
??????????? Control.CheckForIllegalCrossThreadCalls = false;
??????? }
??????? private void Listen()
??????? {
??????????? try
??????????? {
??????????????? tcpl = new UdpClient(5270);//在5656端口新建一個(gè)TcpListener對(duì)象
???????????????? groupEP = new IPEndPoint(IPAddress.Any, 5270);
???????????????? pclient = new ArrayList();
??????????????? while (listenerRun)//開始監(jiān)聽
??????????????? {
??????????????????? pclient.Add(groupEP);????
??????????????????? Byte[] stream = new Byte[80];
??????????????????? stream = tcpl.Receive(ref groupEP);
??????????????????? this.textBox1.AppendText(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));
??????????????? }
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? MessageBox.Show(ex.Message);
??????????? }
??????????? finally
??????????? {
??????????? }
??????? }
??????? public void Stop()
??????? {
??????????? try
??????????? {
????????????? tcpl.Close();
??????????????? th.Abort();//終止線程
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? MessageBox.Show(ex.Message);
??????????? }
??????? }
??????
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? this.button1.Enabled = false;
??????????? this.button2.Enabled = true;
??????????? th = new Thread(new ThreadStart(Listen));//新建一個(gè)用于監(jiān)聽的線程
??????????? th.Start();//打開新線程
??????? }
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? this.button2.Enabled = false;
??????????? this.button1.Enabled = true;
??????????? Stop();
??????? }
??????? private void button3_Click(object sender, EventArgs e)
??????? {
??????????? try
??????????? {
??????????????? int index = int.Parse(this.textBox3.Text.Trim());
??????????????? byte[] bytesMsg = Encoding.Default.GetBytes(this.textBox2.Text.Trim());
??????????????? IPEndPoint sc = (IPEndPoint)pclient[index];
??????????????? tcpl.Send(bytesMsg, bytesMsg.Length, sc);
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? MessageBox.Show(ex.Message);
??????????? }
??????? }
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/top5/archive/2010/03/29/1699479.html
總結(jié)
以上是生活随笔為你收集整理的UDP协议下内网与公网IP进行发送消息,一对多.且选择不同的客户端发送消息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一些小知识
- 下一篇: F#与ASP.NET(1):基于事件的异