Java Sockets,java.net.SocketException:连接重置,因为BufferedWriter / Reader?

时间:2017-04-24 18:02:10

标签: java sockets bufferedwriter

这只是一个非常简单的服务器/客户端应用程序,我用Java编写,目的只使用BufferedReader和BufferedWriter。 当我启动服务器时,我没有得到任何错误。

但是当客户端尝试向服务器发送消息时:

java.net.SocketException:连接重置

出现。我不知道为什么会发生这种情况,但我认为可能需要对BufferedWriter做些什么。 我在互联网上搜索,但我没有找到任何特定的有用的东西。

Server.java

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

public class Server {
    public Server(int port) {
        try {
            ServerSocket seso = new ServerSocket(port);
            System.out.println("Server started");
            System.out.println("Server listening on port " + port);

            Socket client1 = seso.accept();

            BufferedReader reader = new BufferedReader(new InputStreamReader(client1.getInputStream(), "UTF-8"));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client1.getOutputStream(), "UTF-8"));

            String s = null;

            while((s = reader.readLine()) != null) {
                System.out.println("Client: " + s);
            }

            writer.close();
            reader.close();
        } catch (Exception e) {e.printStackTrace();}
    }
    public static void main(String args[]) {
        new Server(4444);
    }
}

Client.java

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

public class Client {
    public Client(String serverip) {
        try {
            Socket client = new Socket(serverip, 4444);
            System.out.println("Client connected with: " + serverip);

            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "UTF-8"));

            writer.write("Hello Server");
            writer.flush();

        }catch(Exception e){e.printStackTrace();}
    }
    public static void main(String args[]) {
        new Client("localhost");
    }
}

当我运行服务器后跟客户端我从控制台得到这个:

Server started
Server listening on port 4444

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Server.<init>(Server.java:24)
at Server.main(Server.java:33)
BUILD SUCCESSFUL (total time: 9 seconds)

如果我添加:

writer.newLine();

writer.write("Hello Server");

Client.java 中我仍然得到:

Server started
Server listening on port 4444
Client: Hello Server

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Server.<init>(Server.java:24)
at Server.main(Server.java:33)
BUILD SUCCESSFUL (total time: 2 seconds)

0 个答案:

没有答案