客户端关闭连接后,Java TCPSocket中断

时间:2015-02-07 22:34:29

标签: java tcp

我是编程新手,特别是对socket编程。我试图弄清楚通信是如何工作的(不是在我的所有书中),但是立即运行了下载的SimpleEchoServer示例的第一个问题。 Communicationflow有效,但当Clientsocket关闭连接而不发送特定字符串时,我的serversocket会崩溃。你能说出我做错了什么吗?

这是服务器端:

import java.net.*; 
import java.io.*; 
import java.net.InetAddress;

public class EchoServer { 

public static void main(String[] args) throws IOException { 

    ServerSocket serverSocket = null;
    Integer port = new Integer(args[0]);

    try { 

        serverSocket = new ServerSocket(port); 

        } 

    catch (IOException e) { 

            System.err.println("Could not listen on port: "+port); 
            System.exit(1); 

        } 

    Socket clientSocket = null; 
    System.out.println ("Waiting for connection on port "+port+" ...");

    try { 

        clientSocket = serverSocket.accept(); 

        }

    catch (IOException e) { 

            System.err.println("Accept failed."); 
            System.exit(1);

        } 

    System.out.println ("Connection successful");
    System.out.println ("Waiting for input.....");

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8")); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) { 

        System.out.println ("received: " + inputLine); 
        out.println(inputLine); 

        if (inputLine.equals("Bye.")) 
        break; 

    } 

    out.close(); 
    in.close(); 
    clientSocket.close(); 
    serverSocket.close(); 

   } 

}

那是客户端

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

public class EchoClient {

    public static void main(String[] args) throws IOException {

    String serverHostname = new String (args[0]);
    Integer port = new Integer(args[1]);

    if (args.length > 0)

        serverHostname = args[0];
        System.out.println ("Attemping to connect to host " + serverHostname + " on port 10007.");

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {

            echoSocket = new Socket(serverHostname, port);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream(), "UTF-8"));

            } catch (UnknownHostException e) {

                System.err.println("Don't know about host: " + serverHostname);
                System.exit(1);

            } catch (IOException e) {

                System.err.println("Couldn't get I/O for " + "the connection to: " + serverHostname);
                System.exit(1);

            }

       BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
       String userInput;

     System.out.print ("input: ");

     while ((userInput = stdIn.readLine()) != null) {

           out.println(userInput);
           System.out.println("echo: " + in.readLine());
         System.out.print ("input: ");

       }

       out.close();
       in.close();
       stdIn.close();
       echoSocket.close();

    }

}

1 个答案:

答案 0 :(得分:1)

不是ServerSocket破坏了。

EchoServer从循环while ((inputLine = in.readLine()) != null)中的客户端套接字的InputStream中读取。套接字连接终止时,尝试读取将引发异常。由于循环不在try-catch-block中,因此异常会终止服务器进程。

为防止这种情况,您需要处理异常。

try {
    while ((inputLine = in.readLine()) != null) { 

        System.out.println ("received: " + inputLine); 
        out.println(inputLine); 

        if (inputLine.equals("Bye.")) 
            break; 

    } 
} catch (IOException ex) {
    System.out.println("Connection error... terminating.");
}