UDP实现内网到网的点对点发送消息与转发消息
2010-03-29 12:54
344 查看
UDP实现内网到网的点对点发送消息与转发消息 与上篇讲到的TCP连接有一点不一样 ,TCP可以一直保持着链接.然后进行通讯. 而UDP发送后,服务器需要接收后立即关闭此消息然后让再次调用监听,立即释放资源.然后再次处于监听状态,我估计这样实现是有问题的.但是我现在也只能这么实现了.
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();
}
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("127.0.0.1");
byte[] sendbuf = Encoding.UTF8.GetBytes("中国人在哪里");
IPEndPoint ep = new IPEndPoint(broadcast, 5270);
s.Connect(ep);
s.SendTo(sendbuf, ep);
byte[] data = new byte[2048];
int rect = s.Receive(data);
byte[] chat = new byte[rect];
Buffer.BlockCopy(data, 0, chat, 0, rect);
MessageBox.Show(System.Text.Encoding.Default.GetString(chat));
s.Close();
}
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;
namespace p2pserver
{
public partial class Form1 : Form
{
private Thread th;
private UdpClient tcpl;
public bool listenerRun = true;
public Form1()
{
InitializeComponent();
this.button2.Enabled = false;
}
private void Listen()
{
try
{
tcpl = new UdpClient(5270);//在5270端口新建一个TcpListener对象
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5270);
while (listenerRun)//开始监听
{
string msg = "welcome";
Byte[] stream = new Byte[80];
stream = tcpl.Receive(ref groupEP);
MessageBox.Show(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));
byte[] bytesMsg = Encoding.Default.GetBytes(msg);
tcpl.Connect(groupEP);
tcpl.Send(bytesMsg, bytesMsg.Length);
tcpl.Close();
Listen();
}
}
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();
}
}
}
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();
}
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("127.0.0.1");
byte[] sendbuf = Encoding.UTF8.GetBytes("中国人在哪里");
IPEndPoint ep = new IPEndPoint(broadcast, 5270);
s.Connect(ep);
s.SendTo(sendbuf, ep);
byte[] data = new byte[2048];
int rect = s.Receive(data);
byte[] chat = new byte[rect];
Buffer.BlockCopy(data, 0, chat, 0, rect);
MessageBox.Show(System.Text.Encoding.Default.GetString(chat));
s.Close();
}
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;
namespace p2pserver
{
public partial class Form1 : Form
{
private Thread th;
private UdpClient tcpl;
public bool listenerRun = true;
public Form1()
{
InitializeComponent();
this.button2.Enabled = false;
}
private void Listen()
{
try
{
tcpl = new UdpClient(5270);//在5270端口新建一个TcpListener对象
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5270);
while (listenerRun)//开始监听
{
string msg = "welcome";
Byte[] stream = new Byte[80];
stream = tcpl.Receive(ref groupEP);
MessageBox.Show(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));
byte[] bytesMsg = Encoding.Default.GetBytes(msg);
tcpl.Connect(groupEP);
tcpl.Send(bytesMsg, bytesMsg.Length);
tcpl.Close();
Listen();
}
}
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();
}
}
}
相关文章推荐
- UDP实现内网到网的点对点发送消息与转发消息
- socket 实现点对点发送消息
- UDP协议下内网与公网IP进行发送消息,一对多.且选择不同的客户端发送消息
- 在unity中用UDP实现发送消息机制
- UDP协议下内网与公网IP进行发送消息,一对多.且选择不同的客户端发送消息
- 利用P2P点对点技术实现UDP内网穿透
- UDP协议下内网与公网IP进行发送消息
- SpringBoot webSocket实现发送广播、点对点消息和Android接收
- C#基于TCP&UDP实现服务器与多个客户端之间的通信(客户端之间直接通信,不靠服务器端转发消息)
- (二)ActiveMQ之点对点消息实现
- swoole 点对点发送消息推送
- PHP实现RTX发送消息提醒的实例代码
- workerMan学习篇:websocket+workerman聊天功能(三):点对点发送消息模拟
- Nodejs socket.io 实现私聊:如何给指定客户端发送消息,不是群广播
- iPhone消息推送机制(Push)实现及通过.net应用程序发送消息给ios应用程序
- (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误
- vc自定义消息的发送与接收的方法实现
- (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误
- BCB 如何实现在不同的程序间发送和响应自定义消息?
- Tomcat + JNDI + ActiveMQ实现JMS的点对点消息传送