在服务器建立连接之前,如何让客户端保持运行状态?

时间:2016-03-23 06:22:23

标签: java sockets server client

客户端:

public class MyClient {
  public static void main(String[] args) throws  IOException, SocketException,     ConnectException {
    ServerSocket serverSocket = null;
    Socket clientSocket = null;
    BufferedReader in = null;

    System.out.println("Welcome to the Daytime client.");
    try{
    clientSocket = new Socket("localhost", 4321);
         while(true){
           in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
           String s = in.readLine();
           System.out.println("Here is the timestamp received from the server: "+s); 
           System.out.println("The program terminated with no error and no exception");
         in.close();     
         clientSocket.close(); 
         clientSocket.close();
        }
        }catch (ConnectException e){
         System.exit(0); 
        }catch (SocketException f){
         System.exit(0); 
        }catch (IOException e) {
        System.out.println("Error: " + e); 
        System.exit(0); 
        }     
   }    
}

服务器:

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

public class MyServer {
  public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    Socket clientSocket = null;
    PrintWriter out = null;

    System.out.println("Daytime server ready."); 

    try {
      serverSocket = new ServerSocket(4321);
       while(true){
          clientSocket = serverSocket.accept(); 
          System.out.println("Request received.");
           out = new PrintWriter(clientSocket.getOutputStream(), true);
           Date timestamp = new Date ();
          out.println(timestamp.toString());
        }
      } catch (IOException e) {
        System.out.println("Error: " + e);
        System.exit(0);
      }
      out.close();
      clientSocket.close();
      serverSocket.close();
      System.out.println("3"); 

    }
  }

1 个答案:

答案 0 :(得分:0)

很难说出你的问题意味着什么,但你的代码是无稽之谈:

public class MyClient {
  public static void main(String[] args) throws  IOException, SocketException,     ConnectException {
    ServerSocket serverSocket = null;
    Socket clientSocket = null;
    BufferedReader in = null;

    System.out.println("Welcome to the Daytime client.");
    try{
    clientSocket = new Socket("localhost", 4321);
         while(true){
           in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
           String s = in.readLine();

此时您需要if (s == null) break;

           System.out.println("Here is the timestamp received from the server: "+s); 
           System.out.println("The program terminated with no error and no exception");

这不是真的。该计划根本没有终止。它位于while (true)循环的中间。删除循环或修复消息。

         in.close();     
         clientSocket.close(); 
         clientSocket.close();

您不需要关闭套接字两次。实际上,您根本不需要关闭它,因为您已关闭其输入流。

        }
        }catch (ConnectException e){
         System.exit(0); 

永远不要忽略异常。打印出来。

        }catch (SocketException f){

永远不要忽略异常。打印出来。

         System.exit(0); 
        }catch (IOException e) {
        System.out.println("Error: " + e); 
        System.exit(0); 
        }     
   }    
}

public class MyServer {
  public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    Socket clientSocket = null;
    PrintWriter out = null;

这里没有理由宣布这些变量。

    System.out.println("Daytime server ready."); 

这不是真的。在您创建ServerSocket之前,它还没有准备就绪。不要写错误信息。

    try {
      serverSocket = new ServerSocket(4321);

您可以在此声明ServerSocket

       while(true){
          clientSocket = serverSocket.accept(); 

您可以在此声明Socket

          System.out.println("Request received.");
           out = new PrintWriter(clientSocket.getOutputStream(), true);
           Date timestamp = new Date ();
          out.println(timestamp.toString());

此时您需要out.close()

        }
      } catch (IOException e) {
        System.out.println("Error: " + e);
        System.exit(0);
      }
      out.close();

out此时可以为空。

      clientSocket.close();

这是多余的,因为您已关闭其输出流。

      serverSocket.close();
      System.out.println("3");     
    }
  }

我建议你修复所有这些并重新测试。