Exemple #1
0
//lê um caracter e converte o caracter para maiuscula
char lerCaracter(void){
    char opcao;
    scanf("%c", &opcao);
    opcao = toupper(opcao);
    limparBuffer(); //limpa o buffer antes de voltar a ler alguma coisa do teclado
return opcao;
}
Exemple #2
0
int lerNumero(int minimo, int maximo){
    int numero, control;
    do{
        control = scanf("%d", &numero);
        limparBuffer();
        if(numero>maximo || numero<minimo)
        {
            printf("o numero encontra-se fora dos limites!\n O numero entre %d e %d\n", minimo, maximo);
            printf("Insira um novo numero: ");
        }
    }
    while(numero>maximo || numero<minimo || control!=1);
return numero;
}
Exemple #3
0
main() {
    char alfabeto[26] = {'a','b','c','d','e', 'f', 'g', 'h', 'i', 'j', 'k','l','m','n','o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    system("cls");
    int i = 0; //Indicador do programa
    // struct  hostent  *ptrh;  /* pointer to a host table entry       */
    struct  protoent *ptrp;  /* pointer to a protocol table entry   */
    struct  sockaddr_in sad; /* structure to hold server's address  */
    struct  sockaddr_in cad; /* structure to hold client's address  */
    int     sd, sd2;         /* socket descriptors                  */
    int     port;            /* protocol port number                */
    int     alen,n,pontarq,tbuf;            /* length of address                   */
    char    buf[1000];       /* buffer for string the server sends  */
    FILE *arqFonte;
    FILE *arq;
    FILE *fp;
    char op[1], buf2[1];
    int found = 10002323;
    char cliente_nome[20], cliente_senha[20], nome[20], senha[20], nome_arquivo[50];
#ifdef WIN32
    WSADATA wsaData;
    WSAStartup(0x0101, &wsaData);
#endif
    memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
    sad.sin_family = AF_INET;         /* set family to Internet     */
    sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address   */

    /* Check command-line argument for protocol port and extract    */
    /* port number if one is specified.  Otherwise, use the default */
    /* port value given by constant PROTOPORT                       */

    port = PROTOPORT;       /* use default port number      */
    if (port > 0)                   /* test for illegal value       */
        sad.sin_port = htons((u_short)port);
    else {                          /* print error message and exit */
        fprintf(stderr,"bad port number \n");
        exit(1);
    }

    /* Map TCP transport protocol name to protocol number */

    if (((int*)(ptrp = getprotobyname("tcp"))) == 0) {
        fprintf(stderr, "cannot map \"tcp\" to protocol number");
        exit(1);
    }

    /* Create a socket */

    sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
    if (sd < 0) {
        fprintf(stderr, "socket creation failed\n");
        exit(1);
    }

    /* Bind a local address to the socket */

    if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
        fprintf(stderr,"bind failed\n");
        exit(1);
    }

    /* Specify size of request queue */

    if (listen(sd, QLEN) < 0) {
        fprintf(stderr,"listen failed\n");
        exit(1);
    }

    char posX[4], posY[4], pos[8];
    FILE *rota;
    char op1;

    //rota = fopen("local2.txt", "w");

    printf("Conectou");

    alen = sizeof(cad);
    sd2=accept(sd, (struct sockaddr *)&cad, &alen) ;
    limparBuffer(buf);
    printf("Bem vindo!!!");

    do {

INICIO:
        rota = fopen("local2.txt", "a+");
        //send(sd2,buf,strlen(buf),0);
        n = recv(sd2, op, sizeof(op),0);

        printf("recebeu %s", op);

        op1 = op[0];

        switch (op1) {
        case '1':
            strcpy(posX,"563");
            strcpy(posY,"172");
            break;
        case '2':
            strcpy(posX,"581");
            strcpy(posY,"297");
            break;
        case '3':
            strcpy(posX,"837");
            strcpy(posY,"574");
            break;
        case '4':
            strcpy(posX,"400");
            strcpy(posY,"270");
            break;
        case '5':
            strcpy(posX,"536");
            strcpy(posY,"613");
            break;
        case '6':
            fclose(rota);
            apagarfile();
            goto INICIO;
            //remove("local2.txt");
            //fseek("local2.txt", -1, SEEK_CUR);
            //fflush();
            break;
        case '7':
            //closesocket(sd2);
            break;
        default:
            // code...
            break;
        }


        if(strcmp(op,"7") != 0) {
            printf("\nX: %s \nY: %s", posX, posY);
            strcpy(pos, posX);
            strcat(pos, "_");
            strcat(pos, posY);

            printf("%sX", pos);

            //fputs(pos, rota);
            fwrite(pos, 1, sizeof(pos), rota);
            putc('\n', rota);
        }

        //fprintf(rota, pos);
        //fprintf(rota, "%s %s", posX, posY);
        //strcpy(buf2, "1");
        //send(sd2, buf2, sizeof(buf2), 0);
        fclose(rota);
    } while(strcmp(op,"7") != 0);
    //fclose(rota);
    closesocket(sd2);
}
main(){
       
        system("cls");
        int i = 0; //Indicador do programa
       // struct  hostent  *ptrh;  /* pointer to a host table entry       */
        struct  protoent *ptrp;  /* pointer to a protocol table entry   */
        struct  sockaddr_in sad; /* structure to hold server's address  */
        struct  sockaddr_in cad; /* structure to hold client's address  */
        int     sd, sd2;         /* socket descriptors                  */
        int     port;            /* protocol port number                */
        int     alen,n,pontarq,tbuf;            /* length of address                   */
        char    buf[50000];       /* buffer for string the server sends  */
        FILE *arqFonte;
        FILE *arq;
        FILE *fp;
        char op[50000], buf2[1];
        
        int found = 10002323;
        char cliente_nome[20], cliente_senha[20], nome[20], senha[20], nome_arquivo[50];
#ifdef WIN32
        WSADATA wsaData;
        WSAStartup(0x0101, &wsaData);
#endif
        memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
        sad.sin_family = AF_INET;         /* set family to Internet     */
        sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address   */

        /* Check command-line argument for protocol port and extract    */
        /* port number if one is specified.  Otherwise, use the default */
        /* port value given by constant PROTOPORT                       */

        port = PROTOPORT;       /* use default port number      */
        if (port > 0)                   /* test for illegal value       */
                sad.sin_port = htons((u_short)port);
        else {                          /* print error message and exit */
                fprintf(stderr,"bad port number \n");
                exit(1);
        }

        /* Map TCP transport protocol name to protocol number */

        if (((int*)(ptrp = getprotobyname("tcp"))) == 0) {
                fprintf(stderr, "cannot map \"tcp\" to protocol number");
                exit(1);
        }

        /* Create a socket */

        sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
        if (sd < 0) {
                fprintf(stderr, "socket creation failed\n");
                exit(1);
        }

        /* Bind a local address to the socket */

        if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
                fprintf(stderr,"bind failed\n");
                exit(1);
        }

        /* Specify size of request queue */

        if (listen(sd, QLEN) < 0) {
                fprintf(stderr,"listen failed\n");
                exit(1);
        }
		
		char posX[4], posY[4], pos[8];
		FILE *rota;
		char op1;
		
		//rota = fopen("local2.txt", "a+");
		
		printf("Servidor Iniciado\n");
		
		alen = sizeof(cad);
        sd2=accept(sd, (struct sockaddr *)&cad, &alen) ;
        limparBuffer(buf);
        printf("\n\nBem vindo!!!");
        //alteracao de op para tamanho 200	
  		do{
		  	INICIO: rota = fopen("local2.txt", "a+");
        	//send(sd2,buf,strlen(buf),0);
			n = recv(sd2, op, sizeof(op),0);
			
			printf("recebeu %s", op);
			
			//op1 = op[200];
			
			
		
			if(strcmp(op,"7") != 0){
				printf("\nX: %s \nY: %s", posX, posY);
				strcpy(pos, posX);
				strcat(pos, " ");
				strcat(pos, posY);
			
				printf("%s", pos);
			
				//fputs(pos, rota);
				fwrite(pos, 1, sizeof(pos), rota);
				putc('\n', rota);
			}
			
	        fclose(rota);
   		}while(strcmp(op,"7") != 0);
   		//fclose(rota);
       closesocket(sd2);
}