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,表示可以接受连接请求,false则为结束程序
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
public void Stop()
{
tcpl.Close();
th.Abort();//终止线程
MessageBox.Show("结束监听....");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(Listen));//新建一个用于监听的线程
th.Start();//打开新线程
}
private void Listen()
{
try
{
tcpl = new UdpClient(5656);//在5656端口新建一个TcpListener对象
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5656);
MessageBox.Show("正在监听中....");
while (listenerRun)//开始监听
{
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));//新建一个用于监听的线程
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();
}
}
}
服务端代码:
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端口新建一个TcpListener对象
groupEP = new IPEndPoint(IPAddress.Any, 5270);
pclient = new ArrayList();
while (listenerRun)//开始监听
{
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));//新建一个用于监听的线程
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);
}
}
}
}