ObjectInputStream阻塞套接字外部ip疯狂

时间:2013-05-18 23:28:27

标签: java sockets connection timeout objectinputstream

当我初始化objectinputstreams时,在我通过objectoutputstreams初始化和刷新之后,我得到了这个:

客户端:

  

java.net.SocketTimeoutException:读取超时       在java.net.SocketInputStream.socketRead0(本机方法)       在java.net.SocketInputStream.read(未知来源)       在java.net.SocketInputStream.read(未知来源)       at java.io.ObjectInputStream $ PeekInputStream.read(Unknown Source)       at java.io.ObjectInputStream $ PeekInputStream.readFully(Unknown Source)       at java.io.ObjectInputStream $ BlockDataInputStream.readShort(Unknown Source)       at java.io.ObjectInputStream.readStreamHeader(Unknown Source)       在java.io.ObjectInputStream。(未知来源)       在Start.refreshChangeLog(Start.java:87)       在开始$ 2.windowOpened(Start.java:234)       在java.awt.AWTEventMulticaster.windowOpened(未知来源)       at java.awt.Window.processWindowEvent(Unknown Source)       在javax.swing.JFrame.processWindowEvent(未知来源)       在java.awt.Window.processEvent(未知来源)       at java.awt.Component.dispatchEventImpl(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Window.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 200(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       在java.awt.EventQueue $ 4.run(未知来源)       在java.awt.EventQueue $ 4.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       在java.awt.EventDispatchThread.run(未知来源)

服务器:

  

java.net.SocketException:连接重置       在java.net.SocketInputStream.read(未知来源)       在java.net.SocketInputStream.read(未知来源)       at java.io.ObjectInputStream $ PeekInputStream.read(Unknown Source)       at java.io.ObjectInputStream $ PeekInputStream.readFully(Unknown Source)       at java.io.ObjectInputStream $ BlockDataInputStream.readShort(Unknown Source)       at java.io.ObjectInputStream.readStreamHeader(Unknown Source)       在java.io.ObjectInputStream。(未知来源)       在客户端。(Client.java:27)       在ThreadAccept.run(ThreadAccept.java:23)

我的客户代码:

public void refreshChangeLog() {
    Socket logSocket = null;
    ObjectInputStream input = null;
    ObjectOutputStream output = null;
    try {
        logSocket = new Socket(InetAddress.getByName(server), 21992);
        logSocket.setSoTimeout(0);
        output = new ObjectOutputStream(logSocket.getOutputStream());
        output.flush();
        input = new ObjectInputStream(logSocket.getInputStream());
        output.writeObject("type:changeLog");
        output.flush();
        Object o = input.readObject();
        while (o instanceof String && !((String) o).equals("done")) {
            String msg = (String) o;
            int index = msg.indexOf("|");
            if (index > -1) {
                changeLogMap.put(msg.substring(0, index), msg.substring(index + 1));
                changeLogList.addItem(msg.substring(0, index));
            }
            o = input.readObject();
        }
    }catch (Exception e) {
        e.printStackTrace();
        loadLocalChangeLog();
    }
    if (changeLogList.getItemCount() > 0) {
        changeLogList.setSelectedIndex(0);
        File changelog = new File(path + "changelog");
        if (!changelog.exists() || !changelog.isDirectory()) {
            changelog.mkdirs();
        }
        for (String key : changeLogMap.keySet()) {
            File changef = new File(changelog, key + ".txt");
            if (!changef.exists()) {
                try {
                    changef.createNewFile();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (changef.exists()) {
                PrintWriter writer;
                try {
                    writer = new PrintWriter(changef);
                    writer.write(changeLogMap.get(key));
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    try {
        if (output != null) {
            output.close();
            output = null;
        }
        if (input != null) {
            input.close();
            input = null;
        }
        if (logSocket != null) {
            logSocket.close();
            logSocket = null;
        }
    }catch (IOException e1) {
        e1.printStackTrace();
    }
}`

我的服务器代码:

socket = new ServerSocket(21992);
        new Client.ThreadUpdateLobby();
        while (Start.frame != null && socket != null && !socket.isClosed()) {
            Socket clientSocket = socket.accept();
            clientSocket.setSoTimeout(0);
            Client client = new Client(clientSocket);
            if (client.isValid) {
                synchronized (clients) {
                    clients.add(client);
                }
            }
            Thread.sleep(50L);
        }`

更多服务器代码:

this.socket = socket;
    try {
        this.socket = socket;
        output = new ObjectOutputStream(socket.getOutputStream());
        output.flush();
        input = new ObjectInputStream(socket.getInputStream());
        listener = new ThreadListen(this, input);
        isValid = true;
    }catch (Exception e) {
        e.printStackTrace();
        try {
            if (output != null) {
                output.close();
                output = null;
            }
            if (input != null) {
                input.close();
                input = null;
            }
            if (socket != null) {
                socket.close();
                this.socket = null;
            }
        }catch (IOException e1) {
            e1.printStackTrace();
        }
        isValid = false;
    }`

我一直不知所措,显然头部没有冲洗?因此输入流只是坐着等待。问题似乎是客户端。此外,这只发生在我使用我的外部网络IP(98.232.202.119),但没有问题,如果我使用“127.0.0.1”或“192.168.2.50”我的端口转发,我在线测试。

1 个答案:

答案 0 :(得分:0)

我知道这个问题已经死了,但是为了谷歌的缘故,为了解决这个问题,请检查客户端的外部IP是否与服务器的ip匹配,如果是,请使用本地地址。是什么导致的?这是你的路由器。在某些型号中,WAN安全性可防止本地计算机像远程计算机一样运行。我没有看到这方面的安全问题,但显然路由器公司会这样做。我试图打电话给我的公司并禁用我的安全保障,以发现它是不可能的。