【问题标题】:Port binding on macosmacos 上的端口绑定
【发布时间】:2012-10-03 16:46:58
【问题描述】:

我在java中使用ServerSocket来检查mysql是否正在运行。如果它没有运行,我提示用户请运行你的 mysql 服务器。但是,我的一些用户抱怨虽然 mysql 正在运行,但我们收到了错误消息。它只发生在 macos 中。我发现的是。

Java 代码:

public static boolean isPortFree(int port)
{
    if(port <= 0)
    {
        return false;
    }
    ServerSocket sock = null;
    try
    {
        String bindAddress = System.getProperty("bindaddress", "127.0.0.1");
        sock = new ServerSocket(port,0,InetAddress.getByName(bindAddress));
    }
    catch(Exception ex)
    {
        return false;
    }
    finally
    {
        if (sock != null)
        {
            try
            {
                sock.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    return true;
}

如果我将系统属性 bindaddress 设置为 localhost 它返回 false 同样我设置为 127.0.0.1 返回 true 。 localhost , 127.0.0.1 与它的行为相同。它只出现在 macos 中。我认为这不是my.cnf bindaddress 问题。因为我用

签入了命令提示符
 mysql -uroot -hlocalhost
 mysql -uroot -h127.0.0.1

都是作品。

网络统计

MacOS

 tcp4       0      0  *.3306                 *.*                    LISTEN

其他操作系统

tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN 

tcp4和tcp有区别吗?

知道为什么会这样吗?

【问题讨论】:

  • 我不明白你为什么首先使用ServerSocket。只需尝试打开Socket 到端口 3306。如果成功,则 MySQL 正在运行。如果没有,就没有。

标签: java sockets osx-server


【解决方案1】:

这两个示例有效地绑定到不同的接口:INADDR_ANY 不会阻止绑定到特定的适配器。

如果在 OSX 上 MySQL 的配置与您报告的“Other Os”完全相同,它将按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2011-10-31
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多