home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
GianuzziV
/
SO1
/
cerca_server.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-03-28
|
2KB
|
63 lines
/* cerca_server.c
sintassi: cerca host server
scrive l'indirizzo in dotted form dell'host indicato e la
well known port del server indicato.
esempio cerca olivia ftp
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
main (argc, argv)
int argc;
char **argv;
{ int sockfd; /* socket descr. */
int len; /* n.ro byte dati ricevuti */
struct sockaddr_in sa; /* indirizzo Internet */
struct hostent *hp; /* risultato host name lookup */
struct servent *sp; /* risultato service lookup */
char buf[BUFSIZ]; /* informazioni da whois */
char *myname; /* nome di questo eseguibile */
char *host; /* nome dell'host remoto */
char *server; /* nome del server remoto */
myname=argv[0];
if (argc != 3) {
printf("Usage: %s host server \n", myname);
exit(1);
}
host=argv[1];
server=argv[2];
if (( hp=gethostbyname(host) )==NULL) {
printf("%s: %s: no such host \n", myname, host);
exit(1);
}
printf("%s: nome host:%s indirizzo: %s \n", myname, hp->h_name,
inet_ntoa(ntohl(hp->h_addr)));
/* costruisce la struttura che contiene l'indirizzo dell'host
da contattare, e il tipo di indirizzamento usato */
bcopy((char *) hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
sa.sin_family=hp->h_addrtype;
/* chiede qual'e' il well known socket number per il servizio server
(localmente) */
if (( sp=getservbyname(server,"tcp"))==NULL) {
printf ("%s: %s: no such server on this host \n", myname, host);
exit(1);
}
/* inserisce il socket number dentro la struttura del socket */
sa.sin_port=sp->s_port;
printf("%s: server %s sulla porta numero: %d\n", myname, server, ntohs(sa.sin_port));
}