How to UDP send and receive on same port?

前端 未结 2 723
故里飘歌
故里飘歌 2021-02-14 02:21

I need to be able to send and receive UDP packets on the same port. I am able to listen, on say port 5000, but my send uses a random high port. The system I am working written i

相关标签:
2条回答
  • 2021-02-14 02:43

    System consists of a number of nodes listening on same port (different ip addr's). System [A] sends datagram to System [B]. System [B] asynchronously responds and send datagram(s) back to [A] all using same port. Even if [B] identifies [A]'s port, [A] is not listening on that port

    I'm not sure I understand the "all using the same port" phrase in that sentence. If A sends a datagram to B, B will know A's IP and port right away (a quick check of your library documentation reveals OnRawData has a struct sockaddr *sa parameter, if you cast it to sockaddr_in* you'll be able to extract the IP:port pair). You can use that IP:port to send datagrams to and A will receive them. A is not "listening" on that port in the sense that it haven't called listen() on the socket, but since A owns a socket that is bound to that port (whether explicitly by calling bind() or assigned random port by the OS) it will receive the data.

    Now if you want ALL your communication between nodes to go through your fixed port, you can do that. You just have to send all your datagrams through your "listening" socket. If every node "listens" on the same port, it means every node owns a socket that is bound to that port. If you want datagrams sent from A to B to appear coming from this fixed port you have to send them through that socket. I'm guessing that's why bind() doesn't work for your sending socket - A has a socket bound to port X, then you create another socket and try to bind it to the same port X, bind() fails since the port is already taken (and you don't check for errors :), and then the OS assigns random free port above 1024.

    Note 1: I use "listening" in quotes everywhere, because the concept is not very clear in the context of UDP sockets. Once you have created socket and bound it to a port, either by calling bind() explicitly or by sending data and letting the OS bind it to a port, you can receive data from everywhere through it. No listen() or accept() calls needed.

    Note 2: You say that UdpSocket::Open() calls connect(), but that doesn't make much sense - connect() does very little for UDP sockets - it merely establishes a default address so you can use send() instead of sendto() and not specify address on every send.

    Hope that clears things up.


    Edit to address OP's comment: I've never used this library but according their UdpSocket documentation there are 4 overloads of the Bind() method and every single one of them accepts port in some way. None of them works for you?

    0 讨论(0)
  • 2021-02-14 02:44

    A bidirectional communication link always involves two participants: a server-side and a client-side.

    The client expects to communicate to a server on a defined port: that's why on the server-side one must bind() to a socket.

    On the client-side, one must open a socket to the server: it doesn't really matter which socket is chosen (except for the need for it to be free).

    In other words, don't try to specify a socket on the client-side: the network protocol stack will assign it to your client.

    0 讨论(0)
提交回复
热议问题