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
/
TCPtimeclient.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-04-03
|
2KB
|
80 lines
/**********************************************************************************
* Programma: TCPtimeclient.c *
* *
* finalita': si connette con il server e legge il valore di time *
* il server e' TCPtimeserver.c *
* *
* Sintassi: TCPtimeclient IPserver [ portserver ] *
* IPserver - IP del server in dotted notation *
* portserver - port number del server *
* portserver e' opzionale, se non passato si usa TIME_PORT *
* *
***********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SIZE 1024
char buf[SIZE];
#define TIME_PORT 3123
int main(int argc, char **argv)
{
int sockfd;
int nread, port, err;
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr, "usage: %s IPaddr [ portserver ]\n", argv[0]);
exit(1);
}
// apro il socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror(NULL);
exit(2);
}
// assegno la struttura indirizzo del server (argomento di
serv_addr.sin_family = AF_INET;
err = inet_aton(argv[1], &serv_addr.sin_addr);
if (err<0)
{
printf("client errore inet_addr \n");
exit(1);
}
// Controlla gli argomenti passati nella command-line (port)
if (argc > 2) port = atoi (argv[2]); // converte in binario
else port = TIME_PORT; // default port number del server
if (port > 0) serv_addr.sin_port = htons((u_short)port);
else {
printf ("client errore port number %s/n", argv[1]);
exit (1);
}
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
perror(NULL);
exit(3);
}
printf("connesso con port %d IP %s\n", ntohs(serv_addr.sin_port), inet_ntoa(serv_addr.sin_addr));
nread = read(sockfd, buf, SIZE);
write(1, buf, nread);
close(sockfd);
exit(0);
}