JNI中socket()崩溃出错,错误代码13,用errno解析结果

安卓JNI中用socket崩溃出错,错误代码13,用errno解析结果

 

安卓jni中写一个socket连接但是一直创建不成功返回-1,然后用打印出错句柄,用<errno.h>头文件,在代码中加入errno

 
  1. if(UDP_DataSocket = socket(AF_INET, SOCK_DGRAM, 0) == -1) //create data packag socket

  2. {

  3. LOGD("socket创建出错!", errno);

  4. LOGI("UDP_DataSocket is %d:%d\n", UDP_DataSocket, errno);

  5. exit(1);

  6. }

  7. LOGD("UDP_DataSocket IS SUCCESSED = %d", UDP_DataSocket);

然后运行之后发现errno的值为13,errno是记录系统最后一次的错误代码,当api函数发生异常时候,一般会将errno一个整数值,然后查一下这个整数值的含义就好了。

然后发现是安卓在创建socket的时候需要添加网路权限,13代表是权限拒绝,应该是AndroidManifest.xml权限不够,所以给socket加上一个网络权限。

 
  1. <uses-permission android:name="android.permission.INTERNET" />

  2. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

  3. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  4.  
  5. <application

  6. android:allowBackup="true"

  7. android:icon="@mipmap/ic_launcher"

  8. android:label="@string/app_name"

  9. android:roundIcon="@mipmap/ic_launcher_round"

  10. android:supportsRtl="true"

  11. android:theme="@style/AppTheme">

  12. <activity android:name=".MainActivity">

  13. <intent-filter>

  14. <action android:name="android.intent.action.MAIN" />

  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />

  17. </intent-filter>

  18. </activity>

  19. </application>

在application的上方加入那三行代码,就是给安卓添加网络权限。然后再重新运行发现创建socket已经成功。所以errno这个变量还是很有用的,可以把这些数值添加进去宏,如果有不知道的错误,在相关地方加入errno。

 
  1.  
  2. #define EPERM 1 /* Operation not permitted */

  3. #define ENOENT 2 /* No such file or directory */

  4. #define ESRCH 3 /* No such process */

  5. #define EINTR 4 /* Interrupted system call */

  6. #define EIO 5 /* I/O error */

  7. #define ENXIO 6 /* No such device or address */

  8. #define E2BIG 7 /* Argument list too long */

  9. #define ENOEXEC 8 /* Exec formats error */

  10. #define EBADF 9 /* Bad file number */

  11. #define ECHILD 10 /* No child processes */

  12. #define EAGAIN 11 /* Try again */

  13. #define ENOMEM 12 /* Out of memory */

  14. #define EACCES 13 /* Permission denied */

  15. #define EFAULT 14 /* Bad address */

  16. #define ENOTBLK 15 /* Block device required */

  17. #define EBUSY 16 /* Device or resource busy */

  18. #define EEXIST 17 /* File exists */

  19. #define EXDEV 18 /* Cross-device link */

  20. #define ENODEV 19 /* No such device */

  21. #define ENOTDIR 20 /* Not a directory */

  22. #define EISDIR 21 /* Is a directory */

  23. #define EINVAL 22 /* Invalid argument */

  24. #define ENFILE 23 /* File table overflow */

  25. #define EMFILE 24 /* Too many open files */

  26. #define ENOTTY 25 /* Not a typewriter */

  27. #define ETXTBSY 26 /* Text file busy */

  28. #define EFBIG 27 /* File too large */

  29. #define ENOSPC 28 /* No space left on device */

  30. #define ESPIPE 29 /* Illegal seek */

  31. #define EROFS 30 /* Read-only file system */

  32. #define EMLINK 31 /* Too many links */

  33. #define EPIPE 32 /* Broken pipe */

  34. #define EDOM 33 /* Math argument out of domain of func */

  35. #define ERANGE 34 /* Math result not representable */

  36. #define EDEADLK 35 /* Resource deadlock would occur */

  37. #define ENAMETOOLONG 36 /* File name too long */

  38. #define ENOLCK 37 /* No record locks available */

  39. #define ENOSYS 38 /* Function not implemented */

  40. #define ENOTEMPTY 39 /* Directory not empty */

  41. #define ELOOP 40 /* Too many symbolic links encountered */

  42. #define EWOULDBLOCK EAGAIN /* Operation would block */

  43. #define ENOMSG 42 /* No message of desired type */

  44. #define EIDRM 43 /* Identifier removed */

  45. #define ECHRNG 44 /* Channel number out of range */

  46. #define EL2NSYNC 45 /* Level 2 not synchronized */

  47. #define EL3HLT 46 /* Level 3 halted */

  48. #define EL3RST 47 /* Level 3 reset */

  49. #define ELNRNG 48 /* Link number out of range */

  50. #define EUNATCH 49 /* Protocol driver not attached */

  51. #define ENOCSI 50 /* No CSI structure available */

  52. #define EL2HLT 51 /* Level 2 halted */

  53. #define EBADE 52 /* Invalid exchange */

  54. #define EBADR 53 /* Invalid request descriptor */

  55. #define EXFULL 54 /* Exchange full */

  56. #define ENOANO 55 /* No anode */

  57. #define EBADRQC 56 /* Invalid request code */

  58. #define EBADSLT 57 /* Invalid slot */

  59. #define EDEADLOCK EDEADLK

  60. #define EBFONT 59 /* Bad font file format */

  61. #define ENOSTR 60 /* Device not a stream */

  62. #define ENODATA 61 /* No data available */

  63. #define ETIME 62 /* Timer expired */

  64. #define ENOSR 63 /* Out of streams resources */

  65. #define ENONET 64 /* Machine is not on the network */

  66. #define ENOPKG 65 /* Package not installed */

  67. #define EREMOTE 66 /* Object is remote */

  68. #define ENOLINK 67 /* Link has been severed */

  69. #define EADV 68 /* Advertise error */

  70. #define ESRMNT 69 /* Srmount error */

  71. #define ECOMM 70 /* Communication error on send */

  72. #define EPROTO 71 /* Protocol error */

  73. #define EMULTIHOP 72 /* Multihop attempted */

  74. #define EDOTDOT 73 /* RFS specific error */

  75. #define EBADMSG 74 /* Not a data message */

  76. #define EOVERFLOW 75 /* Value too large for defined data type */

  77. #define ENOTUNIQ 76 /* Name not unique on network */

  78. #define EBADFD 77 /* File descriptor in bad state */

  79. #define EREMCHG 78 /* Remote address changed */

  80. #define ELIBACC 79 /* Can not access a needed shared library */

  81. #define ELIBBAD 80 /* Accessing a corrupted shared library */

  82. #define ELIBSCN 81 /* .lib section in a.out corrupted */

  83. #define ELIBMAX 82 /* Attempting to link in too many shared libraries */

  84. #define ELIBEXEC 83 /* Cannot exec a shared library directly */

  85. #define EILSEQ 84 /* Illegal byte sequence */

  86. #define ERESTART 85 /* Interrupted system call should be restarted */

  87. #define ESTRPIPE 86 /* Streams pipe error */

  88. #define EUSERS 87 /* Too many users */

  89. #define ENOTSOCK 88 /* Socket operation on non-socket */

  90. #define EDESTADDRREQ 89 /* Destination address required */

  91. #define EMSGSIZE 90 /* Message too long */

  92. #define EPROTOTYPE 91 /* Protocol wrong type for socket */

  93. #define ENOPROTOOPT 92 /* Protocol not available */

  94. #define EPROTONOSUPPORT 93 /* Protocol not supported */

  95. #define ESOCKTNOSUPPORT 94 /* Socket type not supported */

  96. #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */

  97. #define EPFNOSUPPORT 96 /* Protocol family not supported */

  98. #define EAFNOSUPPORT 97 /* Address family not supported by protocol */

  99. #define EADDRINUSE 98 /* Address already in use */

  100. #define EADDRNOTAVAIL 99 /* Cannot assign requested address */

  101. #define ENETDOWN 100 /* Network is down */

  102. #define ENETUNREACH 101 /* Network is unreachable */

  103. #define ENETRESET 102 /* Network dropped connection because of reset */

  104. #define ECONNABORTED 103 /* Software caused connection abort */

  105. #define ECONNRESET 104 /* Connection reset by peer */

  106. #define ENOBUFS 105 /* No buffer space available */

  107. #define EISCONN 106 /* Transport endpoint is already connected */

  108. #define ENOTCONN 107 /* Transport endpoint is not connected */

  109. #define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */

  110. #define ETOOMANYREFS 109 /* Too many references: cannot splice */

  111. #define ETIMEDOUT 110 /* Connection timed out */

  112. #define ECONNREFUSED 111 /* Connection refused */

  113. #define EHOSTDOWN 112 /* Host is down */

  114. #define EHOSTUNREACH 113 /* No route to host */

  115. #define EALREADY 114 /* Operation already in progress */

  116. #define EINPROGRESS 115 /* Operation now in progress */

  117. #define ESTALE 116 /* Stale NFS file handle */

  118. #define EUCLEAN 117 /* Structure needs cleaning */

  119. #define ENOTNAM 118 /* Not a XENIX named type file */

  120. #define ENAVAIL 119 /* No XENIX semaphores available */

  121. #define EISNAM 120 /* Is a named type file */

  122. #define ENOKEY 126 /* Required key not available */

  123. #define EKEYEXPIRED 127 /* Key has expired */

  124. #define EKEYREVOKED 128 /* Key has been revoked */

  125. #define EKEYREJECTED 129 /* Key was rejected by service */

  126. #define EOWNERDEAD 130 /* Owner died */

  127. #define ENOTRECOVERABLE 131 /* State not recoverable */

  128. #define ERFKILL 132 /* Operation not possible due to RF-kill */

  129. #define EHWPOISON 133 /* Memory page has hardware error */

这样的话就会更加有效率的找到程序崩溃的原因。

 

转自: https://blog.csdn.net/double_lee3/article/details/93496798

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值