*
* sender_udp hostname [nodename]
* Serviced by udp_echo
* Run in CTOS and UNIX. Compile with DCTOS for CTOS.
* It sends a fixed number of packets to UDP port 4096 at 
* the specified hostname. 
* If in CTOS the parameter nodename specify the node of the 
* TCPIP system service to be used. 
*
*  HISTORY OF CHANGES *
*  UCF 17648039: Socket example used to not compile under BTOS C. *


include <stdio.h>
include <systypes.h>
include syssocket.h
include netinetin.h
include netdb.h


ifndef CTOS
 if defined( __BTOSC__)
  define CTOS
 elif defined(__CTOS__)
  define CTOS
 endif
endif



define BUFSZ       16
define ECHOPORT    4096         
define MYPORT      2999
define LOOPCNT     100
define TIMERVAL	20

extern unsigned int errno;   * global error code returned from system calls *
char inbuff[BUFSZ];          * input buffer  *
char *sendbuf=ECHO REQUEST;
char * nodename = local;


main(argc, argv)
int argc;
char *argv[];
***************************
 * argv[0] = program name
 * argv[1] = host name
 * argv[2] = nodename (CTOS only)
 *
{
int i, s, n;
int lost_cnt, error_cnt;
int serverAddrLen;
struct sockaddr_in server, client;  * server and client socket address *
struct hostent *hp;          * pointer to structure containing host address *

ifdef CTOS
        if (argc<2||argc>3) {    * verifiy correct number of arguments  *
	        printf(usage: \n);
            printf(\nsender_udp\n);
            printf(hostname\n);
            printf([nodename]\n\n);
		    exit(0); 
        }
		
		if (argc==3)
			nodename=argv[2];

		n = setctosparams(nodename,0);
		CheckP(n,setctosparams);

else
		if (argc=2){ 
			printf(usage:\n);
			printf(\nsender_udp hostname\n);
		}
endif

        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        CheckP(s, socket,0);
 

ifdef CTOS
		n = setctosudpparams(s, NULL , TIMERVAL, 0);
		CheckP(n,set udp params\n,0);
endif


        client.sin_family = AF_INET;
        client.sin_addr.s_addr = htonl (INADDR_ANY);
        client.sin_port = htons (MYPORT);
       n = bind(s,client,sizeof(client));
		CheckP(n,Bind,0);

        server.sin_family = AF_INET; 
        server.sin_port = htons(ECHOPORT); 

* take the host name entered as an argument to this program
 *  and uses it to get the address of the remote host we connect to.
 *
        hp = gethostbyname(argv[1]);
        if (hp == NULL) {
        printf(invalid host argument = s\n,argv[1]);
        exit(1);
        }
        CheckP( (hp==NULL ? 1 : 0) , gethostbyname,0);
        
        memcpy((char*)server.sin_addr.s_addr, hp>h_addr, 4);
        serverAddrLen=sizeof(server);

		lost_cnt=0;
		error_cnt=0;

        for ( i = 1; i < LOOPCNT+1; i++) {

ifdef CTOS
	        n = recvfrom(s, inbuff, sizeof(inbuff),MSG_NOBLOCK | MSG_POSTREQ, server, serverAddrLen);
            if (errno==EWOULDBLOCK)
				n = 0;
            CheckP(n,POSTING RECVFROM,i);
endif	
            n=sendto(s, sendbuf, 12, 0, server, serverAddrLen);
            CheckP(n,SEND, i);
			printf(\nsend packet d,i);
			
            n = recvfrom(s, inbuff, sizeof(inbuff),0, server, serverAddrLen);
			if (n>0){
				if (strncmp(sendbuf,inbuff,12)){ 
					error_cnt+=1;
					printf (\tpacket received with error);
				}
				else
					printf (\tpacket received ok);
			}
ifdef CTOS
			else if (errno==ETIMEOUT){
				errno=0;
				n=0;
				lost_cnt+=1;
				printf (\t packet lost);
				if (lost_cnt==10  i==10){
					printf (\n\tHost s not responding\n,argv[1]);
					exit(1);
				}
			}
endif
			else
				CheckP(n,RECVFROM,i);
		}

		printf (\n\n\tHost s is alive:\n,argv[1]);
		printf (d packets sent  d packets lost d packets with error\n, i1, lost_cnt, error_cnt); 

} 



CheckP (n, msg, m)
**************
 * check n positive else display msg  quit
 *
int n;
char *msg;
int m;
{
		if (n<0) {
			if (m==0)
                printf ( Program terminated: error u in s\n,errno,msg);
			else
                printf ( Program terminated: error u in s Count d \n, errno, msg, m);
            exit(1);
        }
        else {
ifdef DEBUG
		if(m==0)
			printf (Trace OK s\n, msg);
		else
			printf (Trace OK in Count d : s\n, m, msg);
endif
        }
}



