Thread: Server Problem !! (I hate C++)

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Server Problem !! (I hate C++)

    Hi everyone,

    Sorry for my title, But This language make me cold to programming language.I was a succesful programmer but C++ uwww its horrible..

    Anyway, I have a course that is Operational System.. My lecturer doesnt explain sso much and after He say, You must solve this problem, because u r a programmer.. I havent information about linux programmin and I cant.. I am good in c#,vb.neteven java ... anyway

    I put my codes here, He wants, I must allow multi-client to server. It means, server must accept all clients that request..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.

    I attached server&client file here. Can u say where is my problem?

    this is just main function

    Code:
    int main( int argn, char **arg )
    {
      if ( argn <= 1 ) help( *arg );
    
      int port = 0;
    
      // zpracovani prikazoveho radku
      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, "Chybejici nebo spatne cislo portu!" );
          help( *arg);
      }
    
      zprava( ZPR_INFO, "Server bude poslouchat na portu: %d.", port );
    
      // vytvoreni socketu
      int sock_listen = socket( AF_INET, SOCK_STREAM, 0 );
      if ( sock_listen == -1 )
      {
          zprava( ZPR_CHYBA, "Nelze vytvorit 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;
    
      // socket smi znovu okamzite pouzit jiz drive pouzite cislo portu
      int opt = 1;
      if ( setsockopt( sock_listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) ) < 0 )
          zprava( ZPR_CHYBA, "Nelze nastavit vlastnosti socketu." );
    
      // prirazeni adresy a portu socketu
      if ( bind( sock_listen, (const sockaddr * ) &srv_addr, sizeof( srv_addr ) ) < 0 )
      {
          zprava( ZPR_CHYBA, "Prirazeni adresy selhalo." );
          close( sock_listen );
          exit( 1 );
      }
    
      // aplikace bude naslouchat na zadanem portu
      if ( listen( sock_listen, 1 ) < 0 )
      {
          zprava( ZPR_CHYBA, "Nelze naslouchat na pozadovanem portu." );
          close( sock_listen );
          exit( 1 );
      }
    
      int sock_client = 0;
    
      // jedeme!
    
      while ( 1 )
      {
    
    
    
          char buf[ 100 ];
    
          // mnozina hadlu
          fd_set read_wait_set;
          // vynulovani mnoziny
          FD_ZERO( &read_wait_set );
    
          // pridani handlu stdin
          FD_SET( STDIN_FILENO, &read_wait_set );
    
          // vyber handlu - listen nebo spojeni se serverem?
          // cekame na spojeni, nebo na data od serveru?
    
    
          if ( sock_client )
              {
                  FD_SET( sock_client, &read_wait_set );
    
    
              }
    
    
          else
          {
    
              FD_SET( sock_listen, &read_wait_set );
    
          //    fork();
          }
    
          // cekame na data u nektereho handlu
          if ( select( MAX( sock_client, sock_listen ) + 1,
                               &read_wait_set, 0, 0, 0 ) < 0 ) break;
    
    
    
    
          // data na stdin?
          if ( FD_ISSET( STDIN_FILENO, &read_wait_set ) )
          {
              // cteme data ze stdin
    
    
    
              int l = read( STDIN_FILENO, buf, sizeof( buf ) );
              if ( l < 0 )
                  zprava( ZPR_CHYBA, "Nelze cist data ze stdin." );
              else
                  zprava( ZPR_LADENI, "Nacteno %d bytu ze stdin.", l );
    
              // posleme data klientovi
              l = write( sock_client, buf, l );
              if ( l < 0 )
                  zprava( ZPR_CHYBA, "Nelze zaslat data klientovi." );
              else
                  zprava( ZPR_LADENI, "Odeslano %d bytu klientovi.", l );
          }
    
    
           else if ( FD_ISSET( sock_listen, &read_wait_set ) )
          {
              sockaddr_in rsa;
    
              int rsa_size = sizeof( rsa );
    
              // prijmeme nove spojeni
              sock_client = accept( sock_listen, ( sockaddr * ) &rsa, ( socklen_t * ) &rsa_size );
    
              if ( sock_client == -1 )
              {
                  zprava( ZPR_CHYBA, "Spojeni se nezdarilo." );
                  close( sock_listen );
                  exit( 1 );
              }
    
    
            if (fork() !=0)
            {
            close (sock_client);
            continue;
            }
    
            else {
    
            if (fork() == 0)
            {
    
    
              uint lsa = sizeof( srv_addr );
    
              // ziskani vlastni IP
              getsockname( sock_client, ( sockaddr * ) &srv_addr, &lsa );
    
              zprava( ZPR_INFO, "Moje IP: '%s'  port: %d",
                       inet_ntoa( srv_addr.sin_addr ), ntohs( srv_addr.sin_port ) );
    
              // ziskani IP klienta
              getpeername( sock_client, ( sockaddr * ) &srv_addr, &lsa );
    
              zprava( ZPR_INFO, "Klient IP: '%s'  port: %d",
                       inet_ntoa( srv_addr.sin_addr ), ntohs( srv_addr.sin_port ) );
    
              zprava( ZPR_INFO, "Zadejte 'quit' pro ukonceni procesu serveru." );
            }
    
       
    
            }
              }
         }
    
    
    
    
          // data od klienta?
          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, "Klient uzavrel spojeni." );
                  close( sock_client );
                  sock_client = 0;
                  break;
              }
              else if ( l < 0 )
                  zprava( ZPR_LADENI, "Nelze precist data od klienta." );
              else
                  zprava( ZPR_LADENI, "Precteno %d bytu od klienta.", l );
    
              // vsechna data na stdout
              l = write( STDOUT_FILENO, buf, l );
              if ( l < 0 )
                  zprava( ZPR_CHYBA, "Nelze zapsat na stdout." );
    
              // konrola, zda klient nezada o ukonceni spojeni
              if ( !strncasecmp( buf, "close", 5 ) )
              {
                  zprava( ZPR_INFO, "Klient zaslal 'close' pozadavek, ukoncujeme spojeni." );
                  zprava( ZPR_INFO, "Cekame na pripojeni noveho klienta." );
                  close( sock_client );
                  sock_client = 0;
              }
    
           }
    
    
          // byl pozadavek na ukonceni prace serveru?
          if ( !strncasecmp( buf, "quit", 4 ) )
          {
              close( sock_client );
              zprava( ZPR_INFO, "Byl zadan pozadavek 'quit'. Server konci svou cinnost.\n" );
              break;
          }
    
    
      }
    
      close( sock_listen );
    
      return 0;
    }

    Please help me, I dont want to fail this course.. I'm an erasmus student also...
    Attached Files Attached Files

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ertan2002 View Post
    I must allow multi-client to server. It means, server must accept all clients that request..And also server can send value which clients connected And I must use fork. I tried some ways but no successful.

    I attached server&client file here. Can u say where is my problem?
    Which problem? Perhaps you should think about them one at a time.

    How To Ask Questions The Smart Way
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            if (fork() !=0)
            {
            close (sock_client);
            continue;
            }
    
            else {
    
            if (fork() == 0)
    Well the first problem is you're calling fork() twice, so you're getting more child processes than you think.

    It should be
    Code:
    pid = fork();
    if ( pid == 0 ) {
      // child, close listener socket, deal with client socket
    } else if ( pid > 0 ) {
      // parent, close client socket, do whatever
    } else {
      // panic
    }
    Also, it seems that both parent and child seem to make use of sock_client, and I can see that leading to confusion.
    For one thing, the server will only deal with the last connected client.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem isn't as much C++ as your teacher's code. C++ is not a bad language, albeit network programming can be a pain.
    In short, blame your teacher, not the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  2. BSD Socket Server Problem
    By ma_mazmaz in forum C Programming
    Replies: 3
    Last Post: 01-26-2009, 01:03 PM
  3. client -server problem
    By hegdeshashi in forum C Programming
    Replies: 1
    Last Post: 06-13-2006, 11:13 PM
  4. ftp server name problem.
    By earth_angel in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 08:19 AM
  5. TCP client/server problem.
    By WL in forum C Programming
    Replies: 0
    Last Post: 10-15-2001, 12:55 AM