Host IP number instead of host name ?

Host IP number instead of host name ?

Post by Pete » Sat, 01 Jul 2000 04:00:00



Hi

How could I use host IP number to make connection with the srever instead of
using host name .

In my program below I am using host name to make connection with server i
want to change it to be host IP number.

Any help will be appreciated   Peter

Her is my program

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>

#define MYPORT 4950    /* the port users will be connecting to */

int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in their_addr; /* connector's address information */
    struct hostent *he;
    int numbytes;

    if (argc != 3) {
        fprintf(stderr,"usage: talker hostname message\n");
        exit(1);
    }

    if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
        perror("gethostbyname");
        exit(1);
    }

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

    their_addr.sin_family = AF_INET;         /* host byte order */
    their_addr.sin_port = htons(MYPORT);     /* short, network byte order */
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    bzero(&(their_addr.sin_zero), 8);        /* zero the rest of the struct
*/

    if ((numbytes=sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr
*)&their_addr,
                         sizeof(struct sockaddr))) == -1) {
        perror("recvfrom");
        exit(1);
    }
    printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr));
    close(sockfd);
    return 0;

Quote:}

 
 
 

Host IP number instead of host name ?

Post by Richard Cale » Sat, 01 Jul 2000 04:00:00


p> How could I use host IP number to make connection with the srever instead of
p> using host name .

p> their_addr.sin_addr = *((struct in_addr *)he->h_addr);

Becomes

   their_addr.sin_addr = inet_addr("127.0.0.1");

For maximum flexibility, do the ablve then see if their_addr.sin_addr
is negative (meaning it wsn't a numeric address), and if so do what
you now do to look it up as a hostname.

--

                                                 |<

 
 
 

Host IP number instead of host name ?

Post by Rafi Kie » Thu, 06 Jul 2000 04:00:00


Instead of saying :
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);

say just:

    their_addr.sin_addr = inet_addr(argv[1]);

(of course if argv[1] is ip address in dotted notation)

you also do not need to 'gethostbyname' anymore

                Rafi


Quote:> Hi

> How could I use host IP number to make connection with the srever instead
of
> using host name .

> In my program below I am using host name to make connection with server i
> want to change it to be host IP number.

> Any help will be appreciated   Peter

> Her is my program

> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <string.h>
> #include <sys/types.h>
> #include <netinet/in.h>
> #include <netdb.h>
> #include <sys/socket.h>
> #include <sys/wait.h>

> #define MYPORT 4950    /* the port users will be connecting to */

> int main(int argc, char *argv[])
> {
>     int sockfd;
>     struct sockaddr_in their_addr; /* connector's address information */
>     struct hostent *he;
>     int numbytes;

>     if (argc != 3) {
>         fprintf(stderr,"usage: talker hostname message\n");
>         exit(1);
>     }

>     if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
>         perror("gethostbyname");
>         exit(1);
>     }

>     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
>         perror("socket");
>         exit(1);
>     }

>     their_addr.sin_family = AF_INET;         /* host byte order */
>     their_addr.sin_port = htons(MYPORT);     /* short, network byte order
*/
>     their_addr.sin_addr = *((struct in_addr *)he->h_addr);
>     bzero(&(their_addr.sin_zero), 8);        /* zero the rest of the
struct
> */

>     if ((numbytes=sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr
> *)&their_addr,
>                          sizeof(struct sockaddr))) == -1) {
>         perror("recvfrom");
>         exit(1);
>     }
>     printf("sent %d bytes to

%s\n",numbytes,inet_ntoa(their_addr.sin_addr));

- Show quoted text -

Quote:>     close(sockfd);
>     return 0;
> }

 
 
 

1. Can't ping the host name or host IP address from host.

Hello there,
I have the unusual problem of not being able to ping the machines host name
or host IP address from the host.
I've checkout the network settings and I can't seem to find any problems.  I
am able to ping & telnet to the machine
from the network.  Seems quite strange.

Any help would be really appreciated.

thanks,

Owen.

Here is output from ifconfig


lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Bcast:127.255.255.255  Mask:255.0.0.0
          BROADCAST LOOPBACK  MTU:3584  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0

eth0      Link encap:Ethernet  HWaddr 00:60:08:83:28:08
          inet addr:10.0.0.100  Bcast:10.0.0.255  Mask:255.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:41411 errors:0 dropped:0 overruns:0 frame:0
          TX packets:259 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          Interrupt:3 Base address:0x300



PING penguin (10.0.0.100): 56 data bytes
ping: sendto: Network is unreachable
ping: wrote penguin 64 chars, ret=-1

--- penguin ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss


PING 10.0.0.100 (10.0.0.100): 56 data bytes
ping: sendto: Network is unreachable
ping: wrote 10.0.0.100 64 chars, ret=-1

--- 10.0.0.100 ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss

2. HELP: Call to signal() trashes my stack!

3. /etc/hosts (or NIS host map): official-host-name vs nicknames

4. Help 4 new user : connectting to ISP PLEASE

5. iptables block host name instead of ip address

6. Missing KDE Stuff in RH 8

7. can't ping host or host IP from host ?

8. Grep question

9. htaccess problems with host names, but not IP numbers

10. Can only access web server using IP number not host name...why?

11. No route to host and IP numbers with no domain names

12. how to change domain name, host name, ip address

13. IP Masquerading - Can't use Host names only IP address