コード例 #1
0
ファイル: MsjHelper.c プロジェクト: marcelocejas/Cache13
/**
 * @NAME: sockets_createClient
 * @DESC: Inicializa un cliente.
 * Retorna la cantidad de bytes enviados.
 */
int sockets_createClient(char *addr, char *port) {
	int sockfd = sockets_getSocket();
	if (sockets_connect(sockfd, addr, port) == -1) {
		close(sockfd);
		return -1;
	}
	return sockfd;
}
コード例 #2
0
int main(int argc, char *argv[])
{
	struct sockaddr *address;
	struct sockaddr_in address_in;
	struct sockaddr_in6 address_in6;
	socklen_t addrlen;
	uint8_t *address_start;

	int *socket_ids;
	char *data;
	int index;
	struct timeval time_before;
	struct timeval time_after;

	int rc;

	size = 28;
	verbose = false;
	type = SOCK_DGRAM;
	sockets = 10;
	messages = 10;
	family = PF_INET;
	port = 7;

	/*
	 * Parse the command line arguments.
	 *
	 * Stop before the last argument if it does not start with dash ('-')
	 */
	for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
	    (argv[index][0] == '-')); ++index) {

		/* Options should start with dash ('-') */
		if (argv[index][0] == '-') {
			rc = nettest2_parse_opt(argc, argv, &index);
			if (rc != EOK)
				return rc;
		} else {
			nettest2_print_help();
			return EINVAL;
		}
	}

	/* If not before the last argument containing the address */
	if (index >= argc) {
		printf("Command line error: missing address\n");
		nettest2_print_help();
		return EINVAL;
	}

	/* Prepare the address buffer */

	switch (family) {
	case PF_INET:
		address_in.sin_family = AF_INET;
		address_in.sin_port = htons(port);
		address = (struct sockaddr *) &address_in;
		addrlen = sizeof(address_in);
		address_start = (uint8_t *) &address_in.sin_addr.s_addr;
		break;
	case PF_INET6:
		address_in6.sin6_family = AF_INET6;
		address_in6.sin6_port = htons(port);
		address = (struct sockaddr *) &address_in6;
		addrlen = sizeof(address_in6);
		address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
		break;
	default:
		fprintf(stderr, "Address family is not supported\n");
		return EAFNOSUPPORT;
	}

	/* Parse the last argument which should contain the address. */
	rc = inet_pton(family, argv[argc - 1], address_start);
	if (rc != EOK) {
		fprintf(stderr, "Address parse error %d\n", rc);
		return rc;
	}

	/* Check data buffer size. */
	if (size <= 0) {
		fprintf(stderr, "Data buffer size too small (%zu). Using 1024 "
		    "bytes instead.\n", size);
		size = 1024;
	}

	/*
	 * Prepare the buffer. Allocate size bytes plus one for terminating
	 * null character.
	 */
	data = (char *) malloc(size + 1);
	if (!data) {
		fprintf(stderr, "Failed to allocate data buffer.\n");
		return ENOMEM;
	}

	/* Fill buffer with a pattern. */
	nettest2_fill_buffer(data, size);

	/* Check socket count. */
	if (sockets <= 0) {
		fprintf(stderr, "Socket count too small (%d). Using "
		    "2 instead.\n", sockets);
		sockets = 2;
	}

	/*
	 * Prepare the socket buffer.
	 * Allocate count entries plus the terminating null (\0)
	 */
	socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
	if (!socket_ids) {
		fprintf(stderr, "Failed to allocate receive buffer.\n");
		return ENOMEM;
	}
	socket_ids[sockets] = 0;

	if (verbose)
		printf("Starting tests\n");

	rc = sockets_create(verbose, socket_ids, sockets, family, type);
	if (rc != EOK)
		return rc;

	if (type == SOCK_STREAM) {
		rc = sockets_connect(verbose, socket_ids, sockets,
		    address, addrlen);
		if (rc != EOK)
			return rc;
	}

	if (verbose)
		printf("\n");

	rc = gettimeofday(&time_before, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
	    &addrlen, data, size, messages);
	if (rc != EOK)
		return rc;

	rc = gettimeofday(&time_after, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	if (verbose)
		printf("\tOK\n");

	printf("sendto + recvfrom tested in %ld microseconds\n",
	    tv_sub(&time_after, &time_before));

	rc = gettimeofday(&time_before, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
	    data, size, messages);
	if (rc != EOK)
		return rc;

	rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
	    data, size, messages);
	if (rc != EOK)
		return rc;

	rc = gettimeofday(&time_after, NULL);
	if (rc != EOK) {
		fprintf(stderr, "Get time of day error %d\n", rc);
		return rc;
	}

	if (verbose)
		printf("\tOK\n");

	printf("sendto, recvfrom tested in %ld microseconds\n",
	    tv_sub(&time_after, &time_before));

	rc = sockets_close(verbose, socket_ids, sockets);
	if (rc != EOK)
		return rc;

	if (verbose)
		printf("\nExiting\n");

	return EOK;
}
コード例 #3
0
ファイル: main.c プロジェクト: jcordasc/No_Agenda_Bat_Sig
int main(int argc, char **argv){
	GtkWidget *win;
	t_tweet *info;
	t_tweet *refreshed;
	t_setting sets;
	char *get;
	char *mem;
	char *host;
	char count;
	int sockfd;
	int rv;
	pid_t pid;

	setbuf(stdout, NULL);
	setbuf(stderr, NULL);

	memset(sets.set_from_rc, 0, 3);
	sets.irc = sets.gtk_on = 0;
	sets.mp3player = sets.path_to_jingle = sets.browser = get = (char *)DBPTR;

	if(argc == 1){
		/* Read from rc file */
		bat_sig_rc(&sets);
	} else {
		/* Parse arguments */
		rv = parse_cmdline_opts(argc, argv, &sets);
		if(rv < 0) {
			return 0;
		}
		else if(rv > 0) { /* technically else not needed */
			return 10; /* bad args */
		}
	}

	if(sets.path_to_jingle == (char*)DBPTR)
		sets.path_to_jingle = "jingles/douchebag.mp3";
	if(sets.browser == (char*)DBPTR)
		sets.browser = "/usr/bin/firefox";
	if(sets.mp3player == (char*)DBPTR)
	    sets.mp3player = "/usr/bin/mpg123";

   host = "Host: search.twitter.com\n\n";

	mem = (char *)xmalloc(MAX_SIZE * sizeof(char));

   sockfd = sockets_connect();

   if(sockfd < 0){
      printf("Cannot establish connection.\n");
      return 1;
   } else {
#ifndef SIGMANATEST
      get = "GET /search.json?q=@pocketnoagenda&from=adamcurry&rpp=1 HTTP/1.1\n";
#else
      get = "GET /search.json?q=@pocketnoagenda&from=sigmanatest&rpp=1 HTTP/1.1\n";
#endif
      sockets_request(sockfd, get, host, &mem, MAX_SIZE);
      get = (char *)xmalloc(MAX_SIZE * sizeof(char));
      /* Twitter sends the Connection: close header so by RFC2616 we must close our
       * connection and then reconnect later.
       */
   }

	info = parse_mem(mem);
	if(info){
		/* Now to grab the "refresh_url" string from the data and attach it to
		 * a new URL buffer and have it watch that. 
		 * For testing, I'm using the one as of Tue Nov 9
		 */
      make_get(get, info->refresh);

		for(count = 0; count < MAX_ITER; count++){
			sockfd = sockets_connect();

			if(sockfd > 0){
				memset(mem, '\0', MAX_SIZE); /* Clear out the memory so we do not get false information */

            sockets_request(sockfd, get, host, &mem, MAX_SIZE);

				/* Returns "{\"results\":[]" if there is nothing. 
				 * Can run a while loop until this does not match. */
				refreshed = parse_mem(mem);
				if(refreshed)
					break;

				(void)sleep(SLEEP_SECONDS);
			} else {
				printf("Cannot reestablish connection.\n");
				return 1;
			}
		}

		if(!refreshed)
			printf("No bat signal was sent.\n");
		else{
         if(sets.irc)
            notice(refreshed);
			/* fork() and exec Jingle */
			if(0 > (pid = fork())){
				fprintf(stderr, "Error forking.\n");
				return 1;
			}
			if(pid == 0){
				char *args[] = {sets.mp3player, sets.path_to_jingle, NULL};
				close(STDOUT_FILENO);
				close(STDERR_FILENO);
				execvp(*args, args);
				exit(EXIT_FAILURE);
			} else {
				if(!sets.gtk_on)
					printf("Time: %s\nTweetURL: %s\nTweet: %s\n", refreshed->date, refreshed->tweet_url, refreshed->text);
				else {
					rv = 0;
					gtk_init(&rv, NULL);
					win = make_window(refreshed, sets.browser);
					gtk_widget_show(win);
					gtk_main();
				}
			}
			free_t_tweet(refreshed);
		}
		free_t_tweet(info);
	}

	if(get != (char *)DBPTR)
		free(get);
	free(mem);
	if(sets.set_from_rc[0])
		free(sets.path_to_jingle); 
	if(sets.set_from_rc[1])
		free(sets.browser);
	if(sets.set_from_rc[2])
		free(sets.mp3player);
	return 0;
}
コード例 #4
0
ファイル: nettest1.c プロジェクト: fhector/helenOS-0.5-Hector
static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
{
	int rc;

	if (verbose)
		printf("%d sockets, %d messages\n", nsockets, nmessages);

	rc = sockets_create(verbose, socket_ids, nsockets, family, type);
	if (rc != EOK)
		return rc;

	if (type == SOCK_STREAM) {
		rc = sockets_connect(verbose, socket_ids, nsockets, address,
		    addrlen);
		if (rc != EOK)
			return rc;
	}

	rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
	    &addrlen, data, size, nmessages);
	if (rc != EOK)
		return rc;

	rc = sockets_close(verbose, socket_ids, nsockets);
	if (rc != EOK)
		return rc;

	if (verbose)
		printf("\tOK\n");

	/****/

	rc = sockets_create(verbose, socket_ids, nsockets, family, type);
	if (rc != EOK)
		return rc;

	if (type == SOCK_STREAM) {
		rc = sockets_connect(verbose, socket_ids, nsockets, address,
		    addrlen);
		if (rc != EOK)
			return rc;
	}

	rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
	    data, size, nmessages);
	if (rc != EOK)
		return rc;

	rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
	    data, size, nmessages);
	if (rc != EOK)
		return rc;

	rc = sockets_close(verbose, socket_ids, nsockets);
	if (rc != EOK)
		return rc;

	if (verbose)
		printf("\tOK\n");

	return EOK;
}