/* * senddata.c * Daten von stdin ueber einen socket versenden */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <sys/socket.h> #include <netdb.h> #include "defs.h" void Usage(char *name) { fprintf(stderr, "Aufruf: %s [-t] [-s bps] adresse port\n", name); fprintf(stderr, "\t -t: TCP verwenden\n"); fprintf(stderr, "\t -s bps: Senderate in Bit pro Sekunde\n"); exit(1); } int main(int argc, char **argv) { int sock, err, bytesRead, ch, i = 0; int delay = 0, speed = 0, debug = 0; char *hostName, *portName; char buf[PACKETSIZE]; struct addrinfo hints, *address; int socketType = SOCK_DGRAM; while((ch = getopt(argc, argv, "tds:")) != EOF) { switch(ch) { case 't': socketType = SOCK_STREAM; break; case 's': speed = atoi(optarg); break; case 'd': debug = 1; break; default: Usage(argv[0]); } } if(argc - optind != 2) Usage(argv[0]); hostName = argv[optind++]; portName = argv[optind++]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_socktype = socketType; hints.ai_flags = AI_CANONNAME; err = getaddrinfo(hostName, portName, &hints, &address); if(err != 0) { fprintf(stderr, "getaddrinfo(): %d\n", err); exit(1); } sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol); if(sock < 0 ) { perror("socket()"); exit(1); } err = connect(sock, address->ai_addr, address->ai_addrlen); if(err != 0) { perror("connect()"); exit(1); } if(speed) { delay = (int) (1000000.0 / ((float) speed / ((float) PACKETSIZE * 8.0))); printf("delay: %d usec\n", delay); } while((bytesRead = fread(buf, 1, sizeof(buf), stdin)) > 0) { if(write(sock, buf, bytesRead) < 0) { perror("write()"); exit(1); } if(delay) usleep(delay); if(debug) { if(++i % 10 == 0) { putchar('.'); fflush(stdout); } } } return 0; }