/* atcprecv.c */ /* Demonstates the use of recvfrom for datagram transfer */ #include #include #include #include #include #include #include #include #include #include struct timeval time1; struct timeval time2; static double tu1; static double tu2; static struct timezone dummy = {0,0}; static int frames = 0; static int drops = 0; static int dropcount = 0; static double bytes = 0; char buf[16 * 1024]; #define APDU 1448 void sigintr( int dummy) { tu1 = 1000000.0 * time1.tv_sec; tu1 += time1.tv_usec; printf("%d %d \n", time1.tv_sec, time1.tv_usec); printf("%d %d \n", time2.tv_sec, time2.tv_usec); tu2 = 1000000.0 * time2.tv_sec; tu2 += time2.tv_usec; printf("Frames recvd = %10d \n", frames); printf("Elapsed usec = %10.0lf \n", tu2 - tu1); printf("Frames/sec = %10.2lf \n", frames * 1000000.0 / (tu2 - tu1)); printf("Mbits / sec = %10.2lf \n", bytes * 8 / (tu2 - tu1)); exit(1); } void aip_recv( int s) /* socket handle */ { int amt = 0; int status; signal(SIGINT, sigintr); status = read(s, buf, APDU); gettimeofday(&time1, &dummy); while (status > 0) { amt += status; bytes += status; /* write(1, buf, status); */ status = read(s, buf, APDU); frames += 1; gettimeofday(&time2, &dummy); } printf("Final return code was %d \n", status); printf("Final errno was %d \n", errno); printf("Receiver complete.. received %d bytes \n", amt); perror("Final"); } struct sockaddr_in name; struct sockaddr_in sname; int namelen; char hnamebuf[80]; main( int argc, char** argv) { int i, me, you; unsigned char c; int sock; int netaddr; int status; unsigned dummy; int msgcount; int msgsock; int namesize; struct hostent *hp; /* Create TCP socket */ sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { printf("Socket create failed \n"); exit(1); } dummy = 0; bcopy((char *)&dummy, (char *)&name.sin_addr, sizeof(dummy)); /* Fill in protocol family and port # from command line then */ /* bind the socket to the specified address. */ name.sin_family = AF_INET; if (argc > 1) name.sin_port = htons(atoi(argv[1])); else name.sin_port = htons(33456); status = bind(sock, (struct sockaddr *)&name, sizeof(name)); printf("Bind to port %d status is %d\n", ntohs(name.sin_port), status); if (status < 0) { perror("bind error"); exit(1); } status = listen(sock, 4); if (status < 0) { printf("Listen failed with code %d. \n", errno); exit(4); } nextsession: status = accept(sock, (struct sockaddr *)&name, &namesize); if (status < 0) { printf("Accept failed with code %d. \n", errno); exit(5); } msgsock = status; /* Wait for someone to send me a datagram */ aip_recv(msgsock); goto nextsession; }