Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having a printing issue with my server. I want there to be simultaneous pri

ID: 659670 • Letter: I

Question

I am having a printing issue with my server. I want there to be simultaneous printing when I have 2 or more clients active on terminals. However, I am only printing from one client at a time. Once I close a client, the other clients are free to write to the server. What can I do to fix my problem?

I have tried to fork the printing section, which I think didn't really do anything. (Just realized if I do this, then the select system call is a waste, i'd rather use the select system call) *edit

while(TRUE) {

       FD_ZERO(&readfds);
      
       FD_SET(socket1, &readfds);
       FD_SET(socket2, &readfds);
       FD_SET(socket3, &readfds);

       select(socket3+1, &readfds, NULL, NULL, NULL);

//add socket1
       if(FD_ISSET(socket1, &readfds)) {
           if((client_socket1 = accept(socket1, NULL, NULL)) < 0) {
               perror("accept1");
               exit(EXIT_FAILURE);
           }
           printf("New Connection ");

           puts("Welcome message1 sent successfully ");
       }

//add socket2
       if(FD_ISSET(socket2, &readfds)) {
           if((client_socket2 = accept(socket2, (struct sockaddr *)&addr2, (socklen_t*)&addr2)) < 0) {
               perror("accept2");
               exit(EXIT_FAILURE);
           }
           printf("New Connection ");

           puts("Welcome message2 sent successfully ");
       }

//add socket 3
       if(FD_ISSET(socket3, &readfds)) {
           if((client_socket3 = accept(socket3, (struct sockaddr *)&addr3, (socklen_t*)&addr3)) < 0) {
               perror("accept3");
               exit(EXIT_FAILURE);
           }
           printf("New Connection ");

           puts("Welcome message3 sent successfully ");
       }

//print from socket 3
       while( (ready = read(client_socket3, buffer, sizeof(buffer))) > 0) {
           printf("%s ", buffer);
       }

//print from socket 2
       while( (ready = read(client_socket2, buffer, sizeof(buffer))) > 0) {
           printf("%s ", buffer);
       }

//print from socket 1
       while( (ready = read(client_socket1, buffer, sizeof(buffer))) > 0) {
           printf("%s ", buffer);
       }
   }

Explanation / Answer

Answer

for this while block

    //print from socket 1
    while( (ready = read(client_socket1, buffer, sizeof(buffer))) > 0) {
        printf("%s ", buffer);
    }

as

while (1)
{
    int result;
    result = recv(client_socket1, buffer, sizeof(buffer)-1, MSG_DONTWAIT);
    if ((result == -1) &&
             ((errno == EAGAIN) || (errno==EWOULDBLOCK)) )
    {
       

}
    else if ((result == -1) || (result == 0))
    {
        // remote close or unrecoverable error
        close(client_socket1);
        client_socket1=-1;
    }
    else
    {
        // null terminate the buffer before printing
        buffer[result] = '';
        printf("%s ", buffer);
    }
}

--------------------------------------------------------------------------------------------------------------------------------------------

for this while block

//print from socket 2
    while( (ready = read(client_socket2, buffer, sizeof(buffer))) > 0) {
        printf("%s ", buffer);
    }

as

while (1)
{
    int result;
    result = recv(client_socket2, buffer, sizeof(buffer)-1, MSG_DONTWAIT);
    if ((result == -1) &&
             ((errno == EAGAIN) || (errno==EWOULDBLOCK)) )
    {
       

}
    else if ((result == -1) || (result == 0))
    {
        // remote close or unrecoverable error
        close(client_socket2);
        client_socket2=-1;
    }
    else
    {
        // null terminate the buffer before printing
        buffer[result] = '';
        printf("%s ", buffer);
    }
}

--------------------------------------------------------------------------------------------------------

Replace all while block in the following

//print from socket 3
    while( (ready = read(client_socket3, buffer, sizeof(buffer))) > 0) {
        printf("%s ", buffer);
    }

As this

while (1)
{
    int result;
    result = recv(client_socket3, buffer, sizeof(buffer)-1, MSG_DONTWAIT);
    if ((result == -1) &&
             ((errno == EAGAIN) || (errno==EWOULDBLOCK)) )
    {
       

}
    else if ((result == -1) || (result == 0))
    {
        // remote close or unrecoverable error
        close(client_socket3);
        client_socket3=-1;
    }
    else
    {
        // null terminate the buffer before printing
        buffer[result] = '';
        printf("%s ", buffer);
    }
}