如:从本机端口4001发送数据到远程终端(IPEndPoint)
6 个解决方案
#1
居然没人回应 ! 我好着急啊
#2
分不够可以加 速度来人啊
#3
UDP客户端的Socket也可以Bind, 不信你试试
#4
如果多网卡, 你得先选定一个IP
#5
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 4001);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(iep);
socket.SendTo(.....
100%可行, 你得试一下IPAddress.Any有没有问题(或者得指定IP)
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(iep);
socket.SendTo(.....
100%可行, 你得试一下IPAddress.Any有没有问题(或者得指定IP)
#6
internal class UDPClient
{
/// <summary>
/// 本地绑定端口
/// </summary>
private int UdpBindPort;
private UdpClient Client;
/// <summary>
/// 远程通信点
/// </summary>
private IPEndPoint Point = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 10000);
private bool m_IsInit = false;
/// <summary>
/// 是否初始化
/// </summary>
public bool IsInit
{
get { return m_IsInit; }
}
/// <summary>
/// 初始化通信模块
/// </summary>
/// <param name="IP">IP地址</param>
/// <param name="BindPort">本地绑定端口</param>
/// <param name="RemotePort">远程端口</param>
public void Init(string IP, int BindPort, int RemotePort)
{
Point.Address = IPAddress.Parse(IP);
Point.Port = RemotePort;
UdpBindPort = BindPort;
byte[] SendArr = ASCIIEncoding.ASCII.GetBytes(string.Format("init {0}", m_
m_IsInit = true
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="Port">端口</param>
/// <param name="vData">发送的二进制数据</param>
/// <returns></returns>
private bool SendData(int Port, Byte[] vData)
{
try
{
Client = new UdpClient(Port);
Client.Connect(Point);
}
catch(Exception ex) {
Console.WriteLine(ex.Message);
return false;
}
Client.Send(vData, vData.Length);
Client.Close();
return true;
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="vData">发送的二进制数据</param>
/// <returns></returns>
public bool SendData(byte[] vData)
{
if (!m_IsInit) return false;
return SendData(UdpBindPort, vData);
}
}
#1
居然没人回应 ! 我好着急啊
#2
分不够可以加 速度来人啊
#3
UDP客户端的Socket也可以Bind, 不信你试试
#4
如果多网卡, 你得先选定一个IP
#5
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 4001);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(iep);
socket.SendTo(.....
100%可行, 你得试一下IPAddress.Any有没有问题(或者得指定IP)
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(iep);
socket.SendTo(.....
100%可行, 你得试一下IPAddress.Any有没有问题(或者得指定IP)
#6
internal class UDPClient
{
/// <summary>
/// 本地绑定端口
/// </summary>
private int UdpBindPort;
private UdpClient Client;
/// <summary>
/// 远程通信点
/// </summary>
private IPEndPoint Point = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 10000);
private bool m_IsInit = false;
/// <summary>
/// 是否初始化
/// </summary>
public bool IsInit
{
get { return m_IsInit; }
}
/// <summary>
/// 初始化通信模块
/// </summary>
/// <param name="IP">IP地址</param>
/// <param name="BindPort">本地绑定端口</param>
/// <param name="RemotePort">远程端口</param>
public void Init(string IP, int BindPort, int RemotePort)
{
Point.Address = IPAddress.Parse(IP);
Point.Port = RemotePort;
UdpBindPort = BindPort;
byte[] SendArr = ASCIIEncoding.ASCII.GetBytes(string.Format("init {0}", m_
m_IsInit = true
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="Port">端口</param>
/// <param name="vData">发送的二进制数据</param>
/// <returns></returns>
private bool SendData(int Port, Byte[] vData)
{
try
{
Client = new UdpClient(Port);
Client.Connect(Point);
}
catch(Exception ex) {
Console.WriteLine(ex.Message);
return false;
}
Client.Send(vData, vData.Length);
Client.Close();
return true;
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="vData">发送的二进制数据</param>
/// <returns></returns>
public bool SendData(byte[] vData)
{
if (!m_IsInit) return false;
return SendData(UdpBindPort, vData);
}
}