Reputation: 10156
I'm using a socketConnection
which takes the TCP port number port
as a mandatory parameter. Now, how can I ensure that I specify an unused port for port
(my process is the server)?
Edits:
I know that you can specify port = 0
so that the system allocates the port for you, but I have no means of subsequently finding out which port I was given.
Moreover, socketConnection(..., server = T)
is blocking until the client process connects. But the client process doesn't know which port to connect to because the server process is being blocked and isn't able to determine the port number it got assigned. Catch 22!
Upvotes: 1
Views: 266
Reputation: 310883
It depends on whether you're creating a client socket or a server socket.
If you're creating a client socket, the port parameter species the target port. So it must be a port that is in use, as a server, otherwise you will get a connection refusal.
If you're creating a server socket, normally you will want a fixed port number, and if it's already in use it means your server is already running. If you want a random system-allocated port number for a server, and you have some other means of telling the clients what the port number is, just specify zero.
Upvotes: 2