首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在第一个EOFException之后接收SocketTimeoutException

在第一个EOFException之后接收SocketTimeoutException
EN

Stack Overflow用户
提问于 2011-08-15 19:31:12
回答 2查看 841关注 0票数 1

这是Java中一个简单的TCP Server (echo)和TCP客户端。

如果我评论下一行,该程序正在工作:

代码语言:javascript
复制
st.setSoTimeout(WAITING_FOR_INPUT);

但是如果我使用这个套接字超时方法,那么我将首先得到一个InterruptedIOException (它仍然很好)。但是在下一次迭代中,我将得到一个EOFException (这就是问题所在)。

我不明白,怎么了?请帮帮我!

CSimpleServer.java

代码语言:javascript
复制
import java.io.*;
import java.net.*;

public class CSimpleServer
{
    public static final int WAITING_FOR_INPUT = 10; // 10 ms

    public CServerThread st;

    public class CServerThread extends Thread
    {
        ServerSocket ss;
        Socket st;
        ObjectInputStream in;
        ObjectOutputStream out;

        int sleeping_time;
        int port;

        CServerThread(int port, int sleeping_time)
        {   
            st = null;
            in = null;
            out = null;

            this.port = port;
            this.sleeping_time = sleeping_time;
        }

        public void run()
        {
            try {
                ss = new ServerSocket(port);
            }
            catch (IOException ioe) {
                System.err.println("Create server failed: " + ioe.getMessage());
                return;
            }

            try {
                st = ss.accept();
                st.setSoTimeout(WAITING_FOR_INPUT); 
                out = new ObjectOutputStream(st.getOutputStream());
                out.flush();
                in = new ObjectInputStream(st.getInputStream());
            }
            catch (IOException ioe) {
                System.err.println("Catch connection failed: " + ioe.getMessage());
                return;
            }

            while (true) {
                try {
                    int i_data = in.readInt();
                    if (i_data == 0) {
                        break;
                    }
                    System.out.println("Get: " + i_data);
                    out.writeInt(i_data);
                    out.flush();
                    out.reset();

                    sleep(sleeping_time);
                }
                catch (InterruptedIOException ire) {
                    System.out.println("Read data timeout."); // for debug
                    continue;
                }
                catch (EOFException eof) {
                    System.err.println("Reach end of stream: " + eof.getMessage());
                    break;
                }               
                catch (IOException ioe) {
                    System.err.println("Read data failed: " + ioe.getMessage());
                    break;
                }
                catch (InterruptedException ire) {
                    System.err.println("Something interrupted this thread: " + ire.getMessage());
                }
            }

            try {
                in.close();
                out.close();
                st.close();
                ss.close();
            }
            catch (IOException ioe) {
                System.err.println("Close server failed: " + ioe.getMessage());
                return;
            }
        }
    }

    CSimpleServer()
    {
        st = null;
    }

    public static void main(String[] args)
    {
        CSimpleServer prog = new CSimpleServer();

        prog.st = prog.new CServerThread(3800, 1000);
        prog.st.start();
    }
}

CSimpleClient.java

代码语言:javascript
复制
import java.io.*;
import java.net.*;

public class CSimpleClient
{

    public CClientThread ct;

    public class CClientThread extends Thread
    {
        Socket st;
        ObjectInputStream in;
        ObjectOutputStream out;

        int port;

        CClientThread(int port)
        {   
            st = null;
            in = null;
            out = null;

            this.port = port;
        }

        public void run()
        {
            try {
                st = new Socket(InetAddress.getLocalHost(), port);
                out = new ObjectOutputStream(st.getOutputStream());
                out.flush();
                in = new ObjectInputStream(st.getInputStream());
            }
            catch (UnknownHostException uhe) {
                System.err.println("Unknown server: " + uhe.getMessage());
                return;
            }           
            catch (IOException ioe) {
                System.err.println("Connect to server failed: " + ioe.getMessage());
                return;
            }

            BufferedReader ink = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                try {

                    String s = ink.readLine();
                    int i_data = Integer.valueOf(s);
                    out.writeInt(i_data);
                    out.flush();
                    out.reset();
                    if (i_data == 0) {
                        ink.close();
                        break;
                    }
                    i_data = in.readInt();
                    System.out.println("Echo: " + i_data);
                }
                catch (IOException ioe) {
                    System.err.println("Send/read data failed: " + ioe.getMessage());
                    break;
                }
            }

            try {
                in.close();
                out.close();
                st.close();
            }
            catch (IOException ioe) {
                System.err.println("Close client failed: " + ioe.getMessage());
                return;
            }
        }
    }

    CSimpleClient()
    {
        ct = null;
    }

    public static void main(String[] args)
    {
        CSimpleClient prog = new CSimpleClient();

        prog.ct = prog.new CClientThread(3800);
        prog.ct.start();
    }
}
EN

回答 2

Stack Overflow用户

发布于 2011-08-15 19:36:51

你是否试图增加超时值,10毫秒是一个相当快的网络,取决于上下文.

顺便说一句,我喜欢给常量单位取它们的名字,代码更易读。

你好,Stéphane

票数 0
EN

Stack Overflow用户

发布于 2011-08-15 23:06:03

如果您得到了EOFException,这意味着对等方关闭了连接,没有两种方法。

我同意Snicolas的观点。10毫秒是一个荒谬的短超时。设置为至少5秒。

您还应该在创建ObjectInputStream之后设置超时。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7069744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档