本文介绍了地址重用不适用于新的Java Runtime Environment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码检查地址可重用性: -

I am using the following Code for Checking Address Resusability:-

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class CheckBind {

public static void main(String[] args) {

    Thread serverThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try
            {
                ServerSocket server = new ServerSocket();
                server.setReuseAddress(true);
                server.bind(new InetSocketAddress("127.0.0.1", 2000));
                System.out.println("Server Listen: "+server.getLocalSocketAddress());

                while(true)
                {
                    Socket client = server.accept();
                    System.out.println(""+client.getRemoteSocketAddress());
                    System.out.println(""+client.getLocalSocketAddress());
                }
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

        }
    });

    serverThread.start();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while(true)
    {
        Socket client = new Socket();
        try 
        {
            client.setReuseAddress(true);
            client.bind(new InetSocketAddress("127.0.0.1", 2000));
            client.connect(new InetSocketAddress("127.0.0.1",4000));
            System.out.println("Client Connect: "+client.getRemoteSocketAddress());
            break;
        } 
        catch (IOException e) 
        {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }






}

}

这在Windows 7上工作正常,64位
我使用的是JRE 7U5 [1.7 Update 5] 32Bit版。
[考虑到服务器已在127.0.0.1:4000运行]

This worked fine on Windows 7, 64bit And I used JRE 7U5 [1.7 Update 5] 32Bit Version.[Considering A Server is already Running on 127.0.0.1:4000]

但是当我尝试使用更新版本的JRE时,就像我在JRE上查看一样7U60 32位和JRE 7U72 64位它提供了JVM_Bind异常。
这基本上否定了使用setReuseAddress(true)OPTION的整个目的。

But When I try the same with newer versions of JRE , like I checked on JRE 7U60 32bit and JRE 7U72 64bit it gives a JVM_Bind Exception.Which basically negates the whole purpose of using setReuseAddress(true) OPTION.

请帮助解决这个问题。

谢谢&问候

推荐答案

的javadocs setReuseAddress 说:

在使用bind(SocketAddress)绑定套接字之前启用SO_REUSEADDR允许套接字绑定,即使先前的连接处于超时状态。

Enabling SO_REUSEADDR prior to binding the socket using bind(SocketAddress) allows the socket to be bound even though a previous connection is in a timeout state.

但是你所做的与这个用例不匹配。当其他一些套接字关闭时,您不会尝试绑定。你正在尝试绑定而另一个套接字是打开的。

But what you are doing does not match this use-case. You are not attempting to bind while some other socket is closing. You are actually trying to bind while another socket is open.

令我感到困惑的是为什么你的测试实际上适用于旧的JRE 。它可能是一个JVM错误......

What puzzles me is why your test actually worked on older JREs. It might be a JVM bug ...

这篇关于地址重用不适用于新的Java Runtime Environment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:05