我目前正在开发一个 C 客户端(A),它将与 Java 服务器(B)进行通信,Java 服务器是 RMI 服务器(C)的客户端。A必须将其参数发送给B,B将它们发送给C,C将处理它们,将它们重新发送回B,B将它们返回给A。显然A必须写入套接字然后保持阻塞,直到B发送一些东西背部。我面临的问题是,在 A 写入套接字并被 select() 阻塞后,B 并没有从它自己的 readLine 中解除阻塞。
如果我从 A 中删除 select() 和 read(),一切都会完美运行。这是代码
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服务器.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;