?
Solved

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

Posted on 2008-10-26
2
Medium Priority
?
653 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
[X]
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
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

777 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