handle multipleclient Server using fork

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?

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
nt 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;
}
Sorry, but I didn't really understand your question. But after a short code review:

1. You should assign 0 to sock_client after line 136. Otherwise select(2) would return EBADF in errno.

2. select(2) may return more than one filedescriptors ready to be read. So as an optimization you may want to remove else from line 117. But its not a real problem because the fd will stay ready until being handled.

3. It's also not a bad idea to close sock_listen in your child server, f.e. at line 144.

4. Using value 0 to indicate an unused fd is not the best idea, because fd=0 represents a valid descriptor - namely stdin. Your algorithm may get confused about this (STDIN_FILENO and sock_xxx=<invalid> will be the same).
Topic archived. No new replies allowed.