Java 为什么我的程序在我的服务器上运行两次?
我正在编写tcp客户机-服务器程序。当客户端键入“Hello”时,服务器返回其当前目录的文件和目录列表。当客户端键入“FileDownload”时,它将从服务器下载所选文件。Java 为什么我的程序在我的服务器上运行两次?,java,server,tcp,client,Java,Server,Tcp,Client,我正在编写tcp客户机-服务器程序。当客户端键入“Hello”时,服务器返回其当前目录的文件和目录列表。当客户端键入“FileDownload”时,它将从服务器下载所选文件。 当我键入“Hello”时,它工作正常,但当我键入“FileDownload”时,它在服务器端运行两次else if(received.contains(“FileDownload”)块。因此,服务器将发送两次数据,这将导致客户端出现其他问题 以下是服务器代码: public static void main(String[
当我键入“Hello”时,它工作正常,但当我键入“FileDownload”时,它在服务器端运行两次
else if(received.contains(“FileDownload”)
块。因此,服务器将发送两次数据,这将导致客户端出现其他问题
以下是服务器代码:
public static void main(String[] args) throws IOException {
ServerSocket servSock = new ServerSocket(1333);
String received="";
String[] s = null;
File[] f1;
int i=0;
File f=new File(System.getProperty("user.dir"));
f1=f.listFiles();
for(File f2:f1) {
if(f2.isDirectory())
System.out.println(f2.getName() + "\t<DIR>\t" + i);
if(f2.isFile())
System.out.println(f2.getName() + "\t<FILE>\t" + i);
i++;
}
while (true) {
Socket client = servSock.accept();
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
PrintWriter pw = new PrintWriter(out, true);
s=f.list();
while(true) {
received = reader.readLine();
if(received.equals("END")) break;
if(received.equals("Hello")) {
System.out.println("Hello-Start");
int length=f1.length;
pw.println(length);
i=0;
for(File f2:f1) {
if(f2.isDirectory())
pw.println(f2.getName() + "\t<DIR>\t" + i);
if(f2.isFile())
pw.println(f2.getName() + "\t<FILE>\t" + i);
i++;
}
pw.println("Options: " + "\tFileDownload <FID>" + "\tFileUpload <name>" + "\tChangeFolder <name>");
System.out.println("Hello-End");
}
else if(received.contains("FileDownload")) {
System.out.println("FileDownload-Start");
int j=-1;
try {
j=Integer.parseInt(received.substring(13).trim());
}catch(NumberFormatException e) {
System.err.println("error: " + e);
}
if(j>0 && j<s.length) {
FileInputStream fi=new FileInputStream(s[j]);
byte[] b=new byte[1024];
System.out.println("file: "+s[j]);
pw.println(s[j]);
fi.read(b,0,b.length);
out.write(b,0,b.length);
System.out.println("FileDownload-End");
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
ServerSocket servSock=新的ServerSocket(1333);
接收字符串=”;
字符串[]s=null;
文件[]f1;
int i=0;
文件f=新文件(System.getProperty(“user.dir”);
f1=f.listFiles();
用于(文件f2:f1){
if(f2.isDirectory())
System.out.println(f2.getName()+“\t\t”+i);
if(f2.isFile())
System.out.println(f2.getName()+“\t\t”+i);
i++;
}
虽然(正确){
Socket client=servSock.accept();
InputStream in=client.getInputStream();
OutputStream out=client.getOutputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(in));
PrintWriter pw=新的PrintWriter(输出,真);
s=f.list();
while(true){
received=reader.readLine();
如果(接收到的)中断等于(“结束”);
if(received.equals(“Hello”)){
System.out.println(“你好开始”);
int-length=f1.length;
pw.println(长度);
i=0;
用于(文件f2:f1){
if(f2.isDirectory())
println(f2.getName()+“\t\t”+i);
if(f2.isFile())
println(f2.getName()+“\t\t”+i);
i++;
}
println(“选项:“+”\tFileDownload“+”\tFileUpload“+”\tChangeFolder”);
System.out.println(“Hello End”);
}
else if(received.contains(“文件下载”)){
System.out.println(“文件下载启动”);
int j=-1;
试一试{
j=Integer.parseInt(received.substring(13.trim());
}捕获(数字格式){
System.err.println(“错误:+e”);
}
如果(j>0&&j是因为您的客户请求数据两次:
msg = sc.nextLine();
pw.println(msg); // <-- this is the first time
msg=sc.nextLine();
pw.println(msg);//为了找到问题,你做了什么?
msg = sc.nextLine();
pw.println(msg); // <-- this is the first time
else if(msg.contains("FileDownload")) {
System.out.println("FileDownload-Start");
pw.println(msg); // <-- this is the second time