1

situation: I need to broadcast from client using UDP from some free port, then to accept tcp connection on client from server on the port with the same number, but TCP. That's why I need to listen (and bind) to this port BEFORE broadcasting. The port cannot be const, because I can have multiple clients running on one machine. So here some questions, which can help me to make this situation clearer:

  1. If I made sendto from unbinded UDP socket, is it binded to any free port and all next sendto messages will go from this port, or each time the port will be chosen for a new message?

  2. Can I ask system to reserve some free port for me? (I need to reserve two ports with the same numbers for UDP and TCP connections)

  3. I'm sure there is a known way to handle these situations, what is it?

user207421
  • 305,947
  • 44
  • 307
  • 483

2 Answers2

2

1)If I made sendto from unbinded UDP socket, is it binded to any free port and all next sendto messages will go from this port

Yes.

or each time the port will be chosen for a new message?

No.

2) Can I ask system to reserve some free port for me? (I need to reserve two ports with the same numbers for UDP and TCP connections)

That's what happens when the auto-bind occurs. You can do explicitly by binding to port number zero, but it isn't necessary. It also does not guarantee that you can bind both UDP and TCP to the same port number.

3) I'm sure there is a known way to handle these situations, what is it?

You've found it. Let the auto-bind happen.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user207421
  • 305,947
  • 44
  • 307
  • 483
  • But how can I know, from what port to listen, if it's number should be the same as the number from which I broadcasted? (I make listen before broadcasting, and I need to bind listen socket to somewhere, that's the problem) – Gleb Makarchuk May 10 '15 at 10:44
  • 2
    Call `getsockname()` on the UDP socket, which gives you the port, then bind the TCP socket to the same port. Or else, bind the TCP socket first, t port zero, and then bind the UDP socket to the same port. Repeat until you succeed. – user207421 May 10 '15 at 10:52
  • I have warranty that the socket that was given to me by binding to zero for example from udp, will be free for tcp? Should I handle this situation? I mean, is it two different ports for UDP/TCP with the same number, or just one port, that could be used for both protocols? – Gleb Makarchuk May 10 '15 at 10:58
  • 1
    No, you don't. That's why you have to repeat until you have two bound sockets. – user207421 May 10 '15 at 10:59
0

I've found some answers on stackoverflow

  1. You can bind to 0 port, that is specified it struct semaddr_in. That will allow you get an unused port for your type of connection. It is not defined to be free for other types of connection.

Look at @remy's answer at Bind to any port available

  1. Yes, they can because headers are specified for UDP or TCP protocols. So the machine can tell one from another.

See Can TCP and UDP sockets use the same port?

  1. If you bind to 0 port you'll get ability to listen to it before calling sendto
  • Mere links are not answers here. You have to explain. – user207421 May 10 '15 at 10:59
  • 1
    If you find other questions on Stackoverflow that are duplicates, then you should flag the question as duplicate. – Mark Rotteveel May 10 '15 at 11:11
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mark Rotteveel May 10 '15 at 11:11
  • @MarkRotteveel Yeah, thank you. Added necessary info. – bu99ycr0c0d11e May 10 '15 at 11:25
  • Every particle of this is wrong. 1. 'It is not defined to be free for other types of connnection' is incorrect: it is defined to be free by `bind(),` which covers all protocols with port numbers. 2. The reason why TCP and UDP ports are distinct is because they are specified *in* the TCP or UDP header *respectively,* not the mere existence of the headers. 3. If you bind to port zero you can *either* listen to it, if it's TCP, *or* send from it: *not* both. You were told to 'include the essential parts of the answer', not to mangle it beyond all recognition. This answer should be deleted. – user207421 May 13 '15 at 22:50