Java 如何将switch语句中的条件返回while?
如何在switch语句中将操作返回while循环?客户端无法返回while语句。我的意思是,每当响应来自服务器时,客户机必须再次从while循环开始,以便客户机可以选择他的选择。这是系统故障吗?(注意:这是从其他人那里复制的代码)以下是客户端和服务器: 客户:Java 如何将switch语句中的条件返回while?,java,sockets,while-loop,Java,Sockets,While Loop,如何在switch语句中将操作返回while循环?客户端无法返回while语句。我的意思是,每当响应来自服务器时,客户机必须再次从while循环开始,以便客户机可以选择他的选择。这是系统故障吗?(注意:这是从其他人那里复制的代码)以下是客户端和服务器: 客户: ............. while (true) { String userchoice = console.readLine("Enter your choice :"); int choice
.............
while (true) {
String userchoice = console.readLine("Enter your choice :");
int choice= Integer.parseInt(userchoice);
switch (choice){
..........
case 2: // for viewing files in the client's directory
try{
Socket mysoc = new Socket("localhost", 9001);
String user_name = username;
DataOutputStream ToServer = new DataOutputStream(mysoc.getOutputStream());
ToServer.writeBytes(user_name + '\n');
BufferedReader FromServer = new BufferedReader(new InputStreamReader(mysoc.getInputStream()));
String line = null;
while ((line = FromServer.readLine()) != null) {
System.out.println(line);
}
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
break;
............
}
服务器:
import java.io.*;
import java.net.*;
class DirList
{
public static void main(String argv[]) throws Exception
{
String clientString;
//String replyString;
ServerSocket welcomeSoc = new ServerSocket(9001);
while(true)
{
Socket connectionSocket = welcomeSoc.accept();
BufferedReader FromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
PrintWriter ToClient = new PrintWriter(connectionSocket.getOutputStream(),true);
clientString = FromClient.readLine();
System.out.println("Received view songs request from : " + clientString);
String path = "/home/sri/Songs/";
String text="";
File f = new File(path);
File[] listOfFiles = f.listFiles();
for (int j = 0; j < listOfFiles.length; j++) {
if (listOfFiles[j].isFile()) {
text = listOfFiles[j].getName();
ToClient.println(text);
}
}
}
}
}
import java.io.*;
导入java.net。*;
类目录表
{
公共静态void main(字符串argv[])引发异常
{
字符串clientString;
//串重串;
ServerSocket welcomeSoc=新的ServerSocket(9001);
while(true)
{
套接字连接Socket=welcomeSoc.accept();
BufferedReader FromClient=新的BufferedReader(新的InputStreamReader(connectionSocket.getInputStream());
PrintWriter ToClient=新的PrintWriter(connectionSocket.getOutputStream(),true);
clientString=FromClient.readLine();
System.out.println(“收到来自:“+clientString”的查看歌曲请求);
字符串路径=“/home/sri/Songs/”;
字符串文本=”;
文件f=新文件(路径);
File[]listOfFiles=f.listFiles();
对于(int j=0;j
你为什么不像这样在最外面的贴上标签呢
outer:while(true)
然后使用continue
关键字返回标签
继续外部
你为什么不像这样在最外面的贴上标签呢
outer:while(true)
然后使用continue
关键字返回标签
继续外部代码>在完成任务后,尝试关闭服务器中的PrintWriter
和套接字
@Sri,关于您在评论中提出的问题:
我所说的Socket是指连接Socket
ServerSocket
将在服务器生命周期内保留在那里。但您将为每个用户提供一个新的连接socket
。因此,每个connectionSocket将只服务于一个客户端 在服务器
完成任务后,尝试关闭打印机
和套接字
@Sri,关于您在评论中提出的问题:
我所说的Socket是指连接Socket
ServerSocket
将在服务器生命周期内保留在那里。但您将为每个用户提供一个新的连接socket
。因此,每个connectionSocket将只服务于一个客户端 哪个while循环最内层还是最外层?Hi,最外层-以便用户可以输入另一个选项..哪个while循环最内层还是最外层?Hi,最外层-以便用户可以输入另一个选项..outer:while(true){String userchoice=console.readLine(“输入您的选项:”);案例2://要查看客户端目录中的文件,请尝试{………System.out.println(行);}继续外部;}……
继续外部
应位于while
循环内。在我看来,它是外部的。@EJP oops my bad,不会再这样评论:)outer:while(true){String userchoice=console.readLine(“输入您的选择”);….case 2://要查看客户端目录中的文件,请尝试{………System.out.println(line);}continue outer;}….
继续外部
应该在循环中,而循环中。对我来说,它看起来像是外部的。@EJP糟糕透了,不会再这样评论了:)是的,我一直怀疑服务器端本身是否有问题,谢谢:)。但如果我们关闭服务器中的套接字,多个用户如何访问它?请澄清……是的,我一直怀疑服务器端本身是否存在问题,谢谢:)。但如果我们关闭服务器中的套接字,多个用户如何访问它?请澄清。。