0

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应该不同?

4

2 回答 2

1

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

于 2013-02-25T15:24:35.627 回答
0

这行得通,也许你可以从中得到一些想法......

ChatServer - 向所有连接的客户端广播

在一个命令提示符中:java ChartServer
在另一个命令提示符中:(java ChatClient localhost 或服务器运行位置的 IP 地址)
另一个:(java ChatClient localhost 或服务器运行位置的 IP 地址)

开始在客户端窗口中聊天。

像这样的服务器...

// xagyg wrote this, but you can copy it
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {

    public static List list = new ArrayList();

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

        ServerSocket svr = new ServerSocket(4444);

        System.out.println("Chat Server started!");

        while (true) {
            try {
                Socket s = svr.accept();
                synchronized(list) {
                   list.add(s);              
                }                                  
                new Handler(s, list).start();
            }
            catch (IOException e) {
                // print out the error, but continue!
                System.err.println(e);
            }
        }
    }
}

class Handler extends Thread {

    private Socket s;
    private String ipaddress;
    private List list;

    Handler (Socket s, List list) throws Exception {
      this.s = s;
      ipaddress = s.getInetAddress().toString();
      this.list = list;
    }

    public void run () {

      try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String message;
        //MyDialog x = (MyDialog)map.get(ipaddress.substring(1));
        while ((message = reader.readLine()) != null) {
            if (message.equals("quit")) {
                synchronized(list) {
                    list.remove(s);
                }
                break;
            }
            synchronized(list) {
                for (Object object: list) {
                    Socket socket = (Socket)object;
                    if (socket==s) continue;
                    PrintWriter writer = new PrintWriter(socket.getOutputStream());
                    writer.println(ipaddress + ": " + message);
                    writer.flush();
                }
            }
        }
        try { reader.close(); } catch (Exception e) {}
      }
      catch (Exception e) {
        System.err.println(e);
      }
    }
}

这样的客户...

// xagyg wrote this, but you can copy it
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatClient {


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

        Socket s = new Socket(args[0], 4444);
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String message;
        new SocketReader(in).start();   
        while ((message = reader.readLine())!=null) {
            out.println(message);
            out.flush();
            if (message.equals("quit")) break;
        }
        in.close();
        out.close();
    }        
}

class SocketReader extends Thread {

    BufferedReader in;

    public SocketReader(BufferedReader in) {
        this.in = in;
    }

    public void run() {   
        String message;
        try {
            while ((message = in.readLine())!=null) {
                System.out.println(message);
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }        
    }
}
于 2013-02-25T15:38:01.430 回答