Set both LOCAL and TARGET port on socket... (C not C++ - Windows XP Pro - LCC Compiler)

Hi everyone!
                 I have written a socket server program that is working just fine. However, I want to be able to go OUT through my firewall on one specific port "6190" and hit the target server program on port "62781".
I can hit the target server program on port "62781" fine and establish a working connection great but I want to go out on this specific local port "6190" and not just whatever port Windows slaps me on!

Hope someone has a clue as I seem to have exhausted my search words in google.

Thanks, Ralph.

RalphGAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RalphGAuthor Commented:
Oh maybe this will help, it's what I have that's working but without local port specification:

client_s = socket(AF_INET, SOCK_STREAM, 0);

server_addr.sin_family      = AF_INET;                      // Address family to use
server_addr.sin_port        = htons(PORT_NUM);        // Port num to use
server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use

if (connect(client_s, (struct sockaddr *)&server_addr, sizeof(server_addr)) == 0){
         printf("Connected to Server Program\n"); ...
0
alexscheeleCommented:
To do this you would need to bind your socket to a local port.


client_s = socket(AF_INET, SOCK_STREAM, 0);

local_addr.sin_family = AF_INET;                // Address family to use
local_addr.sin_port = htons(portnum);        // Local port

if (bind(client_s, (struct sockaddr *)&local_addr, sizeof(local_addr)) != 0) {
    // bind failed
    return(NULL);
}

server_addr.sin_family      = AF_INET;                      // Address family to use
server_addr.sin_port        = htons(PORT_NUM);        // Port num to use
server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use

if (connect(client_s, (struct sockaddr *)&server_addr, sizeof(server_addr)) == 0){
         printf("Connected to Server Program\n"); ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RalphGAuthor Commented:
Alexscheele you are a life saver!

Thanks for your quick reply and for taking the time to actually write a coded example.
I changed my code to include the initial "bind" and it worked a treat.
I checked the lot with netstat and my server program also returns the port that the client left on and all is well.

Cannot thank you enough but here's another one anyway.

Thanks Ralph.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.