@@ -171,7 +171,8 @@ void print_client_addr(struct sockaddr_in addr) {
171
171
/* Handle all communication with the client */
172
172
void * handle_client (void * arg ) {
173
173
char buff_out [BUFFER_SZ ];
174
- char buff_in [BUFFER_SZ / 2 ];
174
+ char * buff_in ;
175
+ size_t buff_in_size ;
175
176
int rlen ;
176
177
177
178
cli_count ++ ;
@@ -181,20 +182,21 @@ void *handle_client(void *arg) {
181
182
print_client_addr (cli -> addr );
182
183
printf (" referenced by %d\n" , cli -> uid );
183
184
185
+ FILE * input_stream = fdopen (cli -> connfd , "r" );
186
+
184
187
// protocol negotiation
185
- if ((rlen = read ( cli -> connfd , buff_in , sizeof ( buff_in ) - 1 )) < 0 ) {
188
+ if ((rlen = getline ( & buff_in , & buff_in_size , input_stream )) < 0 ) {
186
189
printf ("version negotation: error reading from socket\n" );
187
190
goto CLIENT_CLOSE ;
188
191
}
189
- buff_in [rlen ] = '\0' ;
190
192
strip_newline (buff_in );
191
- char * cmd = strtok (buff_in , " " );
193
+ char * cmd = strtok (buff_in , " " );
192
194
if (cmd == NULL || cmd [0 ] != 'v' ) {
193
195
printf ("version negotiation: client command not 'v'\n" );
194
196
195
197
goto CLIENT_CLOSE ;
196
198
}
197
- char * client_version = strtok (NULL , " " );
199
+ char * client_version = strtok (NULL , " " );
198
200
if (client_version == NULL ) {
199
201
printf ("version negotiation: unable to parse client version\n" );
200
202
send_message_self ("can't parse version\n" , cli );
@@ -221,8 +223,7 @@ void *handle_client(void *arg) {
221
223
printf ("sent serialized canvas\n" );
222
224
223
225
/* Receive input from client */
224
- while ((rlen = read (cli -> connfd , buff_in , sizeof (buff_in ) - 1 )) > 0 ) {
225
- buff_in [rlen ] = '\0' ;
226
+ while ((rlen = getline (& buff_in , & buff_in_size , input_stream )) > 0 ) {
226
227
buff_out [0 ] = '\0' ;
227
228
strip_newline (buff_in );
228
229
@@ -277,7 +278,7 @@ void *handle_client(void *arg) {
277
278
}
278
279
free (msg );
279
280
}
280
- CLIENT_CLOSE :
281
+ CLIENT_CLOSE :
281
282
282
283
/* Close connection */
283
284
close (cli -> connfd );
0 commit comments