Solved

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

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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C Programming - If Statement 8 83
Memory going from 12gb to 64gb or 96gb. worth it? 15 214
find Speed using GPS latitude & longitude 4 95
MySQL recovery 7 33
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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