简单的客户端服务器Java程序不会进入while循环

时间:2017-04-18 17:18:51

标签: java server client

我在完成任务时偶然发现了一个死区。它是一个简单的服务器和客户端程序。 该计划的细节如下;

  1. 创建一个侦听传入连接的服务器类EncryptServer。如果存在连接,请接受它并创建一个线程EncryptServerSession来处理输入和输出流。
  2. 创建一个线程类EncryptServerSession,它接收来自服务器类的输入和输出流并进行处理。
  3. 创建一个连接到服务器类的客户端类EncryptClient,并将输出流从EncryptServerSession传递到EncryptServer
  4. 不知怎的,EncryptClient类的while循环不起作用。我似乎无法进入循环。我的代码有问题吗?提前谢谢。

    加密服务器

    import java.net.*;
    import java.io.*;
    
    public class EncryptServer
    {    
        public EncryptServer() throws IOException
        {
             ServerSocket serverSocket = new ServerSocket(1122);
             System.out.println("Server started."); 
    
             while (true)
             {
                 Socket conSocket = serverSocket.accept();
                 System.out.println("Client connected from " + 
    conSocket.getLocalAddress().getHostName()); 
    
                 Thread session = new 
    EncryptServerSession(conSocket.getInputStream(), 
    conSocket.getOutputStream());
                 session.start();
         }
    }
    public static void main(String[] args)
    {
        try
        {
            EncryptServer server = new EncryptServer();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }                                      
    }
    
    }
    

    EncryptServerSession

     import java.net.*;
        import java.io.*;
    
    public class EncryptServerSession extends Thread
    {
        BufferedReader in;
        Writer out;
    
    public EncryptServerSession(InputStream inStream, OutputStream outStream)
    {
        Reader read = new InputStreamReader(inStream);
        in = new BufferedReader(read);
        out = new OutputStreamWriter(outStream);
    }
    
    public void strEncrypt()
    {
        try
        {
            String message = in.readLine();
            out.write(message);
            out.flush();
        }
        catch (Exception e)
        {
        }
    }
    
    public void run()
    {
        try
        {
            //System.out.println(in.readLine());
            out.write("Please enter the message to be encrypted: ");
            out.flush();
    
            //strEncrypt();
    
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    
    }
    }
    

    EncryptClient

    import java.net.*;
    import java.io.*;
    
    
    public class EncryptClient
    {
    BufferedReader input, userTerm;
    Writer output;
    String line;
    
    public EncryptClient() throws IOException
    {
        Socket clientSocket = new Socket("localhost", 1122);
        Reader read = new InputStreamReader(clientSocket.getInputStream());
        input = new BufferedReader(read);
    
        userTerm = new BufferedReader(new InputStreamReader(System.in));
        output = new OutputStreamWriter(clientSocket.getOutputStream());             
    
        /////////////// Somehow I cannot get into this loop, why? //////////
        while (true) 
        {
            System.out.println("test ");
            System.out.println(input.readLine());
            System.out.println("Enter message to be encrypted: ");
            output.write(userTerm.readLine());        
            output.flush();
        }
        /////////////// Somehow I cannot get into this loop, why? //////////
    }
    
    public static void main(String[] args)
    {
        try
        {
          EncryptClient client = new EncryptClient();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    

    }

1 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试向服务器发送消息,执行某些逻辑,然后将其发送回客户端。上面的代码似乎在我身边运行良好。这就是我所做的:

  • 首先运行EncryptServer。我希望这是你面临的问题。或者你的防火墙不允许你听套接字。
  • EncryptServerSession中,你正在读行,但你不是在写行。要么close流,要么在完成后写一个新行。

    ...
    
    out.write(message);
    out.write("\r\n"); // write new line
    out.flush();
    
    ...
    
    } finally {
        try {
          out.close();
        } catch (IOException e) {
        }
    }
    

    OR

    ...
    
    out.write(message);
    out.write("\r\n"); // write new line
    out.flush();
    
    ...