【发布时间】:2013-11-22 18:01:38
【问题描述】:
我想实现以下功能
while (true)
{
if client sends something
process it
else wait till something is send }
我尝试了以下方法,但没有成功,它处理一件事然后停止工作 谁能帮我? 我在这里搜索过这样的案例,但没有找到任何东西。如果有人能给出一个例子,说明如何从上面的描述中的 while 循环中读取套接字,我将不胜感激。
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
DataOutputStream outToclient =new DataOutputStream(client.getOutputStream());
while (true){
if ((request=inFromClient.readLine())!=null){
System.out.println("ser "+request);
msg1= new msgs();
if(msg1.IsListReq(request))
{
System.out.println("Ser :List req");
for (int i = 0; i <listOfFiles.length ; i++)
{
if (listOfFiles[i].isFile())
{
files[i] = listOfFiles[i].getName();
}
}
//prepare the respones
msg1.MakeFileListResponse (files);
outToclient.writeBytes(msg1.getMsg()+'\n');
} // end of processing List Request
} // end of first if statement
} end of while loop
【问题讨论】:
-
请应用正确的格式!
-
我不确定你的花括号在哪里闭合
-
请将您的问题改写为Short, Self Contained, Correct (Compilable), Example。我们无法编译这个。另外,请重新缩进,以便我们阅读。
-
很抱歉,我现在就编辑它
标签: java sockets loops inputstream