来自服务器的BufferedReader不起作用

时间:2013-02-25 15:15:28

标签: java bufferedreader

在此代码中,我可以使用在客户端套接字上创建的BufferedReader inClient正确接收请求。 然后我将请求发送到服务器,我看到服务器得到它。 但是,当我尝试从服务器读取回复时(在服务器的套接字上使用BufferedReader inServer),它始终以IOException: Impossible read from server结束。

我指的是块################ 你知道任何可能的原因吗?

import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ProxyMain {

public static void main(String argv[]) {

    int proxyPort = 55554;
    String proxyAddr = "127.0.0.1";
    ServerSocket proxySocket = null;

    try {
        proxySocket = new ServerSocket(proxyPort, 50, InetAddress.getByName("127.0.0.1"));
    }

    catch (Exception e) {
        System.err.println("Impossible to create socket server!");
        System.out.flush();
        System.exit(1);
    }

    System.out.printf("Proxy active on port: %d and on address %s\n", proxyPort, proxySocket.getInetAddress());
    System.out.println();

    while (true) {
        Socket client = null;
        Socket sockServ = null;
        BufferedReader inClient = null;
        PrintWriter outClient = null;
        BufferedReader inServer = null;
        PrintWriter outServer = null;
        String request = new String();
        String tmp = new String();
        String reply = new String();
        String tmpReply = new String();


        try {
            client = proxySocket.accept();
            System.out.println("Connected to: ");
            System.out.println(client.getInetAddress().toString());
            System.out.printf("On port %d\n", client.getPort());
            System.out.println();
            inClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
            outClient = new PrintWriter(client.getOutputStream(), true);

        }

        /*catch (IOException e) {
            System.err.println("Couldn't get I/O for connection accepted");
            System.exit(1);
        }*/

        catch (Exception e) {
            System.out.println("Error occurred!");
            System.exit(1);
        }

        System.out.println("Received request:");

        try{
            for (int i = 0; i<2; i++) {
                tmp = inClient.readLine();
                request = request + tmp;
            }

            inClient.close();
        }
        catch (IOException ioe) {
            System.err.println("Impossible to read mhttp request!");
            System.exit(1);
        }

        System.out.println(request);
        System.out.println();


        try {

            sockServ = new Socket("127.0.0.1", 55555);
            outServer = new PrintWriter(sockServ.getOutputStream(), true);
            inServer = new BufferedReader(new InputStreamReader(sockServ.getInputStream()));

        }

        catch (UnknownHostException e) {
            System.err.println("Don't know about host: 127.0.0.1:55555");
            System.exit(1);
        }

        catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1:55555");
            System.exit(1);
        }

        outServer.println(request);
        outServer.close();


        try {
            #################################################
            while ((tmpReply = inServer.readLine()) != null) {
                System.out.println(tmpReply);
                reply = reply + tmpReply;
            }

            inServer.close();
            sockServ.close();

        }

        catch (IOException ioe) {
            System.err.println("Impossible to read from server!");
            System.exit(1);
        }


        outClient.println(reply);
        outClient.close();

        try {
            client.close();
        }

        catch (IOException ioe) {
            System.err.printf("Impossible to close connection with %s:%d\n", client.getInetAddress().toString(), client.getPort());
        }


    }

}

}

更新: 似乎如果我这样做:     boolean res = inServer.ready(); 它总是返回虚假。 所以服务器还没有准备好发送回复,但这很奇怪...我的C e Python项目立即起作用。为什么java会有所不同?

1 个答案:

答案 0 :(得分:1)

关闭outServer时,关闭底层套接字。如果只是想要关闭输出并保持输入打开,则需要使用Socket.shutdownOutput()。请注意,关闭inClient时会出现同样的问题。