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 uploaded server&client files here. Can u say where is my problem?

this is just main function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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;
}


server

http://www.sendspace.com/file/cfnvpm

client

http://www.sendspace.com/file/xevurl


Please help me, I dont want to fail this course.. I'm an erasmus student also...
Last edited on
Very quickly I see two problems.

1
2
srv_addr.sin_port = htons( port );
srv_addr.sin_addr = addr_any;    // <- if the above uses htons(), then this should use... ? 



Secondly, you create two child processes instead of 1.
Hi Mr. Smith,
Thank you for answer but actually i dont understand this solution. I think, port or htons funct. is not a problem for coding. I know that problem is about fork or like this...

and secondly, where i must create two child in code?

i wrote




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

else {

if (fork() == 0)
{
fork() returns one of three values:

-1: error, no child process created
0: fork() returns this in the child
>0: fork() returns this in the parent.

Your code:

1
2
3
4
5
6
7
8
9
10
11
if( fork() != 0 )
{
    // Handles either an error, or this is the parent.
}
else
{
   // This has to be the child process

   if( fork() == 0 ) // and now this creates a child process of the child you already created.
    // ...
}


You need to/should htonl( addr_any )
(either htonl does nothing on your platform because network order == host order, or your code is broke).

thank u for attention, but i gave up to finish this task. I will focus on my project.It will be harder than this task :)

Topic archived. No new replies allowed.