Setting source port on a Java Socket?
java, sockets
Solution
Yes, use the `bind()` method. This mirrors the `bind()` function available in most C-level socket implementations. Note that you can't always choose freely which port to use, on some system some ranges are reserved and considered off-limits to user applications.
Problem
I'm very new to socket programming: Is it possible to explicitly set the the source port on a Java Socket? I am working on a client/server application in which clients could potentially be listening for replies from the server on several ports. It would be nice if I could set this reply port on the client side when initializing the Socket, so that the server would be able to determine which port to reply to on the other side.