Ondrej.Behulek 0 Newbie Poster

Hello, I have a course that is Operational Systems.. My lecturer doesnt explain so much and after He say, You must solve this problem, because u r a programmer.. I have such a problem with linux programming.

I put my code here, He wants, I must allow multi-client to server. It means, server must accept all clients that request from multiple terminal..And also server can send value which clients connected And I must use fork. I tried some ways but no successful. And also he said, you must edit my server codes, you cant own code.
Can u please tell what Im doing wrong?

int main( int argn, char **arg )
{
  if ( argn <= 1 ) help( *arg );

  int port = 0;


  for ( int i = 1; i < argn; i++ )
  {
      if ( !strcmp( arg[ i ], "-d" ) )
          debug = 1;

      if ( !strcmp( arg[ i ], "-h" ) )
          help( *arg );

      if ( *arg[ i ] != '-' && !port )
      {
          port = atoi( arg[ i ] );
          break;
      }
  }

  if ( !port )
  {
      zprava( ZPR_INFO, "Missing or wrong port!" );
      help( *arg);
  }

  zprava( ZPR_INFO, "Server will listen on port: %d.", port );

  // creating socket
  int sock_listen = socket( AF_INET, SOCK_STREAM, 0 );
  if ( sock_listen == -1 )
  {
      zprava( ZPR_CHYBA, "Cant create socket.");
      exit( 1 );
  }

  in_addr addr_any = { INADDR_ANY };
  sockaddr_in srv_addr;
  srv_addr.sin_family = AF_INET;
  srv_addr.sin_port = htons( port );
  srv_addr.sin_addr = addr_any;

  // reusing port
  int opt = 1;
  if ( setsockopt( sock_listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) ) < 0 )
      zprava( ZPR_CHYBA, "Cant configure socket properties." );

  // binding address and port to socket
  if ( bind( sock_listen, (const sockaddr * ) &srv_addr, sizeof( srv_addr ) ) < 0 )
  {
      zprava( ZPR_CHYBA, "binding failed." );
      close( sock_listen );
      exit( 1 );
  }

  // apllication will listen on port
  if ( listen( sock_listen, 1 ) < 0 )
  {
      zprava( ZPR_CHYBA, "cant listen on this port." );
      close( sock_listen );
      exit( 1 );
  }

  int sock_client = 0;



  while ( 1 )
  {
      char buf[ 100 ];      
      fd_set read_wait_set;
      FD_ZERO( &read_wait_set );

      FD_SET( STDIN_FILENO, &read_wait_set );

     //handle - listen or connect to server
      if ( sock_client )
          {
              FD_SET( sock_client, &read_wait_set );


          }

      else
      {

          FD_SET( sock_listen, &read_wait_set );
      }

      //waiting data from handle
      if ( select( MAX( sock_client, sock_listen ) + 1,
                           &read_wait_set, 0, 0, 0 ) < 0 ) break;


      // data on stdin?
      if ( FD_ISSET( STDIN_FILENO, &read_wait_set ) )
      {
          // readin from stdin

          int l = read( STDIN_FILENO, buf, sizeof( buf ) );
          if ( l < 0 )
              zprava( ZPR_CHYBA, "Cant read data from stdin." );
          else
              zprava( ZPR_LADENI, "Read %d bytes from stdin.", l );

          // send data to client
          l = write( sock_client, buf, l );
          if ( l < 0 )
              zprava( ZPR_CHYBA, "cant send data to client." );
          else
              zprava( ZPR_LADENI, "Sent %d bytes to client.", l );
      }


       else if ( FD_ISSET( sock_listen, &read_wait_set ) )
      {
          sockaddr_in rsa;

          int rsa_size = sizeof( rsa );

          // accepting connection
          sock_client = accept( sock_listen, ( sockaddr * ) &rsa, ( socklen_t * ) &rsa_size );

          if ( sock_client == -1 )
          {
              zprava( ZPR_CHYBA, "Connection failed." );
              close( sock_listen );
              exit( 1 );
          }


        if (fork() !=0)
        {
        close (sock_client);
        continue;
        }

        else {

        if (fork() == 0)
        {


          uint lsa = sizeof( srv_addr );

          // getting own IP
          getsockname( sock_client, ( sockaddr * ) &srv_addr, &lsa );

          zprava( ZPR_INFO, "My IP: '%s'  port: %d",
                   inet_ntoa( srv_addr.sin_addr ), ntohs( srv_addr.sin_port ) );

          // getting IP of client
          getpeername( sock_client, ( sockaddr * ) &srv_addr, &lsa );

          zprava( ZPR_INFO, "client IP: '%s'  port: %d",
                   inet_ntoa( srv_addr.sin_addr ), ntohs( srv_addr.sin_port ) );

          zprava( ZPR_INFO, "ENter 'quit' to quit server." );
        }



        }
          }
     }




      // data from client
      else if ( FD_ISSET( sock_client, &read_wait_set ) )
      {

          // precteme data od klienta
          int l = read( sock_client, buf, sizeof( buf ) );
          if ( !l )
          {
              zprava( ZPR_LADENI, "client closed the connectio." );
              close( sock_client );
              sock_client = 0;
              break;
          }
          else if ( l < 0 )
              zprava( ZPR_LADENI, "Cant read data from client." );
          else
              zprava( ZPR_LADENI, "Read %d bytes od from client.", l );

          // all data to stdout
          l = write( STDOUT_FILENO, buf, l );
          if ( l < 0 )
              zprava( ZPR_CHYBA, "Cant write to stdout." );

          // checking if client wants to terminate the connection
          if ( !strncasecmp( buf, "close", 5 ) )
          {
              zprava( ZPR_INFO, "Client entered 'close' connection is closing." );
              zprava( ZPR_INFO, "Waiting for new client." );
              close( sock_client );
              sock_client = 0;
          }

       }


      //quit server?
      if ( !strncasecmp( buf, "quit", 4 ) )
      {
          close( sock_client );
          zprava( ZPR_INFO, " 'quit'. Server si closing connection.\n" );
          break;
      }


  }

  close( sock_listen );

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.