简体   繁体   English

为什么以下代码会命中IOException?

[英]Why does the following code hits IOException?

Client Class 客户类别

import java.io.PrintWriter;
import java.net.Socket;

public class Client 
{
    public static void main(String[] args) 
    {
        try 
        {
            System.out.println(" Starting Client ");
            Socket socket = new Socket ("localhost",55555);
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
            printWriter.println("Hello from client");
            printWriter.println("Conected, Yes!");
            socket.close();// Changes as suggested by Jack
        } 
        catch (Exception e) 
        {
            System.out.println(e);
        }


    }
}

Server Class 服务器等级

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server 
{
    public static void main(String[] args)
    {
        System.out.println(" inside main ");
        try 
        {
            System.out.println("Starting Server");
            ServerSocket serverSocket= new ServerSocket(55555);
            Socket clientSocket = serverSocket.accept();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String inputLine;
            while ((inputLine= bufferedReader.readLine())!=null) 
            System.out.println("Server Message:"+inputLine);

        } 
        catch (IOException e) 
        {
            System.out.println("IOException "+e);
        }
    }
}

Output:
Starting Server
Server Message:Hello from client
Server Message:Conected, Yes!
IOException java.net.SocketException: Connection reset

Client and Server java files are in the same package. 客户端和服务器Java文件位于同一软件包中。 What would trigger IOException in the following code? 在以下代码中什么会触发IOException? Is it something to do with Eclipse? 与Eclipse有关吗? FYI, I am using Eclipse SDK Version: 4.2.2 Build id: M20130204-1200 仅供参考,我使用的是Eclipse SDK版本:4.2.2内部版本号:M20130204-1200

The problem is that your client opens a Socket , sends some data and then exits the program, thus abruptly closing the connection. 问题是您的客户端打开一个Socket ,发送一些数据,然后退出程序,从而突然关闭了连接。

You should call close() on the Socket from the client side to notify the server that the socket it is going to be closed. 您应该从客户端在Socket上调用close()以通知服务器它将要关闭的套接字。

As @Jack mentioned in his answer you need to close() your socket before the client application exit. 正如@Jack在他的回答中提到的那样,您需要在客户端应用程序退出之前关闭套接字。

Socket is AutoCloseable 套接字是自动关闭的

The safe way of work with closable resource is creation in try block: 使用可关闭资源的安全方法是在try块中创建:

public class Client {
    public static void main(String[] args) {
            System.out.println(" Starting Client ");
            try(Socket socket = new Socket("localhost", 55555)) {
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                printWriter.println("Hello from client");
                printWriter.println("Conected, Yes!");
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

and it will be closed automatically on exit (from try section). 它将在退出时自动关闭(从“尝试”部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM