English 中文(简体)
General sockets UDP programming question
原标题:

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 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)





相关问题
Failure scenarios for reliable UDP?

What could be good list of failure scenaros for testing a reliable UDP layer? I have thought of the below cases: Drop Data packets Drop ACK, NAK Packets Send packets in out of sequence. Drop intial ...

C windows sendto()

I am trying to send over UDP using the following code, but i m getting strange results. if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAddr, sizeof(sendAddr)) == bytesSent) &...

General sockets UDP programming question

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 ...

Twisted Spread suitable for multiplayer racing sim?

Do you think that Twisted Spread may be suitable (in terms of performance) for a multiplayer racing simulator? The rest of the application is based on Python-Ogre. Can Perspective Broker run upon (...

Ruby UDP server/client test fails

I am trying to setup a simple UDP client and server using Ruby. The code looks like this: require socket.so class UDPServer def initialize(port) @port = port end def start @socket = ...

Should I use (non-blocking) NIO for UDP?

According to this post, UDP just doesn t block. Are there any advantage using the (non-blocking) NIO API for UDP? Or should I just use the easier "traditional" io API?

Connecting to a Source game server in VB.NET

I m developing an application that detects Source-based games running on the LAN. Following up on the specifications provided by Valve, I have it narrowed down to exactly what I want: making a UDP ...

热门标签