Esempio n. 1
0
File: read.c Progetto: Fusiow/msh
int		distrib_buttons(int i, char **result, char *buffer, int *v)
{
	if (buffer[0] == 10)
		return (-2);
	else if (buffer[0] == 27 && buffer[1] == 91)
		i = distrib_arrow(i, result, buffer, v);
	else if (buffer[0] == 127)
		*result = del_c(*result, &i);
	else if (buffer[0] == 9 && *result && i == ft_strlen(*result))
		i = distrib_tab(i, result);
	else if (buffer[0] == 12)
		clear_and_prompt();
	else if (buffer[0] == 1)
		i = ctrl_a(i, result);
	else if (buffer[0] == 5)
		i = ctrl_k(i, result);
	else if (buffer[0] == 21)
	{
		clear_line(i + 1, ft_strlen(*result));
		*result = NULL;
		i = 0;
	}
	else if (buffer[0] == 4)
		ft_exit(NULL);
	else
		ft_putstr(tgetstr("le", NULL));
	return (i);
}
Esempio n. 2
0
// Main
int main(int argc, char *argv[])
{
	// Initialisation des variables
    int portno;
    struct hostent *server;
    char buffer[BUFFER_LENGTH];
    memset(buffer, '\0', BUFFER_LENGTH);

    // Arguments
    if (argc < 3)
    {
       fprintf(stderr,"Usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);

    // Initialisation
    sockfd = socket(AF_INET, SOCK_STREAM, 0); // IPV4, intégrité+flux binaire
    if (sockfd < 0)
        display_error("ERREUR ouverture socket");

    // IP ou hostname en paramêtre ?
    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    server = gethostbyname(argv[1]); // On récupère l'host par son nom
    if (server != NULL)
    {
        bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
    }
    else
    {
    	serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    	if (serv_addr.sin_addr.s_addr == INADDR_NONE)
    	{
            fprintf(stderr,"Socket Error # 11001, Host not found:\n");
			exit(0);
        }
    }

    serv_addr.sin_family = AF_INET; // IPV4
    serv_addr.sin_port = htons(portno); // Conversion et assignation du port
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) // Connexion au serveur
    {
    	display_error("Socket Error # 10061, Connection refused");
    	exit(1);
    }

    memset(buffer, 0, BUFFER_LENGTH);
	read(sockfd,buffer,BUFFER_LENGTH); // Lecture du message de bienvenu
    printf("%s\n",buffer);

	// Boucle principale
	while(!stop) {
	    clear_and_prompt();
	    memset(buffer, 0, BUFFER_LENGTH);
	    fgets(buffer,BUFFER_LENGTH,stdin);

	    // Envoi au serveur
	    write(sockfd,buffer,strlen(buffer));

	    // Lecture de la commande (si traitement côté client nécessaire)
	    read_cmd(buffer);

	    // Réponse du serveur
	    memset(buffer, 0, BUFFER_LENGTH);
		read(sockfd,buffer,BUFFER_LENGTH);
	    printf("%s\n",buffer);
	}

    close(sockfd); // Fermeture du socket

    return 0;
}