Link to home
Start Free TrialLog in
Avatar of RalphG
RalphG

asked on

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.

Avatar of RalphG
RalphG

ASKER

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"); ...
ASKER CERTIFIED SOLUTION
Avatar of alexscheele
alexscheele

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RalphG

ASKER

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.