Java:网络中的EOFEException

Java:网络中的EOFEException,java,networking,io,eofexception,Java,Networking,Io,Eofexception,我是网络新手,当我试图运行MyClient.java时,我遇到了一个EOFEException MyServer.java public class MyServer { public static void main(String[] args) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4321); } ca

我是网络新手,当我试图运行MyClient.java时,我遇到了一个EOFEException

MyServer.java

public class MyServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4321);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(true) {
            try {
                Socket socket = serverSocket.accept();
                OutputStream os =socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
                bw.write("Hello networking");

                bw.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class MyClient {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 4321);
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            System.out.println(dis.readUTF());

            dis.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (ConnectException e) {
            System.err.println("Could not connect");
        } catch (UTFDataFormatException e) {
            System.err.println("if the bytes do not represent a valid modified UTF-8 encoding of a string");
        } catch (EOFException e) {
            System.err.println("if this input stream reaches the end before reading all the bytes");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
MyClient.java

public class MyServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4321);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(true) {
            try {
                Socket socket = serverSocket.accept();
                OutputStream os =socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
                bw.write("Hello networking");

                bw.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class MyClient {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 4321);
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            System.out.println(dis.readUTF());

            dis.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (ConnectException e) {
            System.err.println("Could not connect");
        } catch (UTFDataFormatException e) {
            System.err.println("if the bytes do not represent a valid modified UTF-8 encoding of a string");
        } catch (EOFException e) {
            System.err.println("if this input stream reaches the end before reading all the bytes");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
如果此输入流在读取错误的所有字节之前到达末尾,并且错误似乎是由dis.readUTF()调用引起的,则我得到

有人能帮我理解我错过了什么吗?我正在尝试读取服务器连接到客户端时写入的内容,即Hello networking


谢谢。

问题在于您的服务器代码
如果要使用DataInputStream读取套接字,则应使用DataOutputStream.writeUTF(字符串str)写入套接字。

问题在于服务器代码 若要使用DataInputStream读取套接字,则应使用DataOutputStream.writeUTF(字符串str)写入套接字