Solved

General Questions About connect() to Remote Hosts - getaddrinfo() for C/C++

Posted on 2008-10-26
2
636 Views
Last Modified: 2012-05-05
Ok i will tell you what i know & you let me know if i'm right or not: Just Yes's & No's!

getaddrinfo(NULL, "3457", &hints, &results);
//use bind()

this above code, is written when you want to listen on a port "3457" from incoming connections - clients.

1. Is this right?

--------------------------------------------------------------------------------

getaddrinfo(remoteIP, NULL, &hints, &results);
.
.
.
.
connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);

This connects to a remote host - remoteIP ....
Once connect succeeds, it will specify a random local port for us automatically, so the server knows on which port to respond.
We do this when we want to connect to a server.

2. Is this right?

--------------------------------------------------------------------------

To connect to a remote host, we have to specify on which port
should we connect to, so the above code would be:

getaddrinfo(remoteIP, NULL, &hints, &results);

//ai_addr of type struct sockaddr only
//since struct sockaddr does not have element to carry a port
//we have to cast it sockaddr_in or sockaddr_in6 - depends.
struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;

//now fill the port you want to connect to
specifyPort->sin_port = htons(REMOTEPORT);

//ai_addr now contains the remoteIP + MYPORT, so make connection
//if no port specified, connection would fail.
connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
.
.
.

3. Is this right?

------------------------------------------------------------------------------

Some say, to specify a port for remoteIP, all you need to do is

getaddrinfo(remoteIP, REMOTEPORT, &hints, &results);

I do not think this would work, as REMOTEPORT would be considered
as local port because 2nd Parameter only made to specify a local port.

4. Is this right?
0
Comment
Question by:F-J-K
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22810799
>> 1. Is this right?

It's only a small part of the code you need, but yes, that's one way (if you want to listen on all interfaces on port 3457 - the AI_PASSIVE flag has to be set then - and the other hints fields need to be properly set too).


>> 2. Is this right?

Almost. You didn't specify the remote port that you want to connect to (second argument of getaddrinfo).


>> //since struct sockaddr does not have element to carry a port

Yes, it does : in the sa_data field. Take a look at sockaddr_in for an expanded view.


>> 3. Is this right?

You can just specify the port as the second argument of getaddrinfo. No need to make things complicated.


>> as local port because 2nd Parameter only made to specify a local port.
>> 
>> 4. Is this right?

getaddrinfo is simply used to retrieve information for building the sockets. It's the call to either bind or connect after that that decides how this information is used.
So, no : that's not right.
0
 
LVL 1

Author Closing Comment

by:F-J-K
ID: 31510160
Herer we are. Your ROCK
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 197
Best book to learn C++ 4 70
Dialogbox API leak? 18 82
Meaning of "exceptions()" in "ist.exceptions( ist.exceptions() | ios_base:: badbit);" 3 56
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now