Writing Client code, if IP address already knew

  • Follow


Hi,

suppose we know exactly what the IP address of the server is, but don't
know what its alias is, what kind of change do we have to make to the
client code to connect to such IP address? (instead of using
gethostbyname()


//***********************************
//GETTING INFORMATION ABOUT SERVER

	if ((server=gethostbyname(SERVER_NAME)) == NULL) {
		perror("gethostbyname");
		exit(1);
	}

	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
		perror("socket");
		exit(1);
	}

	//assigning protocol and characteristic about communication

	server_addr.sin_family = AF_INET;
	server_addr.sin_port   = htons(SERVER_PORT);
	server_addr.sin_addr   = *((struct in_addr *)server->h_addr);
	memset(&(server_addr.sin_zero), '\0', 8);


       if ( connect(sd, (struct sockaddr *)&server_addr, sizeof(struct
sockaddr)) == -1) {
            perror("connect");
            exit(1);
	}
	//messages
	printf("Successfully connected to the server\n");
	printf("Host name  : %s\n", server->h_name);

0
Reply shivesh (11) 4/5/2005 3:07:27 PM

shivesh@gmail.com schrieb:
> Hi,
> 
> suppose we know exactly what the IP address of the server is, but don't
> know what its alias is, what kind of change do we have to make to the
> client code to connect to such IP address? (instead of using
> gethostbyname()
gethostbyname() handles IP address literals just fine, if the server is
running on a node with the IP 172.16.0.1 and SERVER_NAME contains
"172.16.0.1", the below code does the obvious thing and converts that to
a binary IP representation.
An alternative for the same example IP would be:

server_addr.sin_addr.s_addr = htonl( 172 << 24 | 16 << 16 | 1 );

But that doesn't exactly enhance the code's legibility, precludes a
readable constant definition and adds unnecessary obstacles to possible
IPv6 migration.
Speaking of which, apart from making such a migration easier, another
alternative is to use getaddrinfo() instead, even for IPv4-only code, as
it provides a much cleaner interface than the getXXXbyYYY() family of
functions:

> //***********************************
> //GETTING INFORMATION ABOUT SERVER
> 
> 	if ((server=gethostbyname(SERVER_NAME)) == NULL) {
> 		perror("gethostbyname");
> 		exit(1);
> 	}
> 
> 	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
> 		perror("socket");
> 		exit(1);
> 	}
> 
> 	//assigning protocol and characteristic about communication
> 
> 	server_addr.sin_family = AF_INET;
> 	server_addr.sin_port   = htons(SERVER_PORT);
> 	server_addr.sin_addr   = *((struct in_addr *)server->h_addr);
> 	memset(&(server_addr.sin_zero), '\0', 8);
> 
> 
>        if ( connect(sd, (struct sockaddr *)&server_addr, sizeof(struct
> sockaddr)) == -1) {
>             perror("connect");
>             exit(1);
> 	}
All of the above would simply become like the below, note that you don't
have to care about the individual members of sockaddr_XXX at all.  Sorry
for the C++isms, I don't speak C too well.

addrinfo hints = { 0 };
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSRV;
hints.ai_family = AF_INET;
hints.ai_protocol = SOCK_STREAM;
addrinfo *res = 0;
if ( int error = getaddrinfo( SERVER_IP, SERVER_PORT, &hints, &res ) )
{
    printf( "getaddrinfo: %s\n", gai_strerror( error ) );
    exit( 1 );
}
// further error checking omitted
sd = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
connect( sd, res->ai_addr, res->ai_addrlen );
freeaddrinfo( res );

Cheers,
Malte
0
Reply Malte 4/5/2005 4:23:07 PM


Hello,

> > suppose we know exactly what the IP address of the server is, but
don't
> > know what its alias is, what kind of change do we have to make to
the
> > client code to connect to such IP address? (instead of using
> > gethostbyname()
>
> gethostbyname() handles IP address literals just fine, if the server
is
> running on a node with the IP 172.16.0.1 and SERVER_NAME contains
> "172.16.0.1", the below code does the obvious thing and converts that
to
> a binary IP representation.

This is correct on most modern Unix I gess. But on some older OS (for
instance, Tru64 4.0F), gethostbyname() shall fail to recognize that the
string passed is an address literal... On such system, one must
explicitely check for such an address, and convert it to binary IP
representation using e.g. inet_addr().

> An alternative for the same example IP would be:
>
> server_addr.sin_addr.s_addr = htonl( 172 << 24 | 16 << 16 | 1 );

Why not use inet_pton() (or inet_addr() if the OS in question doesn't
support that function). 


Cheers,
Loic.

0
Reply loic 4/6/2005 7:54:53 AM

loic-dev@gmx.net wrote:
> Hello,
> 
> 
>>>suppose we know exactly what the IP address of the server is, but
> 
> don't
> 
>>>know what its alias is, what kind of change do we have to make to
> 
> the
> 
>>>client code to connect to such IP address? (instead of using
>>>gethostbyname()
>>
>>gethostbyname() handles IP address literals just fine, if the server
> 
> is
> 
>>running on a node with the IP 172.16.0.1 and SERVER_NAME contains
>>"172.16.0.1", the below code does the obvious thing and converts that
> 
> to
> 
>>a binary IP representation.
> 
> 
> This is correct on most modern Unix I gess. But on some older OS (for
> instance, Tru64 4.0F), gethostbyname() shall fail to recognize that the
> string passed is an address literal... On such system, one must
> explicitely check for such an address, and convert it to binary IP
> representation using e.g. inet_addr().
> 
> 
>>An alternative for the same example IP would be:
>>
>>server_addr.sin_addr.s_addr = htonl( 172 << 24 | 16 << 16 | 1 );
> 
> 
> Why not use inet_pton() (or inet_addr() if the OS in question doesn't
> support that function). 
> 
> 
> Cheers,
> Loic.
> 

Why are you doing gethostbyname()???  If you already have the IP 
address, then you already know how to contact the client.  If the IP is 
a string, you can use inet_addr() to do the conversion to a format 
compatible with setting a struct inaddr.

--

Fletcher Glenn

0
Reply Fletcher 4/6/2005 7:44:10 PM

Hi everybody. Thank you for all your replies! I think this is it,
right?

//LIBRARIES
#include <windows.h>
#include <winsock2.h>
/*There are still other libraries needed to be included from CLP*/


//PROJECT PARAMETERS
#define SERVER_IP "10.10.12.102"
#define SERVER_PORT	   34000
#define BUFFER_SIZE    10000

int main(void)
{
	//windows socket startup
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0){
		return FALSE;
	}

	//attempting to get DSP's socket descriptor
	int dsp_sd;
	if ((dsp_sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
		perror("socket");
		exit(1);
	}

	//getting information about PPC: IP address and PORT number
	struct sockaddr_in PPC_addr;
	PPC_addr.sin_family = AF_INET;
	PPC_addr.sin_port   = htons(SERVER_PORT);
	PPC_addr.sin_addr   = inet_addr(SERVER_IP);
	memset(&(server_addr.sin_zero), '\0', 8);

0
Reply shivesh 4/6/2005 8:55:12 PM

//LIBRARIES

//socket libraries
#include <winsock.h>							//windows socket programming functions
#pragma comment(lib, "wsock32.lib")				//to eliminate external resolve
problem

//standard libraries
#include <iostream>								//for input and output
using namespace std;


/*There are still other libraries needed to be included from CLP*/




//PROJECT PARAMETERS
#define SERVER_IP	   "10.10.10.89"			//really is "10.10.12.102"
#define SERVER_PORT	   34000
#define BUFFER_SIZE    10000

int main(void)
{
	//windows socket startup
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0){
		cerr << "Error!" << endl;
	}

	//attempting to get DSP's socket descriptor
	SOCKET dsp_sd;
	if ((dsp_sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
		perror("socket");
		exit(1);
	}

	//getting information about PPC: IP address and PORT number
	struct sockaddr_in PPC_addr;
	PPC_addr.sin_family = AF_INET;
	PPC_addr.sin_port   = htons(SERVER_PORT);
	PPC_addr.sin_addr.s_addr  = inet_addr(SERVER_IP);
	memset(&(PPC_addr.sin_zero), '\0', 8);

	//connecting to PPC
	if ( connect(dsp_sd, (struct sockaddr *)&PPC_addr, sizeof(struct
sockaddr)) == -1) {
            perror("connect");
            exit(1);
	}
	cout << "Successful connection!" << endl;

	//closing all sockets and exit
	WSACleanup();
	return 0;
}

0
Reply shivesh 4/6/2005 9:31:50 PM

5 Replies
214 Views

(page loaded in 0.187 seconds)

Similiar Articles:













7/22/2012 5:40:36 AM


Reply: