예제 #1
0
파일: dump_main.c 프로젝트: kejiewei/eresi
/* parse command */
void	lhandler (char *c)
{
  char	*w;
  
  if (c != NULL)
    {
      add_history (c);
      //printf ("[+] -> readline : %s\n", c);
      
      w = strtok (c, " ");
      if (w == NULL)
	return;
      
      if (!strcmp ("connect", w))
	{
	  printf ("[+] connect\n");
	  w = strtok (NULL, " ");
	  if (w == NULL)
	    return ;
	  connect_to (w);
	  return ;
	}
      else if (!strcmp ("exit", w))
	{
	  printf ("\n[+] leaving\n");
	  exit (0);
	}
      else if (!strcmp ("quit", w))
        {
          printf ("\n[+] leaving\n");
          exit (0);
        }
      else if (!strcmp ("send", w))
	{
	  w = strtok (NULL, " ");
          if (w == NULL)
            return ;
          send_to (w, strtok (NULL, " "));
	  return ;
	}
      else if (!strcmp ("disconnect", w))
	{
	  w = strtok (NULL, " ");
          if (w == NULL)
            return ;
	  disconnect_from (w);
	  return ;
	}
    }
  else 
    {
      printf ("\n[+] leaving\n");
      exit (0);
    }
}
예제 #2
0
int main(void) {

	printf("*** Connecting to server...\n");

	if (connect_to("server1.example.com", &ch) < 0) {
		perror("Connection failed");
		exit(EXIT_FAILURE);
	}

	setup_signals();

	printf("*** Connected!\n");
	printf("    This is a client connected to an echo server.\n"
	       "    Any lines you enter will be echoed back by the server.\n"
	       "    To quit, send EOF (usually Ctrl+D).\n\n");

	static char buf[512];

	while (fgets(buf, sizeof(buf), stdin) != NULL) {
		size_t line_len = strlen(buf);

		if (send_info(&ch, buf, line_len) < 0) {
			perror("Error sending data to server");
			continue;
		}

		printf("*** Sent: %s", buf);

		int n;
		if ((n = recv_info(&ch, buf, sizeof(buf)-1)) < 0) {
			perror("Error receiving data from server\n");
			continue;
		}

		buf[n] = '\0';
		printf("*** Received: %s", buf);
	}

	sprintf(buf, "__QUIT__NOW__\n");

	if (send_info(&ch, buf, strlen(buf)) < 0) {
		perror("Unable to notify server of client disconnect");
	}

	printf("*** Goodbye\n");

	disconnect_from(&ch);

	return 0;
}
예제 #3
0
int main(void) {
	int clientfd;

	setup_signals();
	printf("*** Creating a server...\n");

	if ((serverfd = new_server("server1.example.com")) < 0) {
		perror("Couldn't create server");
		exit(EXIT_FAILURE);
	}

	printf("*** Created!\n"
	       "    This is an echo server.\n"
	       "    Anything received will be echoed back to the client.\n\n");

	/* Accept a client */
	if ((clientfd = accept_client(serverfd)) < 0) {
		perror("Couldn't accept client");
		destroy_server(serverfd);
		exit(EXIT_FAILURE);
	}

	printf("*** Accepted incoming connection request.\n");

	static char buf[512+1];
	int n;

	while ((n = recv_info(clientfd, buf, sizeof(buf)-1)) > 0) {
		buf[n] = '\0';
		if (!strcmp(buf, "__QUIT__NOW__\n")) {
			printf("*** Got disconnect request from client, leaving...\n");
			disconnect_from(clientfd);
			break;
		}

		printf("*** Received: %s", buf);

		if (send_info(clientfd, buf, n) < 0) {
			perror("Error sending message to client");
		} else {
			printf("*** Sent: %s", buf);
		}
	}

	printf("*** Goodbye\n");

	destroy_server(serverfd);

	return 0;
}
예제 #4
0
void shutdown_handler(int signo) {
	static char quitmsg[] = "__QUIT__NOW__\n";
	send_info(&ch, quitmsg, sizeof(quitmsg)-1);
	disconnect_from(&ch);
	exit(0);
}