Solved

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

Posted on 2008-10-26
2
632 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
Comment Utility
>> 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
Comment Utility
Herer we are. Your ROCK
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

743 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

18 Experts available now in Live!

Get 1:1 Help Now