Dorin Rusu
Dorin Rusu

Reputation: 1085

BufferedReader readLine remains blocked when using C client and Java server

I am currently working on a C client(A) which will communicate with a Java server(B), who is a client for a RMI server(C). A has to send its arguments to B which will send them to C, C will process them, resend them back to B and B will return them to A. So clearly A has to write to the socket and then remain blocked until B sends something back. The problem I am facing is that after A writes to the socket and remains blocked by select(), B is not unblocked from its own readLine.

If I remove the select() and read() from A, everything will work perfectly. Here's the code

A-client.c

    int err;
    err = send(sockfd,(const char*)toSend,size,0);//send counter
    if (err < 0){
        perror("Problem encountered while sending");
        exit(3);
    }
    //it always gets past this line
    fd_set read_set;
    int nb;
    FD_ZERO(&read_set); //initialize variables for select
    FD_SET(sockfd, &read_set); 
    nb = select(sockfd+1, &read_set, NULL, NULL, NULL);
    if (nb!=0){
        if (FD_ISSET(sockfd, &read_set)) {//if data is incoming from*/

            char word[4096]="";
            if (read(sockfd, word, 4096) == 0){
                perror("The server terminated prematurely");
                exit(4);
            }   
            for (i=0;i<4096;i++){
                printf("%c",word[i]);
            }
        }
    }

B-server.Java

connection = socket1.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//it always passes this line
String str = br.readLine();
//it passes this line only if i kill the C client or remove the read and select from the client
String[] splitStr = str.split("\\s+");
int argsLen = splitStr.length;

Upvotes: 0

Views: 324

Answers (1)

user207421
user207421

Reputation: 311008

  1. You are reading a line, but are you writing a line? With a line terminator? Clearly not.

  2. You are ignoring the count returned by read() in the server. You can't assume that read() filled the buffer.

Upvotes: 3

Related Questions