Click here to Skip to main content
11,433,352 members (59,687 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C# Win7 VB.NET
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
Posted 27-Mar-13 3:03am
Edited 27-Mar-13 3:51am
v2
Comments
ThePhantomUpvoter at 27-Mar-13 8:10am
   
"cannot make it work" is not a helpful description of any kind of a problem. Where is the code that demonstrates your problem?
"Can you help me ?", the only actual question asked, probably, maybe, but you did not define what your actual problem is so the real answer is, "not at the present time".
Jochen Arndt at 27-Mar-13 8:16am
   
In addition to the above comment, please tell us which language you are using or better adjust the tag (only one of C++, C#, and VB).
ThePhantomUpvoter at 27-Mar-13 8:36am
   
Your error is simple. You are trying to send a string when Send is expecting an array of bytes. Either convert the string to an array of bytes or use an array of bytes. Reading the documentation for the Send method is a good place to start. http://msdn.microsoft.com/en-us/library/08h8s12k.aspx
MadeByVince at 27-Mar-13 8:44am
   
Thank you for the answer and sorry for the bad post... I didn't saw there was different places for comments and solution...
On the conversion point of view, I was this but I cannot find the right way to do the good convertion, the message I'm trying to send is what the device at the other end is expecting. I just know it wants a 0x01 0x25 0xFA in a single UDP packet to do what I want but I cannot find the right way to send it on the correct format.
I also tried with the message in a textbox and this conversion :
MyMessage = Encoding.ASCII.GetBytes(TextToSend.Text)
pRet = udpClient.Send(MyMessage, MyMessage.Length)
But when doing this I have a 14 bytes message...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
MadeByVince at 27-Mar-13 8:54am
   
Hi Thomas,
Many thanks for your help.. I'll try that !
Thanks again,
Vince.
MadeByVince at 27-Mar-13 10:04am
   
Hey Thomas,
I got my app working as expected thanks to your help !!!
Many thanks again...
Vince.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100