using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace C_UDP_Client
{
public class Class1
{
//Socket client;
public string buff;
public void sendMsg(Socket udpClient, string msg,string strRomoteIP,int port)
{
EndPoint point = new IPEndPoint(IPAddress.Parse(strRomoteIP), port);
int i=0;
while (i<1)
{
i++;
udpClient.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
}
public string ReciveMsg(Socket udpClient)
{
EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
byte[] buffer = new byte[1024];
int length = udpClient.ReceiveFrom(buffer, ref point);//接收数据报
string message = Encoding.UTF8.GetString(buffer, 0, length);
//message = point.ToString() +":"+ message;
return message;
}
public void CreatUDP(Socket udpClient,string strlocalIP,int port)
{
udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpClient.Bind(new IPEndPoint(IPAddress.Parse(strlocalIP), port));
}
}
}

下面资源中包含了写好的Labvew的子vi,可正常通讯并运用至实际项目中.
Simple UDP - Receiver.vi
Simple UDP - Sender.vi
本文介绍了一个使用 C# 实现的 UDP 客户端通信程序,包括消息发送与接收的功能,并提供了 LabVIEW 的子 VI 示例,可用于实际项目中。
7719

被折叠的 条评论
为什么被折叠?



