Beispiel #1
0
int main(int argc, char* argv[]) {
    rudp_socket_t rsock;
    int port;

    int c;

    /*
     * Parse and collect arguments
     */
    opterr = 0;

    while ((c = getopt(argc, argv, "d")) != -1) {
        if (c == 'd') {
            debug = 1;
        } else
            usage();
    }
    if (argc - optind != 1) {
        usage();
    }

    port = atoi(argv[optind]);
    if (port <= 0) {
        fprintf(stderr, "Bad destination port: %s\n", argv[optind]);
        exit(1);
    }

    if (debug) {
        printf("RUDP receiver waiting on port %i.\n", port);
    }

    /*
     * Create RUDP listener socket
     */

    if ((rsock = rudp_socket(port)) == NULL) {
        fprintf(stderr, "vs_recv: rudp_socket() failed\n");
        exit(1);
    }

    /*
     * Register receiver callback function
     */

    rudp_recvfrom_handler(rsock, rudp_receiver);

    /*
     * Register event handler callback function
     */

    rudp_event_handler(rsock, eventhandler);

    /*
     * Hand over control to event manager
     */

    eventloop(0);

    return (0);
}
Beispiel #2
0
void send_file(char *filename) {
	struct vsftp vs;
	int vslen;
	char *filename1;
	int namelen;
	int file = 0;
	int p;
	rudp_socket_t rsock;

	if ((file = open(filename, O_RDONLY)) < 0) {
		perror("vs_sender: open");
		exit(-1);
	}
	rsock = rudp_socket(0);
	if (rsock == NULL) {
		fprintf(stderr, "vs_send: rudp_socket() failed\n");
		exit(1);
	}
	rudp_event_handler(rsock, eventhandler);

	vs.vs_type = htonl(VS_TYPE_BEGIN);

	/* strip of any leading path name */
	filename1 = filename;
	if (strrchr(filename1, '/'))
		filename1 = strrchr(filename1, '/') + 1;
	
	/* Copy file name into VS data */
	namelen = strlen(filename1) < VS_FILENAMELENGTH  ? strlen(filename1) : VS_FILENAMELENGTH;
	strncpy(vs.vs_info.vs_filename, filename1, namelen);

	vslen = sizeof(vs.vs_type) + namelen;
	for (p = 0; p < npeers; p++) {
		if (debug) {
			fprintf(stderr, "vs_send: send BEGIN \"%s\" (%d bytes) to %s:%d\n",
				filename, vslen, 
				inet_ntoa(peers[p].sin_addr), ntohs(peers[p].sin_port));
		}
		if (rudp_sendto(rsock, (char *) &vs, vslen, &peers[p]) < 0) {
			fprintf(stderr,"rudp_sender: send failure\n");
			rudp_close(rsock);		
			return;
		}
	}
	event_fd(file, filesender, rsock, "filesender");
}