gpt4 book ai didi

java - 客户端-服务器程序中的 while 循环

转载 作者:行者123 更新时间:2023-12-01 12:03:47 25 4
gpt4 key购买 nike

所以我必须做一些简单的客户端-服务器程序。服务器端`

public static void main(String[] args) throws IOException
{
ServerSocket ss = null;
try{ss= new ServerSocket(119);}
catch(IOException ioe){System.out.println("Can't connect to port.");}

Socket sock = null;
try{sock = ss.accept();}
catch(IOException ioe){System.out.println("Can't connect to client.");}
System.out.println("Connection successful.");

PrintStream out = null;
Scanner in = null;

try{
out = new PrintStream(sock.getOutputStream());
in = new Scanner(sock.getInputStream());
}catch(Exception e)
{
System.out.println("Error.");
}

String line;
String lines = "";

while((line = in.nextLine()) != null)
{
switch(line)
{
case "list":out.println("Printing something");break;
case "loop":
{
FileReader fr = new FileReader("D:\\Eclipse\\TestFiles\\text2.txt");
BufferedReader br = new BufferedReader(fr);
while((lines = br.readLine()) != null)
{
out.println(lines);
}
break;
}
default:
{
if(!(line.equals("end"))) out.println("Unknown command.Try again.");break;
}
}
out.flush();
if(line.equals("end"))
{
out.println("Connection over.");
break;
}
}

try{
in.close();
out.close();
sock.close();
ss.close();
}catch(IOException ioe){}

}

`

客户端

public static void main(String[] args)
{
Socket sock = null;
PrintStream out = null;
Scanner in = null;
Scanner sIn = null;

try{
sock = new Socket("localhost",119);
out = new PrintStream(sock.getOutputStream());
in = new Scanner(sock.getInputStream());
sIn = new Scanner(System.in);
}catch(Exception e){
System.out.println("Error connecting to server.");
System.exit(1);
}

System.out.println("Connection successful.");

String temp = "";

while((temp = sIn.nextLine()) != null)
{
out.println(temp);
while(in.hasNextLine())System.out.println(in.nextLine());
out.flush();
if(temp.equals("end")) break;
}

try{
sock.close();
in.close();
out.close();
}catch(IOException ioe){}

}

我的问题是,在客户端,当第二个 while 循环(第一个 while 循环内的 on)必须结束时,它没有结束,我只是在客户端中输入命令而没有任何响应

最佳答案

修复

  1. 使用 PrintWriter 向套接字写入数据,使用 BufferedReader 从套接字读取数据。在自动刷新模式下打开PrintWriter下面的语句中的 true 表示这一点。这样您就不必担心刷新流。

    PrintWriter out = new PrintWriter(sock.getOutputStream(),true);
  2. 您的结束语句位于循环内,该循环会关闭流,并且您无法将它们用于下一次迭代,因为您将收到异常。将它们从 while 循环中拉出。
  3. 在开关中使用不必要的大括号来分隔大小写。
  4. 您的连接终止语句应该位于 switch case 之外。只有这样,break 才会对循环而不是开关产生影响。
  5. 我使用 br.ready() 来感测流是否已准备好读取,然后迭代循环。这比你的版本有效。

    while((temp = sIn.nextLine()) != null)
    {
    out.println(temp);
    Thread.currentThread().sleep(1000);
    while(in.ready())System.out.println(in.readLine());
    out.flush();
    if(temp.equals("end")) break;
    }
  6. Thread.currentThread().sleep(1000) 是获取主线程的句柄并将其 hibernate 1000 毫秒,这样我就可以让 br 准备好服务器响应。否则,对于当前请求,br.ready() 将为 false,并且它会在下一次迭代中准备好。我想你知道我想传达什么。

我没有检查案例循环;

服务器

import java.net.*;
import java.io.*;
import java.util.*;

public class chatServer
{
public static void main(String[] args) throws IOException
{
ServerSocket ss = null;
try
{
ss= new ServerSocket(8000);
}
catch(IOException ioe){System.out.println("Can't connect to port.");}

Socket sock = null;
try{sock = ss.accept();}
catch(IOException ioe){System.out.println("Can't connect to client.");}
System.out.println("Connection successful.");

PrintWriter out = null;
Scanner in = null;

try{
out = new PrintWriter(sock.getOutputStream(),true);
in = new Scanner(sock.getInputStream());
}catch(Exception e)
{
System.out.println("Error.");
}

String line;
String lines = "";

while((line = in.nextLine()) != null)
{
switch(line)
{
case "list":
out.println("1. bla | 2. ble | 3. bli");break;
case "group":out.println("Here group will be chosen.");break;
case "header":out.println("Returns header.");break;
case "loop":
FileReader fr = new FileReader("D:\\Eclipse\\TestFiles\\text2.txt");
BufferedReader br = new BufferedReader(fr);
while((lines = br.readLine()) != null)
{
out.println(lines);
}
break;
default:
if(!(line.equals("end")))
{
out.println("Unknown command.Try again.");
break;
}
}
if(line.equals("end"))
{
out.println("Connection over.");
break;
}
}
try{
in.close();
out.close();
sock.close();
ss.close();
}catch(IOException ioe){}

}

}

客户端

import java.net.*;
import java.io.*;
import java.util.*;

public class chatClient
{
public static void main(String[] args) throws IOException, InterruptedException
{
Socket sock = null;
PrintStream out = null;
BufferedReader in = null;
Scanner sIn = null;

try
{
sock = new Socket("localhost",8000);
out = new PrintStream(sock.getOutputStream());
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
sIn = new Scanner(System.in);
}
catch(Exception e)
{
System.out.println("Error connecting to server.");
System.exit(1);
}

System.out.println("Connection successful.");

String temp = "";

while((temp = sIn.nextLine()) != null)
{
out.println(temp);
Thread.currentThread().sleep(1000);
while(in.ready())System.out.println(in.readLine());
out.flush();
if(temp.equals("end")) break;
}

try
{
sock.close();
in.close();
out.close();
}
catch(IOException ioe){}

}

}

关于java - 客户端-服务器程序中的 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com