Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-10-26
2
642 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 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