在UDP中发送3字节消息 [英] Sending 3 bytes message in UDP
问题描述
大家好,
我正在尝试做一个小应用程序,只需将UDP中的3字节消息发送到指定的IP地址,但我不能让它起作用。
我想要的只是一个非常简单的东西,只是一个带有一个文本框的小接口,用于指定目标IP地址,一个按钮用于将单个数据包中的0x01 0x25 0xFA等3字节消息发送到在文本框中指定IP并使用代码中直接指定的固定端口。
这看起来很简单,可能很简单,但是......; o)
我尝试使用udpClient.Send命令,但似乎我真的不明白如何使用它。
你能帮助我吗?
非常感谢
Vince
<从解决方案1移至问题>
好的,我同意我的信息有点短; o)
所以,首先,我是现代发展的新手。我在10年或15年前制作了很多东西但是从那时起只做了很简单的事情。
这是我在检查了几个网站和例子后做的非工作代码(请不要笑; o))
公共 类 Form1
继承 System.Windows.Forms.Form
Dim DestinationIP As IPAddress
Dim bytCommand As 字节()= 新 字节(){}
Dim udpClient 作为 新 UdpClient
私有 Sub Button1_Click(发件人为 对象,e As EventArgs)句柄 Button1.Click
Dim pRet As Integer
DestinationIP = IPAddress.Parse(DestIP.Text)
udpClient.Connect(DestinationIP, 10242 )
pRet = udpClient.Send( 0x01 0x25 0xFA, 3 )
Console.WriteLine( 没有字节发送 &安培; pRet)
结束 Sub
结束 类
我总是在udpClient.Send行上出错。在上面的代码中,错误是类型的值''字符串''不能转换为''1维数组的字节''。
Thx
UdpClient.Send方法需要发送一个字节数组,所以行< br $>
pRet = udpClient.Send(0x01 0x25 0xFA,3)
需要替换为
pRet = udpClient.Send(new byte [] {0x01,0x25, 0xFA},3)
请注意,这是c#代码,而不是vb,所以你必须调整它。 />
仅供参考,这里有一些可用于发送的c#代码:
// 使用udp协议创建套接字
使用(套接字) sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp))
{
// 创建具有特定IP地址和端口的远程端点
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse( 10.10.10.2), 11000 );
// 将数据报发送到此远程端点
sock。 SendTo( new byte [] {0x01,0x25,0xFA}, 0 , 3 ,SocketFlags.None,endPoint);
}
或:
// 使用本地端口11000进行通信的新udp客户端
UdpClient udpClient = new UdpClient( 11000 );
// 连接到ip地址和远程端口
udpClient .Connect(IPAddress.Parse( 10.10.10.2), 11000 跨度>);
// 发送数据报
udpClient.Send( new byte [] {0x01,0x25,0xFA}, 3 < /跨度>);
// close udp client
udpClient.Close() ;
希望这会有所帮助,
Thomas。
Hi all,
I''m trying to do a small application that can simply send a 3 bytes message in UDP to a specified IP address but I cannot make it work.
All I want seems really simple, just a small interface with one textbox to specify the destination IP address and one single button to send a 3 bytes message like "0x01 0x25 0xFA" in a single packet to the IP specified in the textbox and using a fixed port specified directly in the code.
This look so simple, and is probably very simple, but... ;o)
I tried to use the udpClient.Send command but it seems I don''t really understand how to use it.
Can you help me ?
Many thanks
Vince
<Moved from Solution 1 to question>
Hi,
OK, I agree my information are a little bit short ;o)
So, first of all, I''m a newbee in "modern" developpment. I made a lot of things 10 or 15 years ago but only very simple things since that time.
Here is my non working code I made after checking weveral websites and examples (please don''t laugh ;o) )
Public Class Form1
Inherits System.Windows.Forms.Form
Dim DestinationIP As IPAddress
Dim bytCommand As Byte() = New Byte() {}
Dim udpClient As New UdpClient
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pRet As Integer
DestinationIP = IPAddress.Parse(DestIP.Text)
udpClient.Connect(DestinationIP, 10242)
pRet = udpClient.Send("0x01 0x25 0xFA", 3)
Console.WriteLine("No of bytes send " & pRet)
End Sub
End Class
I always get errors on the udpClient.Send line. On the code above the error is "Value of type ''String'' cannot be converted to ''1-dimentional array of byte''.
Thx
Hi,
the UdpClient.Send method expects an array of byte to send, so the line
pRet = udpClient.Send("0x01 0x25 0xFA", 3)
needs to be replaced with
pRet = udpClient.Send(new byte[] {0x01, 0x25, 0xFA}, 3)
Please be aware, that this is c# code, not vb, so you have to adjust it.
FYI, here is some c# code which you can use to send:
// create socket using udp protocol using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { // create remote endpoint with specific ip address and port IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("10.10.10.2"), 11000); // send datagram to this remote endoint sock.SendTo(new byte[] { 0x01, 0x25, 0xFA }, 0, 3, SocketFlags.None, endPoint); }
Or:
// new udp client that uses local port 11000 to communicate UdpClient udpClient = new UdpClient(11000); // connect to ip address and remote port udpClient.Connect(IPAddress.Parse("10.10.10.2"), 11000); // send datagram udpClient.Send(new byte[] { 0x01, 0x25, 0xFA }, 3); // close udp client udpClient.Close();
Hope this helps,
Thomas.
这篇关于在UDP中发送3字节消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!