/* Test distributed client program using UDP. */ /* This program sends a simple packet to the host and port specified on the command line. It waits for a return and then prints the result. The program should be exected with the hostname and decimal port number on the comand line. */ /* if PC == 1, then use WINSOCK */ #define PC 0 #include <iostream.h> #include <stdlib.h> #include <sys/types.h> #include <stdio.h> #if PC #include <winsock.h> #else #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #endif /* maximum host name length */ #define MAXHOSTNAME 63 /* size of input buffer */ #define INSIZE 128 #define WINSOCK_VERSION 0x0101 /* Program requires Winsock version 1.1 */ #define NO_FLAGS 0 /* No special flags specified */ void main(int argc, char *argv[]) { char *tpkt = "tclient test message"; /* message to send to host */ struct hostent *h; /* getnamebyhost info for remote */ struct sockaddr_in sa; /* socket address */ struct sockaddr_in lsa; /* socket address for host */ struct sockaddr_in fromaddr; /* host address */ int addrlen; /* length of host address */ int soxdes; /* socket descriptor */ int inlen; /* length of input message */ int sockstat; /* status of socket call */ short portnum; /* port number of server */ char inbuf[INSIZE]; /* input message buffer */ char hostname[MAXHOSTNAME+1]; /* this host's name */ #if PC WSADATA wsaData; /* Windows socket initialization data */ if (WSAStartup(WINSOCK_VERSION, &wsaData)) { puts("* Error opening Winsock *"); exit(11); } #endif /* get the port number */ portnum = 1234; /* get the local host name and address */ gethostname( hostname, MAXHOSTNAME ); if((h = gethostbyname( hostname )) == NULL) { cout<<"cannot find host address"<< hostname; exit(7); } /* build socket address structure */ memcpy((char *)&lsa.sin_addr, (char *)h->h_addr, h->h_length); lsa.sin_family = h->h_addrtype; lsa.sin_port = htons(portnum); cout<<"local host address\n"; cout<<h->h_name<<endl; cout<<inet_ntoa( lsa.sin_addr ); /* get remote host address */ h = gethostbyname("jade.ncat.edu"); if (h == NULL) { perror("gethostbyname"); exit(5); } /* build remote socket address structure */ memcpy((char *)&sa.sin_addr, (char *)h->h_addr, h->h_length); sa.sin_family = h->h_addrtype; sa.sin_port = htons(portnum); /* allocate an open socket */ soxdes = socket(AF_INET, SOCK_DGRAM, 0); if (soxdes == -1) { perror("socket"); exit(7); } else cout<<"socket created. result is \n"; cout<<soxdes; /* bind the socket to the specified port */ if (bind( soxdes, (struct sockaddr *)&lsa, sizeof lsa) < 0) { perror("bind"); exit(5); } cout<<" and bound to port \n"; cout<<ntohs(lsa.sin_port); /* send a packet to the server */ sockstat = sendto(soxdes, tpkt, strlen(tpkt)+1, NO_FLAGS, (struct sockaddr *)&sa, sizeof sa ); if (sockstat < 0) { perror("sendto"); exit(5); } puts("packet sent to server"); /* wait for a reply */ addrlen = sizeof(fromaddr); if ((inlen = recvfrom(soxdes, inbuf, INSIZE, 0, (struct sockaddr *)&fromaddr, &addrlen)) <= 0) { #if PC sockstat = WSAGetLastError(); printf("recv error code %d\n", sockstat ); #else perror("recvfrom"); #endif } else cout<<"byte reply from server\n"<< inlen << inbuf; #if PC WSACleanup(); /* Free all allocated program resources */ #else close(soxdes); #endif }