/*timeclnt.c -a client for timeserv.c
* usage:timeclnt hostname portnumber
*/
#include
#include
#include
#include
#include
#define oops(msg) {perror(msg);exit(1);}
main(int ac,char *av[])
{
struct sockaddr_in servadd; /*the number to call*/
struct hostent *hp; /*used to get number*/
int sock_id,sock_fd; /*the socket and fd*/
char message[BUFSIZ]; /*TO RECEIVE MESSAGE*/
int messlen; /*for message length*/
/*
*step 1:get a socket
*/
sock_id=socket(AF_INET,SOCK_STREAM,0);
if(sock_id==-1)
oops("socket");
/*
*step 2:connect to server
*need to build address (host,port) of server first
*/
bzero(&servadd,sizeof(servadd)); /*zero the address*/
hp=gethostbyname(av[1]); /*lookup hosts ip #*/
if(hp==NULL)
oops(av[1]); /*or die*/
bcopy(hp->h_addr,(struct sockaddr *)&servadd.sin_addr,hp->h_length);
servadd.sin_port=htons(atoi(av[2])); /*fill in port number*/
servadd.sin_family=AF_INET; /*fill in socket type*/
if(connect(sock_id,(struct sockaddr *)&servadd,sizeof(servadd))!=0)
oops("connect");
/*
*step 3:transfer data from server,then hangup
*
messlen=read(sock_id,message,BUFSIZ); /*read stuff*/
if(messlen==-1)
oops("read");
if(write(1,message,messlen)!=messlen) /*and write to*/
oops("write"); /*stdout*/
close(sock_id);
}
阅读(741) | 评论(0) | 转发(0) |