live555 出现Unable to determine our source address: This computer has an invalid IP address: 0.0.0.0 的解决方案。

网上的方案我没有使用。对于只有一个网卡的主机来说,它的ip只有一个。可用shell命令获取到这个ip。

官方的live555的live/groupsock/GroupsockHelper.cpp里面的函数ourIPAddress如下:

  1 netAddressBits ourIPAddress(UsageEnvironment& env) {
  2   static netAddressBits ourAddress = 0;
  3   int sock = -1;
  4   struct in_addr testAddr;
  5
  6   if (ReceivingInterfaceAddr != INADDR_ANY) {
  7     // Hack: If we were told to receive on a specific interface address, then
  8     // define this to be our ip address:
  9     ourAddress = ReceivingInterfaceAddr;
 10   }
 11
 12   if (ourAddress == 0) {
 13     // We need to find our source address
 14     struct sockaddr_in fromAddr;
 15     fromAddr.sin_addr.s_addr = 0;
 16
 17     // Get our address by sending a (0-TTL) multicast packet,
 18     // receiving it, and looking at the source address used.
 19     // (This is kinda bogus, but it provides the best guarantee
 20     // that other nodes will think our address is the same as we do.)
 21     do {
 22       loopbackWorks = 0; // until we learn otherwise
 23
 24       testAddr.s_addr = our_inet_addr("228.67.43.91"); // arbitrary
 25       Port testPort(15947); // ditto
 26
 27       sock = setupDatagramSocket(env, testPort);
 28       if (sock < 0) break;
 29
 30       if (!socketJoinGroup(env, sock, testAddr.s_addr)) break;
 31
 32       unsigned char testString[] = "hostIdTest";
 33       unsigned testStringLength = sizeof testString;
 34
 35       if (!writeSocket(env, sock, testAddr, testPort, 0,
 36                testString, testStringLength)) break;
 37
 38       // Block until the socket is readable (with a 5-second timeout):
 39       fd_set rd_set;
 40       FD_ZERO(&rd_set);
 41       FD_SET((unsigned)sock, &rd_set);
 42       const unsigned numFds = sock+1;
 43       struct timeval timeout;
 44       timeout.tv_sec = 5;
 45       timeout.tv_usec = 0;
 46       int result = select(numFds, &rd_set, NULL, NULL, &timeout);
 47       if (result <= 0) break;
 48
 49       unsigned char readBuffer[20];
 50       int bytesRead = readSocket(env, sock,
 51                  readBuffer, sizeof readBuffer,
 52                  fromAddr);
 53       if (bytesRead != (int)testStringLength
 54       || strncmp((char*)readBuffer, (char*)testString, testStringLength) != 0) {
 55     break;
 56       }
 57
 58       // We use this packet‘s source address, if it‘s good:
 59       loopbackWorks = !badAddressForUs(fromAddr.sin_addr.s_addr);
 60     } while (0);
 61
 62     if (sock >= 0) {
 63       socketLeaveGroup(env, sock, testAddr.s_addr);
 64       closeSocket(sock);
 65     }
 66
 67     if (!loopbackWorks) do {
 68       // We couldn‘t find our address using multicast loopback,
 69       // so try instead to look it up directly - by first getting our host name, and then resolving this host name
 70       char hostname[100];
 71       hostname[0] = ‘\0‘;
 72       int result = gethostname(hostname, sizeof hostname);
 73       if (result != 0 || hostname[0] == ‘\0‘) {
 74     env.setResultErrMsg("initial gethostname() failed");
 75     break;
 76       }
 77
 78       // Try to resolve "hostname" to an IP address:
 79       NetAddressList addresses(hostname);
 80       NetAddressList::Iterator iter(addresses);
 81       NetAddress const* address;
 82
 83       // Take the first address that‘s not bad:
 84       netAddressBits addr = 0;
 85       while ((address = iter.nextAddress()) != NULL) {
 86     netAddressBits a = *(netAddressBits*)(address->data());
 87     if (!badAddressForUs(a)) {
 88       addr = a;
 89       break;
 90     }
 91       }
 92
 93       // Assign the address that we found to "fromAddr" (as if the ‘loopback‘ method had worked), to simplify the code below:
 94       fromAddr.sin_addr.s_addr = addr;
 95     } while (0);
 96
 97     // Make sure we have a good address:
 98     netAddressBits from = fromAddr.sin_addr.s_addr;
 99     if (badAddressForUs(from)) {
100       char tmp[100];
101       sprintf(tmp, "This computer has an invalid IP address: %s", AddressString(from).val());
102       env.setResultMsg(tmp);
103       from = 0;
104     }
105
106     ourAddress = from;
107
108     // Use our newly-discovered IP address, and the current time,
109     // to initialize the random number generator‘s seed:
110     struct timeval timeNow;
111     gettimeofday(&timeNow, NULL);
112     unsigned seed = ourAddress^timeNow.tv_sec^timeNow.tv_usec;
113     our_srandom(seed);
114   }
115   return ourAddress;
116 }

这里通过发送一个udp包,来获取到主机ip地址;如果获取ip地址失败,则用gethostname的方法获取ip地址。

但是gethostname的方法也有可能失败,我在虚拟机上测试,发现gethostname的方法获取到ip地址都是"0.0.0.0"。所以我们要添加多一个方法来获取到正确的ip地址。

我们知道,如下的shell命令是可以获取到主机ip地址的:

ifconfig eth0|grep ‘inet addr‘|awk -F ":" ‘{print $2}‘|awk ‘{print $1}‘

在如下代码位置的上边,添加一个获取ip地址的代码。

    // Make sure we have a good address:
    netAddressBits from = fromAddr.sin_addr.s_addr;

新的获取ip地址的代码:

 1     if (badAddressForUs(fromAddr.sin_addr.s_addr))
 2     {
 3         #define MY_IP_BUF_LEN 32
 4         char MyIpBuf[ MY_IP_BUF_LEN ]={0};
 5         FILE *fpRead;
 6         //eth0:网口名称,实际主机的网口名称可能不是这个,请使用ifconfig命令查看。
 7         char* command=(char*)"ifconfig eth0|grep ‘inet addr‘|awk -F \":\" ‘{print $2}‘|awk ‘{print $1}‘";
 8         char* renewCh;
 9
10         fpRead = popen(command, "r");
11         fgets(MyIpBuf, MY_IP_BUF_LEN, fpRead);
12         if(fpRead != NULL)
13             pclose(fpRead);
14
15         renewCh=strstr(MyIpBuf,"\r");
16         if(renewCh)
17             *renewCh=‘\0‘;
18         renewCh=strstr(MyIpBuf,"\n");
19         if(renewCh)
20             *renewCh=‘\0‘;
21
22         fromAddr.sin_addr.s_addr=our_inet_addr(MyIpBuf);
23         printf("----------fix for geting local ip fails, local ip=%s.\n", MyIpBuf);
24     }

重新编译,centos64位,运行:

完。

时间: 2024-11-02 18:44:22

live555 出现Unable to determine our source address: This computer has an invalid IP address: 0.0.0.0 的解决方案。的相关文章

PRCT-1302 the OCR has an invalid ip address

PRCT-1302 the OCR has an invalid ip address 1. 报错信息 an internal error occurred within cluster verification framework unable to obtain network interface list from oracle clusterware PRCT-1302 the OCR has an invalid ip address format 11.2.0.3OracleRAC软

Java Regex match IP address

Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPAddressValidator{ private Pattern pattern; private Matcher ma

If application data needs to be sent to IP address xx.xx.xx.xx, how does it work in underneath network?

This is to illustrate the communication between two separate machines which don't have a direct physical connection. Application data is generated and passed to layer 3 (network layer) which wrapps up the data with the destination IP address. Then ha

Unable to determine memory usage

Nagios报错如下 ***** Nagios *****Notification Type: PROBLEM Service: MEM_USEHost: s3Address: 10.10.16.103State: UNKNOWN  Date/Time: Tue Jun 28 17:41:42 CST 2016 Additional Info: Unable to determine memory usage. 解决过程: 1.根据Service:MEM_USE在文件/etc/nagios/ob

创建Hbase Hive外部表报错: Unable to determine ZooKeeper ensemble

创建HBase的Hive外部表 1: create external table ttt(rowkey string,info map<string,string>)STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:") TBLPROPERTIES ("hb

iOS Interface builder was unable to determine the type of xxx.xib / xxx.storyboard

上午开工程出现, "Interface builder was unable to determine the type of xxx.xib" 错误svn上报冲突(conflict),xib文件爆红 郁闷 百度 google stack 没有找到答案 于是只能,喝口奶自己研究xib的真身是xml文件, 于是右键 OpenAs Source Code看源代码打开便看到嫌疑犯 根标签下有冲突,于是,将"==="删除.删除后编译 又爆出 "Extra cont

hbase异常:java.io.IOException: Unable to determine ZooKeeper ensemble

项目中用到hbase,有时候可能会报一些异常,比如java.io.IOException: Unable to determine ZooKeeper ensemble 等等,当出现这个问题时,某某说是项目中用到线程池的问题导致的,但查看异常之后,并非跟啥线程池有关系,异常信息如下: java.io.IOException: Unable to determine ZooKeeper ensemble at org.apache.hadoop.hbase.zookeeper.ZKUtil.con

CocoaPods解决Unable to add a source with url

[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master-1`.  You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`. 是由于我装了多个Xcode导致路径变了, 解决:hwjdeMacBook-Pro:IJiaXiao_JiaoLian hwj$ sudo xcode-s

ValueError: Unable to determine SOCKS version from socks://127.0.0.1:1080/

使用ss之后输入conda指令出现错误:“ValueError: Unable to determine SOCKS version from socks://127.0.0.1:1080/”. 解决方法: 在终端中输入 unset all_proxy && unset ALL_PROXY export all_proxy="socks5://127.0.0.1:1080" 这只能在当前终端的使用中有效,重新打开的终端中仍然会有错误. 原文地址:https://www.