开发者

General sockets UDP programming question

开发者 https://www.devze.com 2022-12-12 04:46 出处:网络
I have an FPGA device with which my code needs to talk.The protocol is as follows: I send a single non-zero byte (UDP) to turn on a feature.The FPGA board then begins spewing data on the port from wh

I have an FPGA device with which my code needs to talk. The protocol is as follows:

I send a single non-zero byte (UDP) to turn on a feature. The FPGA board then begins spewing data on the port from which I sent.

Do you see my dilemma? I know which port I sent the message to, but I do not know from which port I sent (is this port not typically chosen开发者_JS百科 automatically by the OS?).

My best guess for what I'm supposed to do is create a socket with the destination IP and port number and then reuse the socket for receiving. If I do so, will it already be set up to listen on the port from which I sent the original message?

Also, for your information, variations of this code will be written in Python and C#. I can look up specific API's as both follow the BSD socket model.


This is exactly what connect(2) and getsockname(2) are for. As a bonus for connecting the UDP socket you will not have to specify the destination address/port on each send, you will be able to discover unavailable destination port (the ICMP reply from the target will manifest as error on the next send instead of being dropped), and your OS will not have to implicitly connect and disconnect the UDP socket on each send saving some cycles.


You can bind a socket to a specific port, check man bind


you can bind the socket to get the desired port.

The only problem with doing that is that you won't be able to run more then one instance of your program at a time on a computer.


You're using UDP to send/receive data. Simply create a new UDP socket and bind to your desired interface / port. Then instruct your FPGA program to send UDP packets back to the port you bound to. UDP does not require you to listen/set up connections. (only required with TCP)

0

精彩评论

暂无评论...
验证码 换一张
取 消