在客户端 - 服务器程序中循环

时间:2015-01-07 08:54:57

标签: java

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

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循环内部)必须结束时,它不会,我只是在客户端输入命令而没有任何响应

1 个答案:

答案 0 :(得分:1)

<强>修复

  1. 使用PrintWriter来写入套接字,使用BufferedReader来读取套接字。在PrintWriter中打开auto flush mode,在下面的语句中为true表示。通过这种方式,您不必担心冲洗流。

    PrintWriter out = new PrintWriter(sock.getOutputStream(),true);
    
  2. 你的结束语句在循环内,它会关闭流,你不能在下一次迭代中使用它们,因为你会得到一个exception。将它们拉出while循环。
  3. 用于分隔交换机中案例的不必要的大括号。
  4. 您的连接终止声明应该不在切换案例中。只有这样,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),是获取主线程的句柄并将其休眠1000ms,这样我就可以准备好服务器响应了。否则br.ready()对于当前请求将为false,并在下一次迭代中准备就绪。我想你知道我想传达什么。

  7. 我没有检查案例循环;

    服务器

    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){}
    
    }
    

    }