Main Topics
Browse All TopicsBeen looking for this for a while. I need a library that allow you to set the local port number when creating an outgoing TCP connection. Winsock won't do it because it always assign a random local port when binding. Thanks in advance!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
How about:
http://www.cpp-home.com/tu
?
It's a shame you're not using .NET for this... it's easy with that.
>>Winsock won't do it because it always assign a random local port when binding
Not true.
The correct sequence of calls is :-
1. socket() to get a socket
1a. setsockopt() to set any optional parameters
2. bind() to set the local address and local port
3. connect() to connect to the destination
see the Bind() funtion in the WinSock Platform SDK on msdn.microsoft.com
If i remember my studies correctly binding an outgoing to a specific port can cause problematic situations. Binding usually only occurs on a listener and the sender grabs whatever is avaliable. (since most of the time the port number will be locked when you release it for about 255 seconds).
C++ works on windows and unix so why will InteractiveMind's solution not work?
If you do not want it in C++ what language are you using?
Business Accounts
Answer for Membership
by: giltjrPosted on 2006-01-09 at 19:35:21ID: 15656896
I'm not a Windows programmer but normally when you get a randmon port it means that you did not specify a specific port. I mean Web browers that run on Windows specific port when they run, Ftp servers get specific ports when they start, telnet servers get specific ports when they run.
Are you specificing a specific port to use when you do the bind?