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: read() error - Bad Address - comp.unix.programmerI'm writing a basic server that handles multiple clients. Whenever a client is connected, a new ... in handle_client: Bad address CLIENT ... Find the host IP address ... last ... fork (UDP) server - comp.unix.programmeri'm writing a UDP protocol to transfer file, I ... to fork the server process for any new client. A code ... demultiplexes accordingly to the client's inet address {ip,port ... Sending UDP packets over Ethernet - comp.arch.fpgaI've been tasked to write some code for an FPGA to ... find the destination MAC given the IP address. Since you seem to already ... an handshaking over udp, the client sends ... Windows Time with NTPv4 - comp.protocols.time.ntp... did) work similar to what you write now ... with a time server host name or IP address, will first issue an NTP client mode ... Is this what the "windows hack" code already does? Encapsulation in VPN - comp.dcom.sys.ciscoThe PPTP is using for client to server. IPSec can be ... sells best, or what is part of something that already ... ports - comp.dcom.sys ... interface Async65 no ip address ... Sockets in gfortran? - comp.lang.fortran... Find the host IP address ... fortest.f90 tcpc.o client.o -lws2_32 fortest.f90 ! To test Richard Maine's TCP C code subroutine write ... if it isn't > already ... w32tm DC error - comp.protocols.time.ntpCommand "netstat -na" shows [ip-address]:123 > If you ... run on ordinary Windows machines. > NTPd already does ... matter which software you will try, even if you write ... Operation not permitted when trying to contact time server - comp ...... to set the time, and has presumably already bound ... Actually, the server IP address, client IP address, well ... depends what you're trying to do. Are you writing C code? 806 refusing to cooperate. - comp.dcom.sys.cisco... processor board System flash (Read/Write ... queue 100 out ! interface Ethernet1 ip address dhcp client ... up the first 6 characters of the MAC address which is the vendor code. Bad File Descriptor - comp.protocols.time.ntp... client. > Thanks for the idea, but public servers still have static IP address ... interface IP address changes, because the current code ... from it or write ... dhcp-options - Linux Command - Unix CommandSo you may need to write: option dhcp-client-identifier ... for reference by people who already understand the Client ... option new-name code new-code = ip-address; An option ... Linux Howtos: C/C++ -> Sockets TutorialIf this port is already in use on that ... For server code, this will always be the IP address of the machine on ... instead of read**** and write****, the client uses ... 7/22/2012 5:40:36 AM
|