4

在我ServerSocket监听传入连接的类中,以下是代码:

while(isRunning)
{
    try
    {
        Socket s = mysocketserver.accept();
        acknowledgeClient(s);
        new ClientHandler(s).start(); //Start new thread to serve the client, and get back to accept new connections.
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}

以下是acknowledgeClient(Socket s)代码。

ObjectInputStream in = new ObjectInputStream(s.getInputStream);
ObjectOutputStream out = new ObjectOutputStream(s.getOutStream);
String msg = in.readObject().toString();
System.out.println(msg+" is Connected"); //Show who's connected
out.writeObject("success"); //Respond with success.
in.close();
out.close();

run()方法ClientHandler

try
{
    in = new ObjectInputStream(client.getInputStream());
    out = new ObjectOutputstream(client.getOutputStream());
    String msg = "";
    while(!msg.equalsIgnoreCase("bye"))
    {
        msg = in.readObject().toString();
        System.out.println("Client Says - "+msg);
        out.writeObject("success");
    }
    in.close();
    out.close();
}
catch(Exception ex)
{
    ex.printStackTrace();
}

以下是客户端程序如何与此 Echo Server 通信的方式。

try
{
    int count = 10;
    client = new Socket("localhost",8666);
    in = new ObjectInputStream(client.getInputStream());
    out = new ObjectOutputstream(client.getOutputStream());
    out.writeObject("Foo");
    System.out.println("Connection Status : "+in.readObject().toString());
    while(count>0)
    {
        out.writeObject("Hello!");
        String resp = in.readObject().toString(); //Getting EOFException here.
        System.out.println("Sent with :"+resp);
        count--;
        Thread.sleep(1000);
    }
    out.close();
    in.close();
    client.close();
}
catch(Exception ex)
{
    ex.printStackTrace();
}

您可能已经注意到,在连接后确认客户端后,我关闭读/写流,然后从为客户端提供服务的新线程中,我再次打开流,并从服务器读/写已连接的套接字已启动,但是一旦我尝试读取服务器对Hello!客户端发送的响应,它就会崩溃EOFException而不是success.

我知道 EOF 发生的原因,但不知道为什么会在这里发生,我没有尝试读取其流中没有任何内容的套接字(它应该success由服务器编写)。

Hello!客户端在服务器端打印并写入success响应之前尝试读取套接字是否为时过早?

PS:我知道通过放置这么多代码来提出问题并不是一个好方法,我们希望在这里得到问题的答案并理解它,而不是让别人解决我们的问题并逃脱。所以,我提供了这么多代码来展示问题的各个方面。

4

4 回答 4

5

我研究了 ObjectInputStream 的源代码,似乎对原始输入流的引用s.getInputStream()存储在 ObjectInputStream 中。

当您关闭 ObjectInputStream 时,s.getInputStream()也会关闭。

输入流一旦关闭,就无法再次打开。因此,您会得到一个 EOFException,这表明您处于流的末尾(因为无法再次打开流)。

你应该做这样的事情来承认客户。

在 ClientHandler 的 run() 方法中:

try {
    // acknowledge client
    ObjectInputStream in = new ObjectInputStream(s.getInputStream());
    ObjectOutputStream out = new ObjectOutputStream(s.getOutStream());
    String msg = in.readObject().toString();
    System.out.println(msg+" is Connected"); //Show who's connected
    out.writeObject("success"); //Respond with success.
    // end acknowledge client

    String msg = "";
    while(!msg.equalsIgnoreCase("bye"))
    {
        msg = in.readObject().toString();
        System.out.println("Client Says - "+msg);
        out.writeObject("success");
    }
    in.close();
    out.close();
}
catch(Exception ex)
{
    ex.printStackTrace();
}

如果要在单独的方法中隔离确认代码,只需确保在不关闭流的情况下保持对同一 ObjectInputStream 的正确引用,然后传递引用。

于 2012-06-09T16:59:02.943 回答
1

我再次打开流,并从服务器开始从连接的套接字读取/写入,

流关闭后,您将无法再次打开它。实际上,您根本不能以这种方式在同一个流上使用两个 Object 流。

相反,您应该为输入和输出创建一次且仅一次的对象流,并且在完成之前不要关闭它。

于 2012-06-09T16:53:33.663 回答
0

看看这个程序,我写它是为了理解多个客户端和服务器的通信,你的问题在这个程序中得到了回答。

客户端代码

public class ClientWala {

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

        Boolean b = true;
    Socket s = new Socket("127.0.0.1", 4444);

    System.out.println("connected: "+s.isConnected());


    OutputStream output = s.getOutputStream();
    PrintWriter pw = new PrintWriter(output,true);

    // to write data to server
    while(b){

        if (!b){

             System.exit(0);
        }

        else {
            pw.write(new Scanner(System.in).nextLine());
        }
    }


    // to read data from server
    InputStream input   = s.getInputStream();
    InputStreamReader isr = new InputStreamReader(input);
    BufferedReader br = new BufferedReader(isr);
    String data = null;

    while ((data = br.readLine())!=null){

        // Print it using sysout, or do whatever you want with the incoming data from server

    }




    }
}

服务器端代码

public class ServerTest {

    ServerSocket s;

    public void go() {

        try {
            s = new ServerSocket(44457);

            while (true) {

                Socket incoming = s.accept();
                Thread t = new Thread(new MyCon(incoming));
                t.start();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    class MyCon implements Runnable {

        Socket incoming;

        public MyCon(Socket incoming) {

            this.incoming = incoming;
        }

        @Override
        public void run() {

            try {
                PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                        true);
                InputStreamReader isr = new InputStreamReader(
                        incoming.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String inp = null;

                boolean isDone = true;

                System.out.println("TYPE : BYE");
                System.out.println();
                while (isDone && ((inp = br.readLine()) != null)) {

                    System.out.println(inp);
                    if (inp.trim().equals("BYE")) {
                        System.out
                                .println("THANKS FOR CONNECTING...Bye for now");
                        isDone = false;
                        s.close();
                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                try {
                    s.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {

        new ServerTest().go();

    }

}
于 2012-06-09T17:05:27.343 回答
0

关闭套接字流周围的任何输入流或输出流或读取器或写入器会关闭套接字,并暗示其他流、读取器和写入器。

在套接字的生命周期内使用相同的流、读取器和写入器。

于 2012-06-09T22:39:35.343 回答