Click to See Complete Forum and Search --> : tcp client port #


tecknophreak
12-03-2003, 12:48 PM
Is it possible to set the port number for a TCP client? I've been able to do it for UDP by basically telling both sides to bind to an address/port combination.

Its mostly a curious thing. I have a server which will listen on a particular port and the client will connect to that port, this is done so that the firewall on the server side will allow connections into that port. But the firewall does not care what port the client sends and receives data on.

tecknophreak
12-03-2003, 12:52 PM
k, never mind, the answer is yes

ethfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(addr.c_str());
server_address.sin_port = htons(port);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(port);
len = sizeof(address);
bind(ethfd, reinterpret_cast<sockaddr *>(&address), len);
result = connect(ethfd, reinterpret_cast<sockaddr *>(&server_address), len);