【问题标题】:Socket program for Server using System IP Address使用系统 IP 地址的服务器套接字程序
【发布时间】:2011-09-27 21:36:58
【问题描述】:

我正在尝试使用 ServerSocket 在 PC 中运行服务器应用程序,为此我尝试获取系统的 IP 地址来启动服务器并等待客户端连接,因为我已经写过了,

InetAddress inetAddress = InetAddress.getLocalHost();
ServerSocket serverSocket;
if (serverSocket == null)
serverSocket = new ServerSocket(1000, 0, inetAddress);
Socket socket=serverSocket.accept();

它在 Window 操作系统中正常工作,当我在 Unix 操作系统中尝试此应用程序时,它对我不起作用,我尝试使用打印 IP 地址,

System.out.println(inetAddress.getHostAddress);

在 Windows 操作系统中打印正确的 IP 地址,但在 Unix 操作系统中,我得到的是

127.0.0.1

所以服务器不工作,我没有在Mac OS中尝试过,所以有没有办法在任何操作系统中使用系统的默认IP地址启动服务器。

谢谢。

【问题讨论】:

标签: java unix


【解决方案1】:

这似乎是一个很常见的问题。有关一些可能的解决方案,请参阅以下链接: java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037

具体来说,sun bug 链接中的变通和评估说明了该函数在 linux 上的工作原理以及如何确保正确设置您的 linux 机器以在从 java 查询时返回正确的 ip。

【讨论】:

    【解决方案2】:

    InetAddress localhost = InetAddress.getLocalHost(); // this code assumes IPv4 is used byte[] ip = localhost.getAddress(); for (int i = 1; i <= 254; i++) { ip[3] = (byte)i; InetAddress address = InetAddress.getByAddress(ip); if (address.isReachable(1000)) { // machine is turned on and can be pinged } else if (!address.getHostAddress().equals(address.getHostName())) { // machine is known in a DNS lookup } else { // the host address and host name are equal, meaning the host name could not be resolved } }

    【讨论】:

      【解决方案3】:

      只需为 InetAddress 传递 null。另请注意,您指定的积压可能会被系统向上或向下调整。如果您没有充分的理由指定它,请使用 new ServerSocket(0)。

      【讨论】:

        【解决方案4】:

        试试这个例子:

        import java.net.*;

        导入 java.util.*;

        导入 java.util.regex.Pattern;

        公共类 GetPublicHostname { 公共静态 void main(String[] args) 抛出 Throwable { System.out.println("IP:" + tellMyIP()); }

        公共静态字符串tellMyIP()
        {
            网络接口 iface = null;
            字符串 ethr;
            字符串 myip = "";
            字符串正则表达式 = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
            尝试
            {
                for(枚举 ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();)
                {
                    iface = (NetworkInterface)ifaces.nextElement();
                    ethr = iface.getDisplayName();
        
                    if (Pattern.matches("eth[0-9]", ethr))
                    {
                        System.out.println("接口:" + ethr);
                        InetAddress ia = null;
                        for(枚举 ips = iface.getInetAddresses();ips.hasMoreElements();)
                        {
                            ia = (InetAddress)ips.nextElement();
                            if (Pattern.matches(regex, ia.getCanonicalHostName()))
                            {
                                myip = ia.getCanonicalHostName();
                                返回myip;
                            }
                        }
                    }
                }
            }
            捕捉 (SocketException e){}
            返回myip;
        } }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-25
          相关资源
          最近更新 更多