0

I am coding a server client type socket communication program with C. I am using winsock2.h, and I am trying to implement a way to disconnect the client from the server by the press of a key. The part of the code handling the infinite is this.

while ( ( client = accept( server , ( SOCKADDR * ) &clientAddr , &clientAddrSize ) ) != INVALID_SOCKET) {

    // IP do cliente
    char *clientIP = inet_ntoa( clientAddr.sin_addr );
    IP_cliente = clientIP; // iguala a variável global, para ser acessível
    // em todo o lado

    // envia mensagem de boas vindas ao cliente
    serverWelcomeMsg( client , msg , log_server , clientIP );
    // Loop infinito, que correrá até o cliente desligar.   
    for (;;) {
        if ( _kbhit() ) break;
            // mete o buffer a 0's
            memset( buffer , 0 , sizeof( buffer ) );
            // recv_size = bytes recebidos
            int recv_size;
            recv_size = recv( client , buffer , BUFF_LEN , 0 );
            // se forem recebidos 0 bytes, o cliente desligou-se
            // o mesmo acontece se recv_size for igual a SOCKET_ERROR

            if ( recv_size == 0 ) {

                closesocket( server );
                sprintf_s( log_server , BUFF_LEN , "\t%s Ligacao terminada com %s\n" , Now() , clientIP );
                fprintf_s( serverLogFile , log_server );
                printf( "\n%s - Cliente desligou-se  \n" , Now() );
                return 0;

            }

            if ( recv_size == SOCKET_ERROR ) {

                closesocket( server );
                sprintf_s( log_server , BUFF_LEN , "\t%s Ligacao terminada com %s\n" , Now() , clientIP );
                fprintf_s( serverLogFile , log_server );
                printf( "\n%s - Cliente desligou-se  \n" , Now() );
                return 0;

            }

            else {

                // Adiciona um EOF no buffer para o transformar numa
                // string como deve de ser antes de imprimir
                buffer [ recv_size ] = '\0';

                if ( strcmp( buffer , "1" ) == 0 ) {

                    printf( "\n%s - A inserir Formando...\n" , Now() );
                    adicionarFormandoTelnet( client );
                    FileWriteFormandoXML( head );

                }

                if ( strcmp( buffer , "2" ) == 0 ) {

                    printf( "\n%s - A apagar formando...\n" , Now() );
                    head = deleFormandoTelnet( client , head );
                    FileWriteFormandoXML( head );

                }

                if ( strcmp( buffer , "3" ) == 0 ) {

                    printf( "\n%s - Apresentacao de lista de formandos...\n" , Now() );
                    displayFormandoTelnet( client );

                }
            }
        }


}

Definition of buffer:

buffer = ( char ) malloc( (BUFF_LEN + 1) * sizeof( char ) );
THingamagick
  • 95
  • 1
  • 2
  • 13
  • There are a couple worries... 'memset( buffer , 0 , sizeof( buffer ) );' - a waste of cycles. 'buffer [ recv_size ] = '\0';' - a better plan than the memset, providing that the declared size of 'buffer' is 'BUFF_LEN+1', else the receipt of all 'BUFF_LEN' bytes will result in an out-of-bonds write of the NUL :( – ThingyWotsit May 15 '17 at 20:12
  • `fprintf_s( serverLogFile , log_server );` is bad when `%` appears in `log_server`. Suggest `fprintf( serverLogFile , "%s", log_server );` or `fputs(log_server, serverLogFile);` – chux - Reinstate Monica May 15 '17 at 20:15
  • Post definition of `buffer[]`. – chux - Reinstate Monica May 15 '17 at 20:18
  • ... anyway, your actual problem? Well, short of building a 'real' server that can handle multiple clients concurrently, I suggest you try threading off a keyboard read thread that closes the 'client' socket, so forcing the recv() to return early with an error. A real server would be a better starting point:) – ThingyWotsit May 15 '17 at 20:19
  • I know a real server handles multiples connections, but this assignment explicitly says the server should only handle one connection. – THingamagick May 15 '17 at 20:29
  • buffer definition is char *buffer = ( char* ) malloc( BUFF_LEN * sizeof( char ) ); – THingamagick May 15 '17 at 20:31
  • Don't you mean `char *buffer = malloc (BUFF_LEN * sizeof *buffer);` ?? (`buffer` should be `char *`, not `char`) There is NO need to cast the return of `malloc`, it is unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. – David C. Rankin May 15 '17 at 20:42
  • Can you do that in Visual studio? it gives me error everytime I Don´t cast the return of malloc. – THingamagick May 15 '17 at 20:45

0 Answers0