Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Code Project Community,

I am trying to use UDP, but I have the following questions:

1) I am trying to build a function that would recieve a simple UDP packet with a string like "/location 7 5" and parse out the float value 7 and 5 out and store it into an array. Are there any examples on how to do this?

2) After trying to use OSCPacket from https://code.google.com/p/oscpack/ for hours, I cannot get past any compiler errors shown here: http://i.tylian.net/untitloxo.png

As the error message suggests, the error comes from the OSCPack and not my code. Is anyone familiar with this or is there a better method to implement UDP packet transfers?

3) I am only using the ws32.dll library, but are there any other libraries I should be using besides that?

Feel free to respond to some or all of the multipart question and let me know if more detail is needed! Also, my target platform is Windows 7 64-bit.

Thank you very much for your time!
Posted
Comments
Mohibur Rashid 13-Mar-13 1:32am    
You are transferring number, then why udp? why not tcp?
Member 9906083 13-Mar-13 1:47am    
The reason is because I am trying to do a one-way transfer of commands, basically of many xy coordinates, so I thought udp would be simplier to implement.
Mohibur Rashid 13-Mar-13 1:53am    
UDP Is best fit for media like stream, such as audio or video. Here you would need loss less data transmission. so, your best choice would be TCP.
Member 9906083 13-Mar-13 2:26am    
I actually am using a module that is already written to send out UDP packets for me, and unfortunately, the client does not want to change their code.

Do you think maybe somehow I can catch a UDP packet with TCP?
Mohibur Rashid 13-Mar-13 2:35am    
I dont have deep knowledge TCP and UDP internal work way. I am not sure whether you can do what you want to do, anyway, go to google and look for UDP example

The WinSock library provides all required functions. There is no need to use other packages. See one of the plenty WinSock FAQs and Tutorials in the web on how to setup an UDP listener. After setting up the listener, just call WSARecvFrom().

The problem is that you don't know when and if data are coming in. So you should use a worker thread for receiving using overlapped IO (if WSARecvFrom() is pending, wait for the IO event and call GetOverlappedResult()). But this is also handled by the FAQs and tutorials.

Another problem is that your UDP packets may vary in their length. It is common practice to use some kind of header that identifies the type and size of data. But with short strings, you should get them with one read.
 
Share this answer
 
Addressed your questions by number:
1. There's a ton of examples of this on the internet, just google "UDP C++ example" and you'll get a ton of hits. Basically you just set up your socket and just start listening (UDP doesn't really have a handshake process), every packet is a set of bytes (a datagram), it's up to you to cast these bytes onto a structure that breaks it out into interpreted data.

2. You can use the Windows sockets as described in Solution 1 or you can use a third-party library. Usually third party libraries just try to make the process easier or provide cross-platform sockets. Can't help you with this particular library but looking at the error, you probably have some sort of setup problem (didn't include files you needed or something similar).

3. MSDN tells you everything you need to know.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900