I use bind() on an address to which I have set port value equal to 0. I know that in this way, it is bind a random port to the address. But I want that only port with value x such that (x >= 0 && x <= 1023) || (x >= 49152) were choosen, but I noticed that, among random port that can be choosen, there are also port > 49152 . However, if I re-call bind() , it gives error: invalid argument. How can I re-call bind() function without it gives the invalid argument error, or how to solve this problem in another way? Thanks a lot in advance.
bind() - how to call bind() multiple times on the same socket
5.4k Views Asked by user7836391 At
2
There are 2 best solutions below
0
Remy Lebeau
On
You cannot call bind() again on a socket that is already bound. Once a socket is bound, its binding cannot be changed.
Binding to port 0 will bind to an available random ephemeral port, and the range of ephemeral ports is controlled by the OS, not the application. Some OSes do provide configuration values to set the range, but you need to be an admin to change it.
To do what you are looking for, do not bind to port 0 at all. Bind to a specific desired port instead, and if it is not available then bind() will fail and you can handle the error by calling bind() again with a different port, repeating as needed until a binding is successful or you have exhausted your list of desired ports.
Related Questions in SOCKETS
- Node.js Server + Socket.IO + Android Mobile Applicatoin XHR Polling Error...?
- My server TCP doesn't receive messages from the client in C
- how is strncpy able to copy from source to empty destination?
- Python Multicast packet receiver stops receiving multicast packets when computer is connected to WiFi
- Python Client-Server Communication with Protocol
- Reversed TLS re-connection issue
- Android 13 & 14 seem to close WebSocket connection, if i put app in background, after ~20s
- Java SocketException: Connection reset,. What is the cause?
- Multipart/form-data with chunked data transfer (ICAP protocol)
- View Socket View
- Client connection timeout during Android & Windows PC communication via sockets
- Browser connect to raw sockets even possible?
- Protocol 43200 after unpacking received data
- Unity SocketIo using Best http2 plugin want to use in webgl
- How does pre-allocating a pool of SocketAsyncEventArgs objects upfront improve the performance of a server application in c#
Related Questions in TCP
- Java SocketException: Connection reset,. What is the cause?
- How does a server handle multiple requests, and how does is know where to send which response?
- How does pre-allocating a pool of SocketAsyncEventArgs objects upfront improve the performance of a server application in c#
- How to peek or ready to check whether HTTP request or not in TCP proxy?
- How does bash > /dev/tcp/"ip"/"port 0<&1 keep its connection alive?
- Python TCP Server that both sends and or receives data (independently) using asyncio streams?
- Can't remotely connect to my postgresql database on digitalocean
- Why my message doesn't write into the socket when I try to read the response after sending it?
- What makes MQTT a raw tcp connection that we can't run it in the browser?
- ImGui rendering wrong characters (characters received from tcp sockets)
- TCP/IP Server Using sockets Java
- C# tcp socket keepalive I want to visit a website, but the specified time is very slow and I cannot access it
- Java TCP socket want to multiple times input with one connection
- How do I receive TCP messages on an android Emulator from a physcal device
- Getting error while using the MessagePattern to communicate between microservices
Related Questions in BIND
- Why Bind9 responds with latency for RPZ rule?
- Make Bind DNS server to request only root DNS servers of IANA website
- In tcl/tk how to bind a simple quick <Double-1> (the mouse button 1 double click) to tablelist and get selected row contents?
- Froxlor default IP address for a new domain
- Why the list returned from the view model to the controller is always null?
- How to keep non-numeric columns when using apply() function on a data frame in R?
- Snowflake - Current bind variable substitution
- Dynamic hostnames with Bind9 reverse lookup
- I want to check if the socketcan is bind with roscpp
- Python tkinter: how to properly propagate a bind to a function inside childs of a widget
- Bind9 with multiple RPZ files and views causing overload on memory and CPU
- How to use mounted non-system drive as storage for database Docker container?
- boost bind error for async reading with asio
- Vue3 :style backgroundImage not working with require
- Bind message has 6545 parameter formats but 0 parameters
Related Questions in SOCKADDR-IN
- Problems with pointers to a struct sockaddr_in in C
- Delete and cast. Will delete free the right amount of bytes?
- port number of an UDP socket
- How do I set the Protocol field in an Ipv4 header to 41 in windows?
- how to provide custom IP to sockets in C?
- Can't seem to save the client's address to sourceMsgs[j]
- why sockaddr_in and sockaddr each is imcompatible?
- Unexpected compile error on specific sockaddr_in variable name
- Sockaddr union and getaddrinfo()
- Compare ipv4 address - sockaddr_in
- C - Mavlink program : sendto fails and returns EINVAL while connecting to a drone
- C sockaddr function call for sendto?
- getsockname returns -1, errno is EBADF?
- How to legally use type-punning with unions to cast between variations of struct sockaddr without violating the strict aliasing rule?
- Trying to get ipv4 from const sockaddr
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can't. You have to close the socket and start again. You can't be so picky about what port you get. They system will give you whatever it gives you.