Example #1
0
/**
 * loopClient
 *
 * Boucle principale de l'entrée.
 * Création d'un client, écoute et envoie des commandes vers un shel distant
 *
 * @param  {char *}                 addr
 * @param  {int}                    port
 */
void loopClient(char *addr, int port) {

    const char eot = 4;
    int socketClient = createClientSocket(addr, port);

    while (42) {

        DEBUG("[client] Waiting for message");

        //On lit un message jusqu'à un EOT
        char ch; //caractère par caractère
        int result = -1;
        while ((result = read(socketClient, &ch, sizeof(char))) > 0) {
            printf("%c", ch);
            if (ch == eot) {
                break;
            }
        }

        if (result == -1) { //si erreur autre que pas de data
            perror("reception error");
        }

        DEBUG("[client] end of response");

        //On envoi un message
        char *msg = NULL;
        int n = readInputClient(&msg); //entrée utilisateur

        DEBUG("[client] Sending %s", msg);

        if (n > 0) {
            //envoi de la commande
            if (sendall(socketClient, msg, strlen(msg), 0) == -1) {
                perror("sendall() error");
            }
        } else {
            break;
        }
    }

}
Example #2
0
static RESULT connectToClient(char clientId, int *sockFd)
{
	char buffer[64] = {0};
	CLIENT_DB *cdb = getClientDbInstance();
	struct in_addr addr = {cdb->clientIP[clientId]};
	sprintf(buffer, "%s", inet_ntoa( addr ) );
	LOG_MSG("Connecting to client for sending update %s",buffer);
	
	*sockFd = createClientSocket(buffer,8091);
	if(*sockFd == -1){
		LOG_ERROR("Unable to connect with client for sending update");
		cdb->deregisterClient(cdb,clientId);
		return G_FAIL;
	}
	/*if(setSocketBlockingEnabled(*sockFd,1) != G_OK)
	{
		LOG_ERROR("Unable to set socket non blocking");
		return G_FAIL;
	}*/
	return G_OK;
}
Example #3
0
int main( int argc, const char* argv[] )
{
	int bufferSize = 40, err = 0, n = 0;
	char buffer[bufferSize];
	struct sockaddr_in ControlAddr, Process0_Addr, Process1_Addr, Process2_Addr;
	int controlSocket, process0Socket, process1Socket, process2Socket;
	int processSockets[3];
	char* generatedBytes = generateBytes();
	if (argc < 3)
	{
		printf("Please specify the IP address of the server and a port number\n");
		exit(0);
	}
	int port = atoi(argv[2]);
	//set up all the addresses
	printf("Port: %d\n", port);
	ControlAddr = createSockAddr(argv[1], port++);
	printf("Port: %d\n", port);
	Process0_Addr = createSockAddr(argv[1], port++);
	printf("Port: %d\n", port);
	Process1_Addr = createSockAddr(argv[1], port++);
	printf("Port: %d\n", port);
	Process2_Addr = createSockAddr(argv[1], port);
	printf("Port: %d\n", port);
	//create all connections
	//also do an initial write (have to do this to get all the connections to work...)
	printf("Control connection\n");
	controlSocket = createClientSocket(ControlAddr);
	sprintf(buffer, "Control socket write");
	printf("%s\n", buffer);
	err = write(controlSocket, buffer, strlen(buffer));
	checkError(err, "Control socket error on write");
	
	printf("Process 0 connection\n");
	processSockets[0] = createClientSocket(Process0_Addr);
	sprintf(buffer, "Process 0 socket write");
	printf("%s\n", buffer);
	err = write(processSockets[0], buffer, strlen(buffer));
	checkError(err, "Process 0 socket error on write");
	
	printf("Process 1 connection\n");
	processSockets[1] = createClientSocket(Process1_Addr);
	sprintf(buffer, "Process 1 socket write");
	printf("%s\n", buffer);
	err = write(processSockets[1], buffer, strlen(buffer));
	checkError(err, "Process 1 socket error on write");
	
	printf("Process 2 connection\n");
	processSockets[2] = createClientSocket(Process2_Addr);
	sprintf(buffer, "Process 2 socket write");
	printf("%s\n", buffer);
	err = write(processSockets[2], buffer, strlen(buffer));
	checkError(err, "Process 2 socket error on write");
	
	//create pipes & children
	
	
	int pipefd[3][2];
	pid_t cpid;
	
	int i = 0;	
	for (i = 0; i < 3; i++)
	{
		checkError(pipe(pipefd[i]), "Error creating pipe");
		cpid = fork();
		checkError(cpid, "Error on fork");
		if (cpid == 0)
		{
			char buf[BYTES_PER_PACKET + 1];
			char errorString[40];
			//read from pipe
			err = read(pipefd[i][0], buf, BYTES_PER_PACKET);
			buf[BYTES_PER_PACKET] = '\0';
			while(err)
			{	
				sprintf(errorString, "Process %d error on pipe read", i);
				checkError(err, errorString);
				//write to TCP socket
				err = write(processSockets[i], buf, strlen(buf));
				sprintf(errorString, "Process %d socket error on write", i);
				checkError(err, errorString);
				err = read(pipefd[i][0], buf, BYTES_PER_PACKET);
				buf[BYTES_PER_PACKET] = '\0';
			}
			exit(0);
		}
	}
	FILE *outputFile;
        outputFile = fopen("output_client.txt", "w");
        if (outputFile == NULL)
        {
                printf("Error opening file\n");
                exit(0);
        }

	char* dataSegment = (char *) malloc(sizeof(char) * (BYTES_PER_PACKET + 1));
	int dataSeqNum = 0, byte = 0;
	char* dataSeqNumBuf = (char *) malloc(sizeof(char) * (40));
	for (dataSeqNum = 0; dataSeqNum < (TOTAL_BYTES/BYTES_PER_PACKET);)
	{
		for (i = 0; i < 3 && dataSeqNum < (TOTAL_BYTES/BYTES_PER_PACKET); i++)
		{
			//write sequence number to control socket
			sprintf(dataSeqNumBuf, "Data Seq Num: %d, Subflow Seq Num: %d, Flow: %d", dataSeqNum, dataSeqNum/3, i);		
			fprintf(outputFile, "%s\n", dataSeqNumBuf);
			err = write(controlSocket, dataSeqNumBuf, strlen(dataSeqNumBuf));
			//write data segment to pipe
			strncpy(dataSegment, generatedBytes + byte, BYTES_PER_PACKET);
			fprintf(outputFile, "%s\n", dataSegment);
			err = write(pipefd[i][1], dataSegment, strlen(dataSegment));
			checkError(err, "Error on pipe write");
			dataSeqNum++;
			byte += 4;
		}
	}

	close(outputFile);
	//close pipes
	close(pipefd[0]);
	close(pipefd[1]);
	close(pipefd[2]);
	
	//close all sockets
	close(controlSocket);
	close(processSockets[0]);
	close(processSockets[1]);
	close(processSockets[2]);

	return 0;
}
Example #4
0
static void connectClient(const char *dev) {
  fd_set master;
  fd_set read_fds;
  FD_ZERO(&master);
  FD_ZERO(&read_fds);

  signal(SIGHUP, exit_handler);
  signal(SIGINT, exit_handler);
  signal(SIGQUIT, exit_handler);
  signal(SIGPIPE, exit_handler);
  signal(SIGTSTP, exit_handler);
  signal(SIGTERM, exit_handler);

  int stdi;
  struct ttyRaw* tty_in;
  stdi = STDIN_FILENO;
  if (!isatty(stdi)) {
    return;
  }
  tty_in = setTty(stdi, 0);

  int stdo;
  struct ttyRaw* tty_out;
  stdo = STDOUT_FILENO;
  if (!isatty(stdo)) {
    closeTty(tty_in);
    return;
  }
  tty_out = setTty(stdo, 0);

  int clientfd;
  clientfd = createClientSocket(dev);
  if (clientfd < 0) {
    closeTty(tty_out);
    closeTty(tty_in);
    return;
  }

  FD_SET(tty_in->fd,&master);
  FD_SET(clientfd, &master);

  int fdmax = 0;
  fdmax = (clientfd > tty_in->fd) ? clientfd : tty_in->fd;

  for(;;) {
    if (sigexit) {
      break;
    }
    read_fds = master;
    if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) {
      perror("mTerm_client: select error");
      break;
     }
    if (FD_ISSET(stdi, &read_fds)) {
      if (!readFromStdin(clientfd, tty_in->fd)) {
        break;
      }
    }
    if (FD_ISSET(clientfd, &read_fds)) {
      if (!writeToStdout(clientfd,tty_out->fd)) {
        break;
      }
    }
  }
  closeTty(tty_out);
  closeTty(tty_in);
  close(clientfd);
}
Example #5
0
OlySocket::OlySocket(int port, char* host) {
  mFDServer = 0;
  createClientSocket(host, port);
}