Code with Sockets locking in the middle

Asked

Viewed 50 times

0

Because of the matter of college networks, lately I have had experiences to learn how to deal with sockets in C. But today, trying to develop a game of sailing to practice, I went through a somewhat peculiar problem. Apparently, the code I wrote doesn’t have any syntax errors or anything like that, but when I try to run after compiling with GCC (5.4), the program crashes in the second printf with "All right!" after the "2" option is stored and inserted into the "type" variable, right in Else if. If anyone wants or can, I would even be grateful to receive some feedback on the code:

    else if(tipo == 2){
    h = gethostbyname("127.0.0.1");
    if(h == NULL){
        printf("Impossível localizar o host");
        exit(1);
    }
    // Setup do servidor. Primeiro faz a cópia bit-a-bit do endereço do server para a struct, depois se seta o sin_family na struct e então a porta
    memcpy((char *) &jog1.sin_addr.s_addr, h->h_addr_list, h->h_length);
    jog1.sin_family = AF_INET;
    jog1.sin_port = htons(PORTA);

    jog2.sin_addr.s_addr = htonl(INADDR_ANY);
    jog2.sin_port = (0);
    jog2.sin_family = AF_INET;

    soquete = socket(AF_INET, SOCK_DGRAM, 0);
    if(soquete < 0){
        printf("\nOps! Erro ao criar o soquete... Tente novamente\n");
        exit(1);
    }
    binded = bind(soquete, (struct sockaddr *) &jog2, sizeof(jog2));
    if(binded < 0){
        printf("\nErro ao bindar a porta! Tente novamente\n");
        close(soquete);
        exit(1);
    }
    printf("\nTudo certo! Enviando dados ao jogador 1");
    printf("...");
    printf("\nTudo certo! Enviando dados ao jogador 1");
    printf("\nTudo certo! Enviando dados ao jogador 1");
    clilen = sizeof(jog2);
    msg[0] = (char) PRONTO;
    envio = sendto(soquete, msg, sizeof((char) PRONTO) + 1, 0, (struct sockaddr *) &jog1, sizeof(jog1));
    resposta = recvfrom(soquete, msg, MAX_MSG, 0, (struct sockaddr *) &jog1, &clilen);
    if(msg[0] == (char) PRONTO){
        printf("\nJogador 1 conectado com sucesso! %u:%u", ntohl(jog1.sin_addr.s_addr), ntohs(jog1.sin_port));
    }
    else{
        printf("\nErro! Comando inválido recebido!");
        close(soquete);
        exit(1);
    }
}
  • 1

    The code is not complete. Surely the error is in the buffer size msg past to sendto(). The expression sizeof((char) PRONTO) + 1 is very strange!

  • Thanks for the feedback! - Actually I didn’t post the full code because I’m new to the forum, and when I went to do the collage, it was all broken. I tried to take out "+1" and it didn’t work, as well as previously set a buffer size of the msg array, but still, the bizarre error continues. Just rectifying, there is a sequence of 4 printf() in this code, but for some reason, the program always hangs on the third.Also an addendum, "READY" is a "sign" that I created in an Enum (I just learned this, hehehe) client or server send to each other when they are ready.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.